Deleting files with spaces Batch script

April 11, 2010 at 16:24:25
Specs: Windows 7
Alright, so I am making a script that deletes any files found on the destination drive that is not located on the source drive. It is working minus if there are files with spaces. I know that usually you would put the file path in double quotes but it is in a for loop and is using a dir command to pull multiple file names. Now I also have read up on tokens=* which I figured would do what it is I am wanting... however it still is unable to find files with spaces here is a piece of the code that gives an example of the problem

<code>
FOR /F "tokens=*" %%j IN ('dir /b /a /s ^"%INPUT%^"') DO ( set REM="%%j"
set REM="!REM:~3!"
IF NOT EXIST "%DRIVE%!REM!" DEL /F /Q "%INPUT%\!REM!" 2>NUL
)
<code>
For clarification purposes %INPUT% is the destination drive %DRIVE% is the source and !REM! is used to remove the drive letter from the lists being pulled in.

can anyone help?


See More: Deleting files with spaces Batch script

Report •

#1
April 11, 2010 at 16:34:20
It's funny the second I posted this question I solved the issue... was adding too many quotes.

revised code in case this might help someone else.

<code>
FOR /F "tokens=*" %%j IN ('dir /b /a /s %INPUT%') DO ( set REM=%%j
set REM=!REM:~3!
IF NOT EXIST "%DRIVE%!REM!" DEL /F /Q "%INPUT%\!REM!" 2>NUL
)
</code>


Report •
Related Solutions


Ask Question