Home > Net >  CMD batch file to Loop over Directory and Write File Paths to TXT
CMD batch file to Loop over Directory and Write File Paths to TXT

Time:01-07

I have this batch script that initiates a an empty quote variable and then it loops a target directory and appends the paths of the batch files to the variable to then output to a file.

I have been debugging the script and it says

do(call was unexpected at this time

The script is here:

set a=
for /R C:\Target\Directory %%i in (*.bat) do( call set a="%a%;%%i")
echo %a% > output.txt

How can I modify the script to print the directories of the bat files within the target directory using only a batch file not PowerShell.

Edit

I ran the command between a space between the do and ( now the output file is not being created.

Edit 2

The output file is not being generated in the same folder should I add ~dp0 to the beginning of the output file to make it appear in the same folder? Also when I run the command as an administrator, another issue also arose that it looks as follows:
" " " " " " " ";C:\Target\Directory\mockup\file1.bat";C:Target\Directory\mockup2\mockup\file2.bat"

Does that have to do with the way a was initialized I believed it would come out more as such?

"C:\Target\Directory\mockup\file1.bat";"C:Target\Directory\mockup2\mockup\file2.bat"

CodePudding user response:

I would not bother with the environment variable, just create a blank file and append to it.

echo "" > c:\temp\output.txt
for /R C:\ %i in (*.bat) do echo %i >> c:\temp\output.txt
  • Related