Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have created a script which looks at a folder, then creates a new output file with information as it goes

I have an output folder, within this folder are folders (which have the filename of the timestamp of when created ie 201303051158), within these folders are several files containing information that I want to load into a database.

but I want to add some more information into these data files

rem list files in folders in ts_users and output to a file 3 values, folder, filename
rem without type and full filename

del c:\ts_users\files_list.txt c:\ts_users\filelocations.txt c:\ts_users\folders_list.txt

cd c:\ts_users\output

for /r %%a in (*) do echo %%a >>c:\ts_users\files_list.txt

FOR /F "tokens=4,5 delims=\" %%a in (c:\ts_users\files_list.txt) do @echo %%a %%b %%~nb >>c:\ts_users\filelocations.txt

the output file looks like this

201303051158 eu1ptsw001.out eu1ptsw001

This information I would like to add to each line in the data files.

A line in the data file looks like this


mehmet.togrul rdp-tcp#0 2 Active 41 05/03/2013 05:05

thabo.lesame rdp-tcp#23 3 Active 6 05/03/2013 05:55

but I want it to look like this (exclude 1st line)

201303051158 mehmet.togrul rdp-tcp#0 2 Active 41 05/03/2013 05:05 eu1ptsw001

201303051158 thabo.lesame rdp-tcp#23 3 Active 6 05/03/2013 05:55 eu1ptsw001

is using a batch file the best way to do this?

share|improve this question
Look into using VBScripts. Much more flexible than batch files. – MDT Guy Mar 19 '13 at 14:08

How about

setlocal enabledelayedexpansion

for /r %%a in (*) do echo %%a >>c:\ts_users\files_list.txt

FOR /F "tokens=4,5 delims=\" %%a in (c:\ts_users\files_list.txt) do (
     set myOutput=%%a %%b %%~nb
     ::or whatever %%X you want

Then on the line you are using to echo all that info to the log files:

echo %whatever_you_are_already_using% %myOutput% >> logfile.extension

I'm not sure if enabledelayedexpansion is actually needed, but I think for loops have their own scope on batch, so it's the equivalent of using static var=. Issue set /? to be sure, I don't have Windows here and I can't remember for sure. You may need to use !myOutput! instead of %myoutput%.

As for using batch to do this... If you're familiar with batch scripting and got this far... sure, stick with it, otherwise I'd use something a bit more dynamic and less complicated like Python, since manipulating data can be a bit over complicated with batch. I'm not sure if it supports regex, but if it does it'd be a good idea using it instead of all those crazy for parameters.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .