Reputation: 59
I have batch script which displays the variable content (%Build%) in a mail body.
for example if the Varaible content is having: %Build%= aa001.skc
the mail which is triggered by the script has the mail body as below (OUTPUT).
"Skip file generated for the files aa001.skc"
(Imagine test.log contains the value aa001.skc in it)
below is my current script for the same:
@echo off
for /f "delims=" %%x in (C:\Users\Desktop\test.log) do (
set Build=%%x
echo %Build%
)
"blat.exe" -to "[email protected]" -cc "[email protected]","[email protected]" -f [email protected] -body "Skip file generated for the files %Build%" -subject "Skip file found" -server mailout.xyz.com
exit
But if the test.log contains values as
aa001.skc
aa002.skc
aa003.skc
with the above script my output is showing only as below in the mail body. Current output: "Skip file generated for the files aa003.skc" (Its taking the last value, not sure how to put it in for loop in mail body)
But my OUTPUT of the mail body should be like below.
"Skip file generated for the files
aa001.skc
aa002.skc
aa003.skc"
Kindly help me achieve the same.
Upvotes: 0
Views: 1270
Reputation: 80033
It's normal for batch files to commence with a setlocal
instruction which effectively ensures that the environment remains unchanged at the end of the batch, so running successive batches does not accumulate changes to contaminate for further batch executions.
To fix your process - version 1
@echo off
setlocal
for /f "delims=" %%x in (C:\Users\Desktop\test.log) do (
call set "Build=%%x %%build%%"
call echo %%Build%%
)
blat ......
(in this one, the setlocal
is not strictly necessary - I'm assuming it's being call
ed from another batch)
[EDIT: to add newlines]
Poorly-documented featue of BLAT is that a pipe character (|
) in the body is transformed to a newline, hence
@echo off
setlocal
set "build=|"
for /f "delims=" %%x in (C:\Users\Desktop\test.log) do (
call set "Build=%%x|%%build%%"
call echo %%Build%%
)
blat ......
You may also wish to insert a pipe just after %build%
in the -body
parameter.
[end-edit]
Another common way is to use delayedexpansion
@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%x in (C:\Users\Desktop\test.log) do (
set "Build=%%x !build!"
echo !Build!
)
blat ......
(setlocal enabledelayedexpansionrequired, notice the
call`s have gone.
Within a block statement (a parenthesised series of statements)
, the entire block is parsed and then executed. Any %var%
within the block will be replaced by that variable's value at the time the block is parsed - before the block is executed - the same thing applies to a FOR ... DO (block)
.
Hence - actually something you didn't say. Your code should have shown the original value of build
three times. Now - if this had been retained from an earlier run, it would have shown the last value three times. If on the other hand you had a clean environment, then since build
would not have been set, you should have seen echo is off
three times.
See endless SO items on delayedexpansion
for more info.
Upvotes: 1