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
bwet5753Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vikas_madhusudanaCommented:
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
)
0
bwet5753Author Commented:
No really sure I follow, and also did you post the modifications?  Can you tell me what you changed?
0
vikas_madhusudanaCommented:
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.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

bwet5753Author Commented:
That still only created one file.  Do you want me to post both files (batchfile and text file)?
0
vikas_madhusudanaCommented:
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
0
bwet5753Author Commented:
How can I create one file per user?  
0
vikas_madhusudanaCommented:
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
)
0
bwet5753Author Commented:
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
0
vikas_madhusudanaCommented:
Modified a bit try this

@echo off
for /f "tokens=1,2,3,4 delims=," %%i in (users.txt) do (
echo %%i %%j %%k %%l
SET SIGNATURE="C:\Documents and Settings\bwimberly\Desktop\Email Signature maker\%%i\"
echo L:\Downloads\dont delete\scripts\%%i\
mkdir "C:\Documents and Settings\bwimberly\Desktop\Email Signature maker\%%i\"

rem echo "test" >> "C:\Documents and Settings\bwimberly\Desktop\Email Signature maker\%%i\%%i.html"
ECHO ^<html^> >> "C:\Documents and Settings\bwimberly\Desktop\Email Signature maker\%%i\%%i.html"
ECHO ^<head^> >> "C:\Documents and Settings\bwimberly\Desktop\Email Signature maker\%%i\%%i.html"
ECHO ^<font size="2"^> >> "C:\Documents and Settings\bwimberly\Desktop\Email Signature maker\%%i\%%i.html"
ECHO ^<font face="Verdana"^> >> "C:\Documents and Settings\bwimberly\Desktop\Email Signature maker\%%i\%%i.html"
ECHO ^<b^>%%i^<^/b^>^|%%j^<br^/^>^<br ^/^>11800 Ridge Parkway, Suite #400^<br^/^> Broomfield, CO 80021 ^<br ^/^>303.938.9981 x%%k office ^| 800.880.7318 ^| 303.938.9986 fax^<^/font^>^<^/font^>^<^/P^> >> "C:\Documents and Settings\bwimberly\Desktop\Email Signature maker\%%i\%%i.html"
ECHO ^<table border="0"  width="50%%"^> >> "C:\Documents and Settings\bwimberly\Desktop\Email Signature maker\%%i\%%i.html"
ECHO ^<tr^> >> "C:\Documents and Settings\bwimberly\Desktop\Email Signature maker\%%i\%%i.html"
ECHO ^<td^>^<img src="http://www.2020taxresolution.com/images/2020TR_LOGO_medium.jpg"^/^>^<^/td^> >> "C:\Documents and Settings\bwimberly\Desktop\Email Signature maker\%%i\%%i.html"
ECHO ^<td^>^<br ^/^> >> "C:\Documents and Settings\bwimberly\Desktop\Email Signature maker\%%i\%%i.html"
ECHO ^<b^>email: ^<^/b^>^<a href="mailto:%%l@2020taxresolution.com"^>%%l@2020taxresolution.com^<^/a^>^<br ^/^> >> "C:\Documents and Settings\bwimberly\Desktop\Email Signature maker\%%i\%%i.html"
ECHO ^<b^>web: ^</b^>^<a href="http://www.2020taxresolution.com"^>2020taxaxresolution.com^</a^>^<br ^/^> >> "C:\Documents and Settings\bwimberly\Desktop\Email Signature maker\%%i\%%i.html"
ECHO ^<b^>blog: ^</b^>^<a href="http://www.2020taxresolution.com/taxtalk"^>2020taxresolution.com/taxtalk^<^/a^>^<br ^/^>^<br /^>^</td^> >> "C:\Documents and Settings\bwimberly\Desktop\Email Signature maker\%%i\%%i.html"
ECHO ^<^/tr^> >> "C:\Documents and Settings\bwimberly\Desktop\Email Signature maker\%%i\%%i.html"
ECHO ^<^/table^> >> "C:\Documents and Settings\bwimberly\Desktop\Email Signature maker\%%i\%%i.html"
ECHO ^<hr ^/^> >> "C:\Documents and Settings\bwimberly\Desktop\Email Signature maker\%%i\%%i.html"
rem ECHO ^<font color=\"#000000\"^>^<font face=\"Courier New\"^>^<font size=\"-1.4\"^>The information contained in this transmission is privileged and/or confidential information intended for the use of the individual or entity named above. If the reader of this message is not the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. Any tax information or written tax advice contained herein (including any attachments) is not intended to be and cannot be used by any taxpayer for the purpose of avoiding tax penalties that may be imposed on the taxpayer (the foregoing legend has been affixed pursuant to U.S. Treasury Regulations governing tax practice).^</font^>^</font^>^</font^>^</font^>^<br /^> >> "C:\Documents and Settings\bwimberly\Desktop\Email Signature maker\%%i\%%i.html"
ECHO ^<img src="http://www.2020taxresolution.com/images/environmental.jpg"/^> >> "C:\Documents and Settings\bwimberly\Desktop\Email Signature maker\%%i\%%i.html"
ECHO ^<^/head^> >> "C:\Documents and Settings\bwimberly\Desktop\Email Signature maker\%%i\%%i.html"
ECHO ^<^/body^> >> "C:\Documents and Settings\bwimberly\Desktop\Email Signature maker\%%i\%%i.html"
ECHO ^<^/html^> >> "C:\Documents and Settings\bwimberly\Desktop\Email Signature maker\%%i\%%i.html"


)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bwet5753Author Commented:
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.
0
KlitosCommented:
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.
0
bwet5753Author Commented:
My EE account expired and just learned of this.  Please accept the points and my apology.  
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.