Strange question about windows batch file - scripting

I got 1.txt and 2.txt in my working directory. I use the following batch to list all the files.
The batch is this:
#echo off
for /f "tokens=*" %%a in ('dir *.txt /b') do (
echo ---------------
set file_variable=%%a
echo file_variable=%file_variable%
echo filename=%%a
)
The result is below:
---------------
file_variable=2.txt <---------------why it is not 1.txt here??
filename=1.txt
---------------
file_variable=2.txt
filename=2.txt
Thanks.

You need to put:
#setlocal enableextensions enabledelayedexpansion
at the top of your file and
endlocal
at the end.
Then you need to use the delayed expansion substitution characters.
#setlocal enableextensions enabledelayedexpansion
#echo off
for /f "tokens=*" %%a in ('dir *.txt /b') do (
echo ---------------
set file_variable=%%a
echo file_variable=!file_variable!
echo filename=%%a
)
endlocal
C:\Documents and Settings\Pax\My Documents> qq.cmd
---------------
file_variable=1.txt
filename=1.txt
---------------
file_variable=2.txt
filename=2.txt
What you're seeing without delayed expansion is that the entire for loop is being evaluated before running. That includes the substitution, so that %file_variable% will be replaced with the value it held before the loop started. Using delayed expansion defers the evaluation until the actual line is executed.
There are all sorts of wonderful Windows scripting tricks over at Rob van der Woude's site, containing quite a lot of different ways of doing things under Windows with various tools.

In this particular case you could get the correct output if you ECHOed file_variable like this:
CALL ECHO file_variable=%%file_variable%%
This approach is less flexible and probably less performant than the one described in #paxdiablo's answer. It's probably just a quick-and-dirty method of delaying the expansion of a variable without the need to enable the special syntax for that.

Related

Batch file not keeping variables

Why doesn't the below batch file keep variables when CMD.exe terminates
#echo off
cmd /c "set var=hi"
if defined var (echo var is defined ("var=%var%")) else (echo var isn't defined)
pause
exit
Is there any way to use CMD /c, whilst keeping variables? and why doesn't CMD /c keep variables?
CMD /C starts a new sub-process that has its own environment space where variables are stored in memory. When that process terminates, the associated environment space is lost, so of course you lose the definition of any variables that were defined there.
It is not possible for the sub-process to directly access or manipulate the variables of the parent process.
If you are executing a simple command and have control over any output, then you can write the definitions to stdout and let FOR /F capture the output so that you can set the values in your parent process. But you will need delayed expansion.
Note - this example is pretty inane, but it should get the point across
for /f "delims=" %%A in ('cmd /v:on /c "set var=hi&echo "var=!var!""') do set %%A
But things can get complicated as far as identifying what is quoted, what needs to be escaped, ...
Life can be simplified if you create a batch script that does the work and execute it with FOR /F.
test.bat
#echo off
set var1=hi
set var2=Ain't this fun
set var3=bye
setlocal enableDelayedExpansion
for %%V in (var1 var2 var3) do echo "%%V=!%%V!"
main.bat
#echo off
for /f "delims=" %%A in ('cmd /c test.bat') do set %%A
Actually, FOR /F uses an implicit CMD /C, and you no longer need to enable delayed expansion in the command, so you can ditch the explicit CMD /C
#echo off
for /f "delims=" %%A in ('test.bat') do set %%A
If you don't have control of the output, then you will have to write the values to a temporary file that the main script can then load. You might be tempted to write a temporary batch script that defines the variables, but then you run the risk of having to escape some characters, depending on the content of the variables. The safest option is to use delayed expansion to write out the variable names and values, and let the parent process use FOR /F to load them in.
test2.bat
#echo off
set var1=hi
set var2=Ain't this fun
set var3=bye
echo Here is some output that must be preserved, unrelated to variables
setlocal enableDelayedExpansion
(for %%V in (var1 var2 var3) do echo "%%V=!%%V!") >"%temp%\vars.temp"
main.bat
#echo off
setlocal disableDelayedExpansion
cmd /c test2.bat
call "%temp%\vars.bat"
for /f "usebackq delims=" %%V in ("%temp%\vars.temp") do set "%%V"
del "%temp%\vars.temp"
Note that delayed expansion must be disabled in the main process when you load the variables, otherwise you will lose any ! characters in values when %%V is expanded.
But to be robust, you should do something to make sure that each instantiation uses a unique temp file name so that simultaneous runs do not clobber eachother.

Insert Input into a variable

I'm working on a batch script for fun and learning.
I've set up multiple choises, e.g Fint IP address, MAC address...and so on.
The problem is that i can't find out how i can insert the output of those two lines into variables.
for /f "usebackq skip=1" %%f in (`wmic COMPUTERSYSTEM get name`) do ???
for /f "usebackq skip=1" %%f in (`wmic COMPUTERSYSTEM get domain`) do ???
So I can use the output in a sentence like;
Your DNS-name is %dns_name_output% and your domain is %domain_name_output%.
try this:
for /f %%f in ("%computername%") do set "name=%%f"
for /f %%f in ("%userdomain%") do set "domain=%%f"
echo %name% %domain%
To capture wmic get output, use the /value flag like this
for /f "tokens=2 delims==" %%A in ('wmic computersystem get name /value') do set "Name=%%A"
for /f "tokens=2 delims==" %%A in ('wmic computersystem get domain /value') do set "Domain=%%A"
The problem you are seeing arises from the fact that WMIC output is in unicode. By some mechanism that I do not fully understand (a bug?), the FOR /F command transforms the unicode command output into ASCII, but mysteriously appends an extra carriage return (<CR>) at the end of each line.
FOR /F does not return empty lines, but the mysterious and seemingly blank lines are not really blank - they contain a <CR>.
Even if the extra lines are properly ignored, the last value in the list will include an unwanted <CR> that is included when assigning the value to an environment variable. The <CR> will not be apparent if the variable is later expanded normally using %VAR% because the command parser automatically strips all <CR> characters. But the <CR> is preserved and can cause problems if delayed expansion !VAR! is used.
The FOR /F command strips the last character from each line if it happens to be a <CR>. So passing the value through an extra FOR /F will eliminate the problem. David Ruhman's suggestion to use the /value switch is a good one, and can be improved upon. Multiple values may be requested in one loop, and the property name can be used as the variable name. Having only one name/value pair per line eliminates potential parsing problems with spaces and or commas in values.
The commas in the WMIC command must either be escaped or quoted when used within FOR /F. In this case, quoting the entire command seems easiest. The following will properly define two environment variables - Domain, and Name:
for /f "delims=" %%A in ('"wmic computersystem get domain, name /value"') do (
for /f "tokens=1* delims==" %%B in ("%%A") do set "%%B=%%C"
)
echo Your host name is %name% and your domain is %domain%
This works using wmic without any odd spaces or CRs embedded.
#echo off
for /f "tokens=2 delims=<>" %%a in ('wmic COMPUTERSYSTEM get name /format:htable^|find "hidden"') do set "name=%%a"
for /f "tokens=2 delims=<>" %%a in ('wmic COMPUTERSYSTEM get domain /format:htable^|find "hidden"') do set "dom=%%a"
echo "%name%,%dom%"
pause
You can define a variable, and assign it's value to the result of a command using back-tick enclosure.
Try this:
a=`ls`
echo $a

How to add variables in a batch file

I'm trying to get a list of numbers an letters like 11111111 and a letter. Letter si calculated with the number. Something like:
#echo off
set var=23
FOR /L %%H IN (40000000,1,49999999) DO (
set number = %%H+%var%
echo %number%A
)
pause
exit
I want to get (more or less)
400000023A
400000024A
400000025A
...
But without changing the FOR sentence... Is that possible?
To continue with what jeb stated, for your requested output, the code should be this:
#echo off & setlocal EnableDelayedExpansion
set var=23
FOR /L %%H IN (40000000,1,49999999) DO (
set /a number=%%H+!var!
echo !number!A
)
endlocal
pause
exit
Avoid spaces with set statements
Use set /a if you want to calculate
set /a number=%%H+var
Avoid percent expansion in a block (and also in a FOR block)
Use delayed expansion instead (add a setlocal EnableDelayedExpansion to your file)
echo !number!A
To use numbers in batch use
set /a
See set /? for all the info.
jeb provided the hints needed to fix your code, and Jeff K actually implemented the fix.
However, since you are adding a constant to every value, it is simpler and more efficient to add the constant to the FOR /L start and end values.
#echo off
setlocal
set var=23
set /a "start=40000000+var, end=49999999+var"
for /l %%H in (%start%, 1, %end%) do echo %%HA
There is, but with changing the FOR sentence.
You actually forgot the setlocal command right there.
setlocal EnableDelayedExpansion
This command is needed in order to add variables.
Still don't get it?
Here's the code so that you will add variables.
#echo off
setlocal EnableDelayedExpansion
cls
set var=17
echo %var%
pause
set /a var=%var%+7
echo %var%
pause
Try to find any errors and mistakes on my code. Then test it.
There you have it! Added variables!
The best method is:
set /a counter+=1
so:
#echo off
set /a counter=
:Top
set /a counter+=1
echo 4000000%counter%A
GOTO Top
You can copy the code above.

concatenate multiple variable's names in batch/CMD

searched this site and others - no joy.
:: first for loop
for /L %%x in (1,1,2) do (
generic-executable.output > grab1-%%x.txt
:: second, nested for loop
for /f "delims=" %%i in (grab1-%%x.txt) do (set grab1=%%i)
echo variable string is %grab1%%x%
generic-executable.output > grab2-%%x.txt
for /f "delims=" %%i in (grab2-%%x.txt) do (set grab2=%%i)
echo variable string is %grab2%%x%
)
Trying to run a nested for loop that will
1) write data to the file
2) take data from the file and save it to another variable.
The names of the end variables should be a concatenation of each of the for loops (i.e. grab1-1, 2-1, 1-2, 2-2).
Saving the data to the variables is no problem, formatting the variables to recall the data IS.
I'm most likely missing something in the formatting of the concatenated variable. I've tried single ', double ", ^, !, one %, two %, backslash, ACK!! ... the closest I've gotten is
echo %grab1-%%x
gave:
%grab1-1
I'd appreciate any tips you can provide.
Thanks,
Dave
You have run into a classic stumbling block for batch newbies: You cannot set a variable within a loop (within parentheses) and then access the value using %var% within the same loop. The Entire loop (parenthesized block of code) is parsed in one pass, and %var% is expanded at parse time. So you see the value of var as it was prior to the loop executing.
The solution is to enable delayed expansion using setlocal enableDelayedExpansion near the top of your script, and then expand the variable using delayed expansion as !var!. Delayed expansion means the value is expanded at run time - exactly what you want.
I believe this is what you were trying to achieve
setlocal enableDelayedExpansion
for /L %%x in (1,1,2) do (
genericOutput1.exe > grab1-%%x.txt
for /f "delims=" %%i in (grab1-%%x.txt) do set "grab1-%%x=%%i"
echo grab1-%%x variable string is !grab1-%%x!
genericOutput2.exe > grab2-%%x.txt
for /f "delims=" %%i in (grab2-%%x.txt) do set "grab2-%%x=%%i"
echo grab2-%%x variable string is !grab2-%%x!
)
::List all of the grab variable defined
set grab
You don't need to save the output of your executables to a file. (Unless of course that is your requirement). You can use FOR /F to process the output directly. The FOR command has many variants that look nearly identical, yet behave very differently. This variant uses single quotes to cause FOR /F to process a command.
setlocal enableDelayedExpansion
for /L %%x in (1,1,2) do (
for /f "delims=" %%i in ('genericOutput1.exe') do set "grab1-%%x=%%i"
echo grab1-%%x variable string is !grab1-%%x!
for /f "delims=" %%i in ('genericOutput1.exe') do set "grab2-%%x=%%i"
echo grab2-%%x variable string is !grab2-%%x!
)
::List all of the grab variable defined
set grab
Note that FOR /F will iterate each line (whether it be from a text file or from command output). Your algorithm will only save and print the content of the last line - each successive line will overwrite the value from the prior line.

batch file fails to set variable in IF clause

The following code is not updating Run to equal N even though the match occurs. this means I'm not dropping into the CALL code. Am i missing something here?
SET Run=Y
REM Check current files date/time information and establish if the file has been present too long in the directory
REM Skip first 4 lines as header information not required
FOR /f "tokens=1-5 skip=4 delims= " %%G IN ('dir /OD "%SRCH_CRITERIA% "') DO (
ECHO "Params to processFile: " %%G %%H %%I ""%%K""
IF %%K.==. (
ECHO "K:nothing"
SET Run=N
ECHO %Run%
)
IF %%K==free (
ECHO "K:FREE"
SET Run=N
ECHO %Run%
)
ECHO %Run% RUN
IF %Run%=="Y" (
CALL :processFile "%%G" "%%H" "%%I" "%%K"
)
)
You need to use the delayed expansion option of cmd.exe.
At the top of your script, put:
setlocal enableextensions enabledelayedexpansion
and then put:
endlocal
at the bottom.
Then you need to use !Run! instead of %Run%.
The reason your code is not working is that the entire FOR statement (including the commands within it) is evaluated when it's encountered. That's the point where the %Run% variables are expanded.
By using deferred expansion, you don't expand them until they're actually needed (after you've set them within the block).
You can see the difference in this script:
#echo off
setlocal enableextensions enabledelayedexpansion
set xx=0
for %%i in (a b c d) do (
echo %%i
set /a "xx = xx + 1"
if %xx%==3 echo yes for normal
if !xx!==3 echo yes for delayed
)
endlocal
which outputs:
a
b
c
yes for delayed
d
You'll notice that the check with %xx% does not work because that was evaluated when the for statement started (and xx was 0). The delayed-expansion !xx! does work since that is evaluated each time through the loop.