You need to add a delims
phrase:
FOR /F "delims=" %%G IN (
And definitely change the DEL
to
del "%%G"
-if you don't, del will attempt to delete the list of files provide in the argument, with the usual spaces and commas as separators.
Question
I have the following batch file snippet:
FOR /F %%G IN (
'dir /b /s ApprovalClassLibrary.dll Constants.dll CustomControls.dll NetSpell.SpellChecker.dll'
) do (
ECHO Deleting "%%G"
del %%G
)
which I am trying to use to delete any DLL files listed.
The dir command correctly finds the files as follows:
C:\Users\brian\Documents\Visual Studio 2012\Projects\HRActions 10.3.2\ApprovalClassLibrary\bin\Constants.dll
C:\Users\brian\Documents\Visual Studio 2012\Projects\HRActions 10.3.2\ApprovalClassLibrary\SetupFiles\publish\bin\Constants.dll
C:\Users\brian\Documents\Visual Studio 2012\Projects\HRActions 10.3.2\Constants\bin\Constants.dll
C:\Users\brian\Documents\Visual Studio 2012\Projects\HRActions 10.3.2\Constants\obj\x86\Release\Constants.dll
C:\Users\brian\Documents\Visual Studio 2012\Projects\HRActions 10.3.2\CustomControls\bin\CustomControls.dll
...
But when I run the script I get the following
Deleting "C:\Users\brian\Documents\Visual"
Could Not Find C:\Users\brian\Documents\Visual
Deleting "C:\Users\brian\Documents\Visual"
Could Not Find C:\Users\brian\Documents\Visual
Deleting "C:\Users\brian\Documents\Visual"
Could Not Find C:\Users\brian\Documents\Visual
Deleting "C:\Users\brian\Documents\Visual"
Could Not Find C:\Users\brian\Documents\Visual
...
It appears that the path is breaking due to the space in the directory. How can I get this command to properly expand the path so I can delete the files?
Solution
You need to add a delims
phrase:
FOR /F "delims=" %%G IN (
And definitely change the DEL
to
del "%%G"
-if you don't, del will attempt to delete the list of files provide in the argument, with the usual spaces and commas as separators.
OTHER TIPS
You need to add "Delims="
to your for statement, otherwise it will break on every space by default.