Batch File to Delete Sub Folder If Exist and Else Batch File Should Create New Folders?

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

2

1 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Elena Rostova

Elena Rostova

Lead Health, Wellness & Medical Journalist

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.

Share this article
Twitter Facebook Pinterest