Batch file to delete folder and its contents

batch file

I'm trying to write a batch file that would loop through a directory and delete any folder that is called "SAMPLE". Here is my code so far, any help would be greatly appreciated!

@echo off title WAIT !

Set "sourceDir=z:\FOLDERS"

IF NOT EXIST "%sourceDir%" (echo.Could not find %sourceDir%
&GoTo:done)

:: delete files For /F "Delims=" %%! in ('Dir "%sourceDir%\") do &
@rd "%%!" /s /q "%sourcedir%\ * \SAMPLE"

:done title,Done…….

echo.&pause>nul

Best Answer

  • for /d /r Z:\Folders %d in (SAMPLE) do if exist "%d" rd /s /q "%d"
    

    You'll need to use %%d in a batch file, but that's the basic form. I've used similar constructs for cleaning up Subversion working directories to remove the .svn folders.

    Edit: Fixed the syntax of the for command. I was working from memory and away from a Windows system at the time I posted. Sorry.

  • Related Question