1

I'm a noob at batch programming so excuse me for the simple question.

I have a folder called FSC in C:\TTG\FSC, with a thousand and thousand of text files called like 1_A.txt 2_A.txt etc. I would like to run a programm and have its output in another file renaming the original one ( for ex. 1_A.txt will have an output file called 1_A_out.txt ). I have already tested to run the programm on 1 file and it works (simply writing on the command line tag-french filename).

So how can I create the for loop for parsing each file on the folder and create the output files?

I've tried this but it doesn't work

for /F %%i in ('dir /b "c:\treetagger\FSCB1\"') do
tag-french %%i > %%i_out

I can do it in ubuntu writing this :

for l in /home/sp/Desktop/FSCM5/*
do
  echo $l
  filename=$(basename "$l")
  extension="${filename##*.}"
  filename="${filename%.*}"
  filename=($(echo "/home/sp/Desktop/FSCM5TTG/"$filename"_"ttg"_."$extension))
  echo $filename
  sh /home/sp/Desktop/TTG/cmd/tree-tagger-french $l > $filename
done

But I would like to learn how to do this in windows too

KeyPi
  • 516
  • 5
  • 20
  • That's what the `FOR /F` loop is for - enumerate all (matching) files in a folder, possibly recursively, and run a command on them. Type `FOR /?` to get the command syntax. – user1016274 Jul 27 '16 at 14:33
  • I've tried to do this but it doesn't work : for /F %%i in ('dir /b "c:\treetagger\FSCB1\"') do tag-french %%i > %%i_out – KeyPi Jul 27 '16 at 14:37
  • What if there are already files named `*_out.txt` in the directory? – aschipfl Jul 27 '16 at 15:14
  • in theory I think that " %%i_out " and in particular %%i is the name of the file so if I have three files 1_a.txt, 2_a.txt, 3_a.txt should be renamed as 1_a_out.txt 2_a_out.txt etc. – KeyPi Jul 28 '16 at 09:32

1 Answers1

0

I've tested this with success:

for /F "delims=" %%i in ('dir /b "c:\treetagger\FSCB1\*.*"') do tag-french "%%i" > "%%i_out"

I had to use the delims= option to preserve filenames even if they contained spaces.
edit: options need to be placed before the loop variable.

user1016274
  • 4,071
  • 1
  • 23
  • 19
  • C:\TreeTagger>for /F %i "delims=" in ('dir /b "c:\treetagger\FSCB1\*.*"') do tag -french "%i" > "%i_out" "delims=" was unexpected at this time. C:\TreeTagger>for /F %%i "delims=" in ('dir /b "c:\treetagger\FSCB1\*.*"') do ta g-french "%%i" > "%%i_out" %%i was unexpected at this time. – KeyPi Jul 28 '16 at 09:27
  • sorry, pasted a bug back in: options like "delims" need to be put before the loop variable. Code edited. – user1016274 Jul 28 '16 at 11:09
  • it doesn't work here : the files created are empty :\ – KeyPi Jul 29 '16 at 08:05
  • Can we meet in chat? A one-liner shouldn't take that long to debug. – user1016274 Jul 29 '16 at 10:45
  • Ok. Can you explain me how? – KeyPi Aug 04 '16 at 13:20
  • Join http://chat.stackoverflow.com/rooms/info/120136/for-loop-with-output-redirection?tab=general . Please post your code there. – user1016274 Aug 04 '16 at 14:28