Help needed creating batch...

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
The Dirty Birch
Posts: 3
Joined: 02 Dec 2012 09:13

Help needed creating batch...

#1 Post by The Dirty Birch » 02 Dec 2012 10:29

It has been years since I have done any batch stuff and it was pretty simplistic stuff when I did it. I used to have a friend to help me with these type things, but both of us have moved and I no longer have contact with him. I am hoping one of the pundits here could help me out?

I have a txt file named user.txt. In it each line contains a username. I would like to create a new txt file inputting the username inside a line of text and then have the new file be named with that same username variable as the file name.

Example:
user.txt contains:
Cletus
Festus
Clem

I need a new .txt created for each user in user.txt. Contained in the new file would be a common text string with the user name at the end such as:

"This is the text file for {username}."

In this example the end results would be that I end up with 3 new text files Cletus.txt, Festus.txt and Clem.txt that would contain the text string above in Cletus' case "This is the text file for Cletus." and so for each user.

My user name list actual contains about 325 users. I started to cut, paste and save each but it is very tedious and leaves room for error.

Thank you in advance for any help anyone could give me!

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Help needed creating batch...

#2 Post by Ed Dyreen » 02 Dec 2012 11:31

Code: Select all

@echo off &setlocal enableDelayedExpansion

set "$iFile=myNamesIndex.TXT"

for /f "useback" %%? in (

       "!$iFile!"

) do   >"%%~?.TXT" (
       ::
       echo."This is the text file for {%%~?}."
)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help needed creating batch...

#3 Post by foxidrive » 02 Dec 2012 11:50

I dislike the %%? construct and I think Ed's "useback" will generate an error message

This should be equivalent. (also untested):

Code: Select all

@echo off
for /f "delims=" %%a in (' type "myNamesIndex.txt" ') do (
>>"%%a.txt"  echo."This is the text file for {%%a}."
)

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Help needed creating batch...

#4 Post by Ed Dyreen » 02 Dec 2012 11:52

foxidrive wrote:I think Ed's "useback" will generate an error message
useback is an abbrevation of usebackq :wink:

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help needed creating batch...

#5 Post by foxidrive » 02 Dec 2012 11:56

Ed Dyreen wrote:
foxidrive wrote:I think Ed's "useback" will generate an error message
useback is an abbrevation of usebackq :wink:


So it's an undocumented use... yeah?

Your code works too.

The Dirty Birch
Posts: 3
Joined: 02 Dec 2012 09:13

Re: Help needed creating batch...

#6 Post by The Dirty Birch » 02 Dec 2012 17:03

Thanks guys! Thank you for the quick replies! The code works fine in the example I gave. I am running into problem in my actual application due to some of the punctuation i am using I think. What I am actually trying to do is stick my variable in some html code to display a logo and link. Here is my actual application the string I am trying to get the variable into:

<a href="http://www.mosesorganic.org/ou.html?referrer=%%~?">
<img src="http://www.mosesorganic.org/images/logos/OrganicUniversityBadge.gif" width="270" height="162" alt="MOSES Organic Farming Conference" border="0" />
</a>

Is it the symbols < and > that are causing me grief? Is there any way to make this happen without all the html punctuation getting in the way?

Thanks again!
Mark

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help needed creating batch...

#7 Post by foxidrive » 02 Dec 2012 17:35

Escape the symbols below with the escape character and the symbol. The escape character is ^

^<

^>

^|

^&

etc

in a special case % becomes %% (but NOT in the %%a for in do variable)

The Dirty Birch
Posts: 3
Joined: 02 Dec 2012 09:13

Re: Help needed creating batch...

#8 Post by The Dirty Birch » 02 Dec 2012 19:03

Thank you guys so very much! With your help I was able to get it. Actual you guys got it for me.

Thanks again!
Mark

Post Reply