- Windows Server 2012 cmd.exe box
- I need to make a batch file, call it
get.bat, to process all directories listed indlist.txtwhich is in the same dir asget.bat. Directory names are one per line in dlist.txt. dlist.txt was created withdir /b DIV_* > dlist.txt. These DIV_ files are actually subdirectories to the dir where my batch file is run. - I cannot use a filespec in the FOR command because I need to edit
dlist.txtby hand after creating it. I don't want to process all dirs likeDIV_*, just some of them. - Creating
dlist.txtwithdir /w DIV_* > dlist.txtmeans all directory names have [] around them and I cannot use that. - There is one parameter to
get.batwhich is a base filename to contain all results form thefromxsfcommand. The .txt extension will be added by the batch file. - I need to do multiple commands to each dir in
dlist.txt, some of which are setting a temp filename called %tempoutfile% then used infromxsf. - I've read several pages I found on Google, including some from Superuser but the examples that might work for cmd.exe did not work for me. Maybe Windows 2012 Server is too old? I cannot change that, I have to work with what I've got.
fromxsfthat processes each directory indlist.txtrequires the directory as one parameter after the-cdand a temporary output file as another parameter which is %tempoutfile%.- We don't appear to have powershell or at least there is no path set to it.
Here's what I've tried using a FOR loop:
set destdir=c:\tempdir
set outfile=%destdir%\%1.txt
FOR /F %%f in ('type dlist.txt') DO ( echo "--------------------" echo Basedir=%%f should be directory set tempoutfile=../%%f.txt echo outfile=%destdir%\%outfile% echo tempoutfile=%tempoutfile% echo Processing dir %%f, to tempfile %tempfile% pause rem Below is the command to process the dir name in %%f to make a temp file in %tempoutfile% fromxsf %tempoutfile% -cd %%f +m -nbs -Rep -nofrills if exist "%destdir%" ( echo " " >> %outfile% echo "{DIVNAME=%i}" >> %outfile% rem Append %tempoutfile% to %outfile%. type %tempoutfile% >> %outfile% del %tempoutfile% )
)For the above the value of %%f is always the first line of dlist.txt. That is not what I want.
What I'm doing in the loop is outputting the processed contents of the directory into a temp file in %tempfile%, and then concatenating the contents of %tempfile% to %destdir%\%outfile%. mycommand requires the input directory %%f as a command line arg. I just can't redirect STDOUT and append the output to %destdir%\%outfile%.
Some alternate FOR loops I've tried:
FOR %%f in (dlist.txt) DO (. For this case%%fis always the second dir name indlist.txtand never changes.
Anyone know how to do this?
Thank you!
EDIT: Thanks. I figured it out using delayed expansion and another post I found using Google. The core of the loop is this, with filenames contained in the text file dlist.txt one filename per line. My problem was the missing "tokens=*" option. Also the !variable! notation must be used EVERY time in the loop. The loop below also has echo statements to show the values of variables.
setlocal EnableDelayedExpansion
set outfile=c:\temp\tempout.txt
del %outfile%
FOR /f "tokens=*" %%f in (dlist.txt) DO ( echo. echo -------------------- set indir=%%f echo Basediv indir=!indir! echo Processing division !indir! echo. >> %outfile% echo {DIVNAME=!indir!} >> %outfile% pause fromxsf %outfile% -cd !indir! +m -nbs -Rep -nofrills -a
)Key issues in my loop
- Use the
%%fvariable only once per loop and assign it toindir. From thereafter use the!indir!variable. - I removed the use of
%tempoutfile%because thefromxsfcommand allows me to append output from itself (using -a) IF the file exists, which it does in this case.