How do i delete multiple files from a MS DOS batch file using a wildcard when the path includes spaces?

10,389

Solution 1

The function needs to loop over each line individually, so you need to quote the variable in the FOR loop, tokenize it on the semicolons, rinse and repeat.

@ECHO OFF
SET OLD_ZIPS=^
C:\tmp with spaces\Test_v*.txt;^
C:\tmp\Test_a.txt

ECHO Deleting the following files: %OLD_ZIPS%

:deleteFiles
for /f "tokens=1* delims=;" %%A in ("%OLD_ZIPS%") do (
    ECHO  Checking existance of "%%A"
    IF EXIST "%%A" (
        ECHO  Deleting "%%A"
        DEL /Q "%%A"
    )
    set OLD_ZIPS=%%B
)
if not "%OLD_ZIPS%" == "" goto :deleteFiles

PAUSE

Solution 2

seems you are trying to over complicate things.

for %a in ("C:\Tmp with spaces\Test_v*.txt" "C:\Tmp\Test_a.txt") do del /q "%a"

does what you want, and can be typed from the command line. change %a to %%a if you want to do it in a batch file

Share:
10,389
Jon Cage
Author by

Jon Cage

A Computer Systems Engineer based in the UK

Updated on July 18, 2022

Comments

  • Jon Cage
    Jon Cage almost 2 years

    Say I have a directory with the following files in it:

    Test.bat Test_a.txt Test_b.txt Test_v1.zip Test_v2.zip Test_v3.zip

    I want to delete all Test_v*.zip quietly (no error messages logged to screen). I can achieve this with the following script:

    @ECHO OFF
    SET OLD_ZIPS=^
    C:\Tmp\Test_v*.txt;^
    C:\Tmp\Test_a.txt
    
    ECHO Deleting the following files: %OLD_ZIPS%
    
    FOR %%Y IN (%OLD_ZIPS%) DO (
    IF EXIST %%Y (
    ECHO  Deleting %%Y
    DEL /Q %%Y)
    )
    
    PAUSE
    

    This works fine:

    Deleting the following files: C:\Tmp\Test_v*.txt;C:\Tmp\Test_a.txt
    Deleting "C:\Tmp\Test_v1.txt"
    Deleting "C:\Tmp\Test_v2.txt"
    Deleting "C:\Tmp\Test_a.txt"
    Press any key to continue . . .
    

    Unless of course the file paths contain spaces. So in the example above, if I change C:\Tmp\Test_v*.txt to C:\Tmp with spaces\Test_v*.txt I get:

    Deleting the following files: C:\Tmp test\Test_v*.txt;C:\Tmp test\Test_a.txt
    Press any key to continue . . .
    

    How can I stop it baulking at the spaces?

    Edit - I've tried spaces as per Alex K's answer (plus a little more debug) and it looks like perhaps the for loop isn't splitting things up as I expect:

    @ECHO OFF
    SET OLD_ZIPS=^
    C:\Tmp test\Test_v*.txt;^
    C:\Tmp test\Test_a.txt
    
    ECHO Deleting the following files: %OLD_ZIPS%
    
    FOR %%Y IN (%OLD_ZIPS%) DO (
    ECHO  Checking existance of "%%Y"
    IF EXIST "%%Y" (
    ECHO  Deleting "%%Y"
    DEL /Q "%%Y")
    )
    
    PAUSE
    

    ..gives me:

    Deleting the following files: C:\Tmp test\Test_v*.txt;C:\Tmp test\Test_a.txt
     Checking existance of "C:\Tmp"
     Checking existance of "C:\Tmp"
     Checking existance of "test\Test_a.txt"
    
  • Jon Cage
    Jon Cage over 11 years
    Nice work Kevin; that worked a treat! I tried the tokens / delims bit but I was using tokens=* and didn't get the right answer..
  • Nanook
    Nanook over 11 years
    It does work. I mistakingly pasted your paths without spaces back in. The paths on my machine when I tested had spaces. It's the quotes on the paths that make the difference with spaces. It doesn't seem to work with ^ directly after the ZIPS= though. I've updated the paths now.
  • Jon Cage
    Jon Cage over 11 years
    I get: Deleting the following files: "C:\Tmp test\Test_v*.txt";"C:\Tmp test\Test_a.txt"; Deleting C:\Tmp test\Test_v1.txt The system cannot find the file specified. Deleting C:\Tmp test\Test_v2.txt The system cannot find the file specified. Deleting "C:\Tmp test\Test_a.txt" and neither Test_v1.txt nor Test_v2.txt get deleted. I'm running on windows 7 if that makes a difference?