Search code examples
batch-filecmd

create file inside for-loop, batch


I have the following code:

for %%F in ("C:\test\*.csv") do 
(
    echo TEST1                                                 >test.txt
    echo TEST2                                                  >>test.txt

)
pause

For each csv-file it should create/overwrite a file and append some content to it. However it doesn't work, the file won't be created. When I do it without a loop it works:

   echo TEST1                                                 >test.txt
   echo TEST2                                                  >>test.txt

I am not very familiar which cmd commands, but what is here the problem. I need the %%F (filename of the csv) because I use it later in the for-loop


Solution

  • What do you mean by "work?"

    The opening parenthesis must be on the same line as the do

    for %%F in ("C:\test\*.csv") do (
    

    Since you are using > in the first echo, for each filename the file will be created anew.