[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

For loop in Batch File

Posted on 2010-04-01
12
Medium Priority
?
860 Views
Last Modified: 2012-05-09
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
0
Comment
Question by:bwet5753
  • 6
  • 5
12 Comments
 
LVL 7

Expert Comment

by:vikas_madhusudana
ID: 29360288
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
 

Author Comment

by:bwet5753
ID: 29360976
No really sure I follow, and also did you post the modifications?  Can you tell me what you changed?
0
 
LVL 7

Expert Comment

by:vikas_madhusudana
ID: 29361972
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:bwet5753
ID: 29362716
That still only created one file.  Do you want me to post both files (batchfile and text file)?
0
 
LVL 7

Expert Comment

by:vikas_madhusudana
ID: 29364878
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
 

Author Comment

by:bwet5753
ID: 29365110
How can I create one file per user?  
0
 
LVL 7

Expert Comment

by:vikas_madhusudana
ID: 29365401
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
 

Author Comment

by:bwet5753
ID: 29376065
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
 
LVL 7

Accepted Solution

by:
vikas_madhusudana earned 2000 total points
ID: 29408101
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
 

Author Comment

by:bwet5753
ID: 29412823
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
 
LVL 1

Expert Comment

by:Klitos
ID: 29462390
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
 

Author Closing Comment

by:bwet5753
ID: 31709972
My EE account expired and just learned of this.  Please accept the points and my apology.  
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…
Suggested Courses
Course of the Month9 days, 16 hours left to enroll

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question