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
user-list.JPG
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.
ASKER
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
ASKER
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\Emai l Signature maker\%%i\
IF NOT EXIST %SIGNATURE%" MD %SIGNATURE%"
REM CREATES THE HTML SIGNATURE FILE
ECHO ^<html^> >> %SIGNATURE%\%SIGNATURE%\te st_sig.htm l
ECHO ^<head^> >> %SIGNATURE%\test_sig.html
ECHO ^<font size="2"^> >> %SIGNATURE%\test_sig.html
ECHO ^<font face="Verdana"^> >> %SIGNATURE%\test_sig.html
)
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\Emai
IF NOT EXIST %SIGNATURE%" MD %SIGNATURE%"
REM CREATES THE HTML SIGNATURE FILE
ECHO ^<html^> >> %SIGNATURE%\%SIGNATURE%\te
ECHO ^<head^> >> %SIGNATURE%\test_sig.html
ECHO ^<font size="2"^> >> %SIGNATURE%\test_sig.html
ECHO ^<font face="Verdana"^> >> %SIGNATURE%\test_sig.html
)
ASKER
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
scripts.zip
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
ASKER
My EE account expired and just learned of this. Please accept the points and my apology.
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\Emai
IF NOT EXIST %SIGNATURE%" MD %SIGNATURE%"
REM CREATES THE HTML SIGNATURE FILE
ECHO ^<html^> >> %SIGNATURE%\%SIGNATURE%\te
ECHO ^<head^> >> %SIGNATURE%\test_sig.html
ECHO ^<font size="2"^> >> %SIGNATURE%\test_sig.html
ECHO ^<font face="Verdana"^> >> %SIGNATURE%\test_sig.html
)