简体   繁体   中英

Batch script to display variable content in mail body

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 "abc@gmail.com" -cc "abc@gmail.com","xyz@gmail.com" -f 123@test.com -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.

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 enabledelayedexpansion required, 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM