I am trying to create a sources txt file to pass to the javac compiler. In the text file, I need to have the paths of all the .java files in the subdirectories surrounded by speech marks, in case the folder names contain spaces, and any \ for the path need to be \\ because \ is escaped. With the text file I would run javac -d \classes @sources.txt
. This works and I have tested it with paths I have written into sources.txt. However, I have been experimenting with trying to get the command line to create this file for me as it would be a hassle if there were more files and I had to write every single path.
So far I have tried running for /f "delims=" %A in ('dir /s /B *.java') do @echo "%A" >>sources.txt
. The dir command gets all the paths and the echo surrounds the commands with speech marks but when I try running the following command instead to switch the \ to \\ it doesn't work:
for /f "delims=" %A in ('dir /s /B *.java') do @echo "%A:\=\\" >>sources.txt
This returns the "<thepath>:\=\\". What am I doing wrong? FYI: this is directly in the cmd line and not ran from a file. Thanks.
CodePudding user response:
As this appears to be a Command Prompt, (cmd) question, and not a batch-file, (as shown in the other current answer), perhaps this will achieve what you intended:
(For /F "Delims=" %G In ('Dir "*.java" /A:-D /B /O:N /S 2^>NUL') Do @Set "}=%G" & %SystemRoot%\System32\cmd.exe /D /V /C "Echo !}:\=\\!") 1>"sources.txt"
CodePudding user response:
The substitution needs to be done on an environment variable, not on the loop variable.
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "delims=" %%A IN ('DIR /S /B *.bat') DO (SET "AA=%%~fA" & ECHO "!AA:\=\\!")