0
  • Windows Server 2012 cmd.exe box
  • I need to make a batch file, call it get.bat, to process all directories listed in dlist.txt which is in the same dir as get.bat. Directory names are one per line in dlist.txt. dlist.txt was created with dir /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.txt by hand after creating it. I don't want to process all dirs like DIV_*, just some of them.
  • Creating dlist.txt with dir /w DIV_* > dlist.txt means all directory names have [] around them and I cannot use that.
  • There is one parameter to get.bat which is a base filename to contain all results form the fromxsf command. 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 in fromxsf.
  • 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.
  • fromxsf that processes each directory in dlist.txt requires the directory as one parameter after the -cd and 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 %%f is always the second dir name in dlist.txt and 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

  1. Use the %%f variable only once per loop and assign it to indir. From thereafter use the !indir! variable.
  2. I removed the use of %tempoutfile% because the fromxsf command allows me to append output from itself (using -a) IF the file exists, which it does in this case.
  • 1
    1. Where does %tempoutfile% get assigned a value? 2. Regarding setting/expanding variables inside a FOR or IF multiline block, you need to study "delayed expansion" and the exclamation-mark !variable! notation. – Michael Harvey Sep 6 '18 at 15:57
  • @MichaelHarvey: there was a typo. %tempoutfile% is assigned near the top of the FOR loop. – Bulrush Sep 6 '18 at 16:34
  • Fix the delayed expansion thing. – Michael Harvey Sep 6 '18 at 16:35

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.