User Tools

Site Tools


windows_batch_scripting

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
windows_batch_scripting [2015/03/12 16:37] – created zertrinwindows_batch_scripting [2015/08/04 20:18] (current) – add "Delete folders older than 10 days" zertrin
Line 20: Line 20:
 for /L %%i in (1,1,3) do if /I "%1" equ "!arg[%%i]!" SET "ARG=!arg[%%i]!" for /L %%i in (1,1,3) do if /I "%1" equ "!arg[%%i]!" SET "ARG=!arg[%%i]!"
 </code> </code>
 +
 +==== Escape an exclamation mark when EnableDelayedExpansion is active ====
 +
 +from http://stackoverflow.com/questions/3288552/how-can-i-escape-an-exclamation-mark-in-cmd-scripts
 +
 +<code>
 +@echo off
 +setlocal ENABLEDELAYEDEXPANSION
 +echo I want to go out with a bang^^!
 +</code>
 +
 +Normally the ''^^!'' works, but in quotes you only need ''^!'' instead.
 +
 +<code>
 +echo I want to go out with a bang^^!
 +echo He said "Bang^!"
 +</code>
 +
 +This is a result of the escape mechanism of the batch parser.
 +
 +First the parser parses a line and the caret escapes the next character, in this case it has an effect for ''&|<>()"<linefeed>'', but only outside of quotes, as inside of the quotes all characters are "normal" and the caret itself has no effect.
 +
 +With delayed expansion an extra parse step follows, there is the caret also an escape character for the next character, but only affects the ''!'' and ''^'', and quotes are ignored in this parsing step. This extra step will be executed only, if there is at least one ''!'' in the line.
 +
 +++++ More examples |
 +<code>
 +setlocal DisableDelayedExpansion
 +echo DisableDelayedExpansion
 +echo one caret^^
 +echo one caret^^  bang! "boom^!"
 +
 +setlocal EnableDelayedExpansion
 +echo EnableDelayedExpansion
 +echo one caret^^
 +echo none caret^^  bang^^! "boom^!"
 +</code>
 +
 +OUTPUT:
 +
 +<code>
 +DisableDelayedExpansion
 +one caret^
 +one caret^  bang! "boom^!"
 +
 +EnableDelayedExpansion
 +one caret^
 +none caret  bang! "boom!"
 +</code>
 +
 +Here is a slightly modified example that better illustrates the various escape permutations that are required, depending on the context. The only case that requires unusual escaping is the last example when delayed expansion is on and there exists at least one ! on the line.
 +
 +<code>
 +@echo off
 +setlocal DisableDelayedExpansion
 +echo DisableDelayedExpansion
 +echo caret^^       "caret^"
 +echo caret^^ bang! "caret^ bang!"
 +
 +setlocal EnableDelayedExpansion
 +echo EnableDelayedExpansion
 +echo caret^^       "caret^"
 +echo caret^^^^ bang^^! "caret^^ bang^!"
 +</code>
 +
 +OUTPUT:
 +
 +<code>
 +DisableDelayedExpansion
 +caret^       "caret^"
 +caret^ bang! "caret^ bang!"
 +
 +EnableDelayedExpansion
 +caret^       "caret^"
 +caret^ bang! "caret^ bang!"
 +</code>
 +++++
 +
 +==== Check if the current batch script has admin rights ====
 +
 +from http://stackoverflow.com/questions/4051883/batch-script-how-to-check-for-admin-rights
 +
 +<code>
 +@echo off
 +goto check_Permissions
 +
 +:check_Permissions
 +    echo Administrative permissions required. Detecting permissions...
 +
 +    net session >nul 2>&1
 +    if %errorLevel% == 0 (
 +        echo Success: Administrative permissions confirmed.
 +    ) else (
 +        echo Failure: Current permissions inadequate.
 +    )
 +
 +    pause >nul
 +</code>
 +
 +
 +==== Get current time in a locale independent way ====
 +
 +from http://serverfault.com/questions/227345/locale-unaware-date-and-time-in-batch-files
 +
 +<code>
 +FOR /f %%a in ('WMIC OS GET LocalDateTime ^| find "."') DO set DTS=%%a
 +set CUR_DATE=%DTS:~0,4%-%DTS:~4,2%-%DTS:~6,2%
 +</code>
 +
 +
 +==== Delete folders older than 10 days ====
 +
 +from http://stackoverflow.com/questions/5497211/batch-file-to-delete-folders-older-than-10-days-in-windows-7
 +
 +<code>
 +FORFILES /S /D -10 /C "cmd /c IF @isdir == TRUE rd /S /Q @path"
 +</code>
 +
windows_batch_scripting.1426149467.txt.gz · Last modified: by zertrin