windows_batch_scripting
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| windows_batch_scripting [2015/03/12 16:37] – created zertrin | windows_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 " | for /L %%i in (1,1,3) do if /I " | ||
| </ | </ | ||
| + | |||
| + | ==== Escape an exclamation mark when EnableDelayedExpansion is active ==== | ||
| + | |||
| + | from http:// | ||
| + | |||
| + | < | ||
| + | @echo off | ||
| + | setlocal ENABLEDELAYEDEXPANSION | ||
| + | echo I want to go out with a bang^^! | ||
| + | </ | ||
| + | |||
| + | Normally the '' | ||
| + | |||
| + | < | ||
| + | echo I want to go out with a bang^^! | ||
| + | echo He said " | ||
| + | </ | ||
| + | |||
| + | 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 ''& | ||
| + | |||
| + | With delayed expansion an extra parse step follows, there is the caret also an escape character for the next character, but only affects the '' | ||
| + | |||
| + | ++++ More examples | | ||
| + | < | ||
| + | setlocal DisableDelayedExpansion | ||
| + | echo DisableDelayedExpansion | ||
| + | echo one caret^^ | ||
| + | echo one caret^^ | ||
| + | |||
| + | setlocal EnableDelayedExpansion | ||
| + | echo EnableDelayedExpansion | ||
| + | echo one caret^^ | ||
| + | echo none caret^^ | ||
| + | </ | ||
| + | |||
| + | OUTPUT: | ||
| + | |||
| + | < | ||
| + | DisableDelayedExpansion | ||
| + | one caret^ | ||
| + | one caret^ | ||
| + | |||
| + | EnableDelayedExpansion | ||
| + | one caret^ | ||
| + | none caret bang! " | ||
| + | </ | ||
| + | |||
| + | 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. | ||
| + | |||
| + | < | ||
| + | @echo off | ||
| + | setlocal DisableDelayedExpansion | ||
| + | echo DisableDelayedExpansion | ||
| + | echo caret^^ | ||
| + | echo caret^^ bang! " | ||
| + | |||
| + | setlocal EnableDelayedExpansion | ||
| + | echo EnableDelayedExpansion | ||
| + | echo caret^^ | ||
| + | echo caret^^^^ bang^^! " | ||
| + | </ | ||
| + | |||
| + | OUTPUT: | ||
| + | |||
| + | < | ||
| + | DisableDelayedExpansion | ||
| + | caret^ | ||
| + | caret^ bang! " | ||
| + | |||
| + | EnableDelayedExpansion | ||
| + | caret^ | ||
| + | caret^ bang! " | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | ==== Check if the current batch script has admin rights ==== | ||
| + | |||
| + | from http:// | ||
| + | |||
| + | < | ||
| + | @echo off | ||
| + | goto 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 | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Get current time in a locale independent way ==== | ||
| + | |||
| + | from http:// | ||
| + | |||
| + | < | ||
| + | FOR /f %%a in ('WMIC OS GET LocalDateTime ^| find " | ||
| + | set CUR_DATE=%DTS: | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Delete folders older than 10 days ==== | ||
| + | |||
| + | from http:// | ||
| + | |||
| + | < | ||
| + | FORFILES /S /D -10 /C "cmd /c IF @isdir == TRUE rd /S /Q @path" | ||
| + | </ | ||
| + | |||
windows_batch_scripting.1426149467.txt.gz · Last modified: by zertrin
