Batch file to delete sub folder if exist and else batch file should create new folders

batch file

i need a Batch file to delete Sub folder in a folder if exist else batch file should create new sub folders in Mainfolder?

Best Answer

@echo off
set SF=PathToSubFolder
if not "%~1"=="" set SF=%~1

if exist "%SF%" (
    ECHO Sub folder found. Deleting . . . 
    RD /S /Q "%SF%"
) else (
    ECHO Sub folder not found. Creating . . .
    MKDIR "%SF%"
)
ECHO Completed.

If you save that as CheckSub.bat you can either edit the file to set the subdirectory, or you can run it from another batch file or command prompt window with the command: call CheckSub.bat "C:\Path\To\Sub\Folder" (if you call it that way it overwrites whatever you set in the file

Related Question