Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am currently having trouble trying to delete some files in a cmd script

I have the following:

for /d %%A in (C:\Users\*) do for /d %%B in (%%A\test\*) do echo %%B

Although I need to delete the output files, not list them

I have tried:

for /d %%A in (C:\Users\*) do for /d %%B in (%%A\test\*) do del %%B

But it didnt work

share|improve this question

migrated from stackoverflow.com Oct 1 '12 at 12:21

This question came from our site for professional and enthusiast programmers.

1  
"did not work" is not helpful. Did it give you an error message? Did it not delete the right files? What was the output? –  Mat Apr 10 '11 at 18:59
    
Sorry, thanks for you reply, I have just found out that its because of the spaces.. –  Peter Apr 10 '11 at 19:07
    
Found out, just added quotes around %%B e.g. for /d %%A in (C:\Users*) do for /d %%B in (%%A\test*) do del "%%B" –  Peter Apr 10 '11 at 19:10
2  
you should post that as an answer - it's ok to answer your own questions. –  Mat Apr 10 '11 at 19:12

2 Answers 2

In addtion to Max's answer you can also use parenthesis to make the batch file easier to read. You should also use lowercase variable names.

@echo off
setlocal EnableDelayedExpansion  

for /d %%a in (C:\Users\*) do (
    rem echo "directory: %%a"
    for /f %%b in ("%%a\test\*") do (
        echo "%%b"
        del "%%b"
    )
)
share|improve this answer

Add quotes around %%B e.g. for /d %%A in (C:\Users*) do for /d %%B in %%A\test* do del "%%B"

share|improve this answer
1  
Better use "%%~B" instead of just "%%B", just to be on the safe side. –  Ansgar Wiechers Oct 1 '12 at 12:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.