2

I'm using Windows 7.

I have a program called optAlg.exe that I'm calling within a for loop of a batch script, and I want to redirect the output of optAlg.exe to %outputFile%. When I run the following loop in a batch script, the output of optAlg.exe is written to the console.

for %%i in (data/5Node/*.dat) do @(
  set inputFile=%%i
  set outputFile="%inputFile%.out"
  optAlg %inputFile% 500 ^> %outputFile%
)

How can I get this to output to a file instead of the console?

EDIT: what I'm trying to do, is have it call

optAlg 1.dat 500 > 1.dat.out
optAlg 2.dat 500 > 2.dat.out
...
optAlg n.dat 500 > n.dat.out

where the .dat files I have are named 1.dat through n.dat

6
  • try >> for starters, also why do u need an escape character? I think thats whats preventing the content from be redirected. Commented Aug 29, 2013 at 2:51
  • Doesn't >> append instead of overwrite? Commented Aug 29, 2013 at 3:01
  • Yea, I thought you wanted to redirect multiple lines Commented Aug 29, 2013 at 3:08
  • Ok, when I use >> it writes every iteration of the loop to a single output file, instead of writing each one to a separate output file. As far as escaping, I was under the impression that I needed to. Commented Aug 29, 2013 at 3:09
  • Can you see my answer below? If it works accept, please. What you are seeing is that setting variables like set x=%var% within a loop does not work the way you think. It will be the last value var holds unless you tell it to loop normally ( the way you would expect ) via setlocal enabledelayedexpansion and use associated !var! syntax in place of the usual %var% syntax. dos is a tricky world - I had to write a lot of these scripts back in the day >.< Commented Aug 29, 2013 at 4:07

3 Answers 3

7

You need to use setlocal enabledelayedexpansion and associated !var! syntax.

setlocal enabledelayedexpansion
for %%i in (data/5Node/*.dat) do (
  set inputFile=%%i
  set outputFile="!inputFile!.out"
  optAlg "!inputFile!" 500 > !outputFile!
)
Sign up to request clarification or add additional context in comments.

4 Comments

I'm not sure why I don't what literally what I wrote. I want each output in the for loop to go to a separate file, but I don't want to append to the output files. E.g., if I run it, in the first iteration of the loop I want it to overwrite a previous 1.dat.out if one exists.
I understand overwriting, that makes sense. I just wasn't sure if you wanted each line of output from your executable to be output to your output file ( what I thought ) or if you literally wanted optAlg !inputFile! 500 > !outputFile! placed in the outputFile. Either way, no worries. Just wasn't sure which one you wanted, so I listed both. Will update answer to overwrite. The reason you are seeing it output to one file is because that is what the iterator is at the end of the loop, without setting setlocal enabledelayedexpansion. Does it work once you include this?
@raoulcousins, any luck?
Got it, it works. Thanks for your help. I especially like that you kept environment variables in. I like naming things. It helps me remember what I'm doing.
5

use >>"file.txt" and remove the ^

EDIT: You also need to use delayed expansion, or this should work:

for %%i in (data\5Node\*.dat) do optAlg "%%i" 500 >>"%%~ni.out"

Comments

2

Get rid of environment variables: you don't need them:

for %%i in (data/5Node/*.dat) do @optAlg %%i 500 > "%%~i.out"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.