Difference between revisions of "Windows batch"

From Ever changing code
Jump to navigation Jump to search
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
= Fciv - integration check =
The script assumes the md5 hash value is contained in a .md5 file and the naming convention appears as so:
download.7z
download.7z.md5
<source lang=bash>
:md5
for /f %%f in ('dir "*.md5" /s/b') do (
set file=%%f
for /f %%a in ('type "!file!"') do (
set md5=%%a
set md5=!md5:"=!
set file=!file:~,-4!
for /f "skip=3" %%b in ('fciv "!file!"') do (
if [!md5!]==[%%b] (
echo PASS: !file!
echo MD5: !md5! vs. %%b
echo.
) else (
echo !!! FAIL: !file!
echo MD5: !md5! vs. %%b
)
)
)
)
</source>
= IF and ELSE =
;The else needs to be on the same "line" as the if. From IF /? 'The ELSE clause must occur on the same line as the command after the IF.'
if "zz"=="TRUE" (
    copy /a zz + /a ee=/a zz
) else (
    copy /a e + /a %TMP%=/a e
)
;Environment variables in batch files are expanded when a line is parsed.
In the case of blocks delimited by parentheses (as your if defined) the whole block counts as a "line" or command and it is read once. This means that all occurrences of %FOO% are replaced by their values before the block is run. Therefore %FOO% will be replaced by nothing as it was not set.
To solve this you can enable delayed expansion:
setlocal enabledelayedexpansion
Delayed expansion causes variables delimited by exclamation marks (!) to be evaluated on execution instead of parsing which will ensure the correct behavior in our case:
if not defined BAR (
    set FOO=1
    echo Foo: !FOO!
)
= Text manipulation =
= Text manipulation =
*color 0a - changes text to green
*color 0a - changes text to green
*mode con: cols=80 lines=50 - changes cmd.exe window, no scroll available, type '''mode''' to display the current settings
*mode con: cols=80 lines=50 - changes cmd.exe window, no scroll available, type '''mode''' to display the current settings
= References =
= References =
*[http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true Using batch parameters]
*[http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true Using batch parameters]
*[http://technet.microsoft.com/en-us/library/bb490890.aspx Command-line reference A-Z] For tylda parameters look for FOR
*[http://technet.microsoft.com/en-us/library/bb490890.aspx Command-line reference A-Z] For tylda parameters look for FOR
*[http://stackoverflow.com/questions/112055/what-does-d0-mean-in-a-windows-batch-file What does %~d0 mean in a Windows batch file?]
*[http://stackoverflow.com/questions/112055/what-does-d0-mean-in-a-windows-batch-file What does %~d0 mean in a Windows batch file?]
[[Category:Windows]]

Latest revision as of 13:51, 13 December 2015

Fciv - integration check

The script assumes the md5 hash value is contained in a .md5 file and the naming convention appears as so:

download.7z
download.7z.md5 
:md5
 for /f %%f in ('dir "*.md5" /s/b') do (
 	set file=%%f
 	for /f %%a in ('type "!file!"') do (
 		set md5=%%a
 		set md5=!md5:"=!
 		set file=!file:~,-4!
 		for /f "skip=3" %%b in ('fciv "!file!"') do (
 			if [!md5!]==[%%b] (
 				echo PASS: !file!
 				echo MD5: !md5! vs. %%b
 				echo.
 			) else (
 				echo !!! FAIL: !file!
 				echo MD5: !md5! vs. %%b
 			)
 		)
 	)
 )

IF and ELSE

The else needs to be on the same "line" as the if. From IF /? 'The ELSE clause must occur on the same line as the command after the IF.'
if "zz"=="TRUE" (
    copy /a zz + /a ee=/a zz
) else (
    copy /a e + /a %TMP%=/a e
)
Environment variables in batch files are expanded when a line is parsed.

In the case of blocks delimited by parentheses (as your if defined) the whole block counts as a "line" or command and it is read once. This means that all occurrences of %FOO% are replaced by their values before the block is run. Therefore %FOO% will be replaced by nothing as it was not set.

To solve this you can enable delayed expansion:

setlocal enabledelayedexpansion

Delayed expansion causes variables delimited by exclamation marks (!) to be evaluated on execution instead of parsing which will ensure the correct behavior in our case:

if not defined BAR (
    set FOO=1
    echo Foo: !FOO!
)

Text manipulation

  • color 0a - changes text to green
  • mode con: cols=80 lines=50 - changes cmd.exe window, no scroll available, type mode to display the current settings

References