Link to home
Start Free TrialLog in
Avatar of bwet5753
bwet5753

asked on

For loop in Batch File

I'm working on a For loop in a batch file.  The Batch file should loop through a text file and store the data as variables.  The Variable will be used for setting up Outlook email signatures.  Currently when the Batch file is run, it completes, but on the last set of records are used to create the HTML signature file.  I've attached the Batch file and the text file.  Please Help!
echo off
for /f "tokens=1-4 delims=," %%i in (users.txt) do (
set user=%%i
set title=%%j
set ext=%%k
set alais=%%l
)
REM CREATES FOLDER FOR HOLDING SIGNATURES
SET SIGNATURE="C:\Documents and Settings\NAME\Desktop\Email Signature maker\%user%\
IF NOT EXIST %SIGNATURE%" MD %SIGNATURE%"

REM CREATES THE HTML SIGNATURE FILE
ECHO ^<html^> >> %SIGNATURE%\%SIGNATURE%\test_sig.html
ECHO ^<head^> >> %SIGNATURE%\test_sig.html
ECHO ^<font size="2"^> >> %SIGNATURE%\test_sig.html
ECHO ^<font face="Verdana"^> >> %SIGNATURE%\test_sig.html

Open in new window

user-list.JPG
Avatar of vikas_madhusudana
vikas_madhusudana
Flag of India image

i guess to have it created for all users you should be doing it for each and every users you are creating the signature outside so it will be taking only the last values so script should be modified as below

echo off
for /f "tokens=1-4 delims=," %%i in (users.txt) do (
set user=%%i
set title=%%j
set ext=%%k
set alais=%%l
REM CREATES FOLDER FOR HOLDING SIGNATURES
SET SIGNATURE="C:\Documents and Settings\NAME\Desktop\Email Signature maker\%user%\
IF NOT EXIST %SIGNATURE%" MD %SIGNATURE%"

REM CREATES THE HTML SIGNATURE FILE
ECHO ^<html^> >> %SIGNATURE%\%SIGNATURE%\test_sig.html
ECHO ^<head^> >> %SIGNATURE%\test_sig.html
ECHO ^<font size="2"^> >> %SIGNATURE%\test_sig.html
ECHO ^<font face="Verdana"^> >> %SIGNATURE%\test_sig.html
)
Avatar of bwet5753
bwet5753

ASKER

No really sure I follow, and also did you post the modifications?  Can you tell me what you changed?
I have put the part of the script that was outside the for loop inside the for loop.what i mean to say is for each users i will be creating the signature under the folder singature maker.
That still only created one file.  Do you want me to post both files (batchfile and text file)?
Ya it will create one file it will be for last user in the file because the user variable will have some value in it when for loop exits
How can I create one file per user?  
echo off
for /f "tokens=1-4 delims=," %%i in (users.txt) do (
set user=%%i
set title=%%j
set ext=%%k
set alais=%%l
REM CREATES FOLDER FOR HOLDING SIGNATURES
SET SIGNATURE="C:\Documents and Settings\NAME\Desktop\Email Signature maker\%%i\
IF NOT EXIST %SIGNATURE%" MD %SIGNATURE%"

REM CREATES THE HTML SIGNATURE FILE
ECHO ^<html^> >> %SIGNATURE%\%SIGNATURE%\test_sig.html
ECHO ^<head^> >> %SIGNATURE%\test_sig.html
ECHO ^<font size="2"^> >> %SIGNATURE%\test_sig.html
ECHO ^<font face="Verdana"^> >> %SIGNATURE%\test_sig.html
)
OK, you want to give this one more try.  I'm including both the batch file and the user list.  It's still only creating one signature file.
scripts.zip
ASKER CERTIFIED SOLUTION
Avatar of vikas_madhusudana
vikas_madhusudana
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think you nailed it.  I will test it more intensively at the office in the AM and award the points then.  Thanks for your help.
The problem is that commands inside a block with parentheses around them will be loaded into the interpreter all in one go, and %variable% expansion will occur at the point the interpreter encounters the first opening parenthesis. All the modifications you make to your variables inside the loop will have no effect.

To fix this, you must use delayed expansion. At the start of your batch file, put:

SETLOCAL EnableDelayedExpansion

Then inside the loop, use !variable! instead of %variable%.

Type SET /? for a more detailed explanation of delayed expansion.
My EE account expired and just learned of this.  Please accept the points and my apology.