Okay here some more complex sample for the use of For /F
:: Main
@prompt -$G
call :REGQUERY "Software\Classes\CLSID\{3E6AE265-3382-A429-56D1-BB2B4D1D}"
@goto :EOF
:REGQUERY
:: Checks HKEY_LOCAL_MACHINE\ and HKEY_CURRENT_USER\
:: for the key and lists its content
@call :EXEC "REG QUERY HKCU\%~1"
@call :EXEC "REG QUERY "HKLM\%~1""
@goto :EOF
:EXEC
@set output=
@for /F "delims=" %%i in ('%~1 2^>nul') do @(
set output=%%i
)
@if not "%output%"=="" (
echo %1 -^> %output%
)
@goto :EOF
I packed it into the sub function :EXEC so all of its nasty details of implementation doesn't litters the main script.
So it got some kinda some batch tutorial.
Notes 'bout the code:
- the output from the command executed via call :EXEC command is stored in %output%. Batch cmd doesn't cares about scopes so %output% will be also available in the main script.
- the @ the beginning is just decoration and there to suppress echoing the command line. You may delete them all and just put some @echo off at the first line is really dislike that. However like this I find debugging much more nice.
Decoration Number two is prompt -$G. It's there to make command prompt look like this ->
- I use :: instead of rem
- the tilde(~) in %~1 is to remove quotes from the first argument
- 2^>nul is there to suppress/discard stderr error output. Normally you would do it via 2>nul. Well the ^ the batch escape char is there avoids to early resolving the redirector(>). There's some simulare use a little later in the script:
echo %1 -^>... so there ^ makes it possible the output a '>' via echo what else wouldn't have been possible.
- even if the compare at
@if not "%output%"==""looks like in most common programming languages - it's maybe different that you expected (if you're not used to MS-batch). Well remove the '@' at the beginning. Study the output. Change it tonot %output%==""-rerun and consider why this doesn't work. ;)