Skip to content

Instantly share code, notes, and snippets.

@davidruhmann
davidruhmann / Example_Shortcut.bat
Created December 4, 2012 19:08
[Batch] Create a shortcut in batch using WSH (WScript)
:: Example Usage for the Shortcut function.
:: Create a shortcut in batch using WSH.
:: by David Ruhmann
:: Hide Commands
@echo off
:: Isolate Scope
setlocal
@davidruhmann
davidruhmann / AppendHex.bat
Created December 4, 2012 19:49
[Batch] Generate a GUID in a command line batch
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:AppendHex <xReturn> <xInput>
:: Append a hexidecimal number to the end of the input.
:: 1. Generate Random Number = 0-15
:: 2. Convert Number to Hexidecimal
:: 3. Append to Input
setlocal
set /a "xValue=%RANDOM% %% 16"
if "%xValue%"=="10" set "xValue=A"
if "%xValue%"=="11" set "xValue=B"
@davidruhmann
davidruhmann / Example_Line.bat
Created December 4, 2012 21:07
[Batch] Get the line number of a statement within a batch file. (Debugging)
:: Batch Code Line Debugging
:: Retrieve the batch file line number where the Line function is called.
:: by David Ruhmann
:: Hide Command and Isolate Scope
@echo off
setlocal
:: Usage Example
call :False xResult
@davidruhmann
davidruhmann / Example_DelayedExpansion.bat
Created December 4, 2012 21:27
[Batch] Detect SetLocal DelayedExpansion or EnableExtensions support.
:: Detect if Delayed Expansion or Command Extensions is supported.
:: Identify if delayed expansion is supported and handle both scenarios.
:: by David Ruhmann
:: Hide Commands
@echo off
rem setlocal /?
rem VERIFY OTHER 2>nul
rem SETLOCAL ENABLEEXTENSIONS
@davidruhmann
davidruhmann / Example_Parse.bat
Last active October 13, 2015 16:48
[Batch] Parse the command line parameters for easy consumption
:: Parse Example
:: Set a variable for each parameter flag to the value. flag:value, xflag=value
:: by David Ruhmann
:: Hide Commands
@echo off
:: Initilize Example
set "xParams=%*"
echo.%*
@davidruhmann
davidruhmann / Log.bat
Created December 6, 2012 22:49
[Batch] Log a message to a file and or the screen
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Log <xLog> <xMessage> [xParrot] [xSession]
:: Append the message with a timestamp out to the specified log file and repeat
:: the message to the display if xParrot is set to 1.
if not "%~1"=="" if exist "%~1" echo.%Date% %Time% %~4: %~2 >> "%~1"
if /i "%~3"=="1" echo.%~2
goto :eof
:: by David Ruhmann
@davidruhmann
davidruhmann / Example_WindowsVersion.bat
Created December 6, 2012 23:45
[Batch] Get and Verify the version of Windows
:: Get and Verify the version of Windows
:: by David Ruhmann
:: Hide Commands
@echo off
echo.
call :WindowsVersion xMajor xMinor
echo.%xMajor%
echo.%xMinor%
@davidruhmann
davidruhmann / IsDecimal.bat
Created December 6, 2012 23:47
[Batch] Numeric validation methods.
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:IsDecimal <xReturn> <xInput> [xDelims]
:: Return true if the input is a decimal 10.0 number, else return false.
:::: Only allows for period seperator unless specified by xDelims. ,[tab][space]
call :IsNumber %1 %2 %3
goto :eof
:: by David Ruhmann
@davidruhmann
davidruhmann / Example_IsDefined.bat
Created December 7, 2012 17:00
[Batch] Detect if a variable name is defined with a value.
:: Determine if a Variable name is defined with a value.
:: by David Ruhmann
:: Hide Commands
@echo off
setlocal ENABLEEXTENSIONS
:: Example Usage
set "xVar=Value"
call :IsDefined xResult xVar
@davidruhmann
davidruhmann / ReplaceWith.bat
Created December 8, 2012 01:59
[Batch] Replace a line in a file containing the term with the entire contents of the input file.
:: Hide Command
@echo off
set "xResult="
::if exist "Output.xml" del /f /q "Output.xml"
call :ReplaceWith xResult "Source.xml" "<Tools>" "Input.xml" "Output.xml"
echo.%xResult%
goto End