Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 Oct 1 '12 at 12:21

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

"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
you should post that as an answer - it's ok to answer your own questions. – Mat Apr 10 '11 at 19:12

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
Better use "%%~B" instead of just "%%B", just to be on the safe side. – Ansgar Wiechers Oct 1 '12 at 12:41

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

You must log in to answer this question.