[Script-Batch] Delete specified sub-folder under specified root folder

This batch script is originally written for deleting .svn folder from a check out from subversion.

 :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Batch Script for delete folders from root and its sub.
:: Author: #722
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@ECHO OFF
@SETLOCAL

@IF NOT "%~3" == "" (
  @CALL :ShowHelp "%~0"
  @GOTO :END
)

@IF "%~1" == "" (
  @CALL :ShowHelp "%~0"
  @ECHO. & @PAUSE
  @GOTO :END
) ELSE (
  @SET TargetRootFolder=%~1
)

@IF NOT EXIST "%TargetRootFolder%" (
  @ECHO [ERR] Target folder "%TargetRootFolder%" is not existed.
  @CALL :ShowHelp "%~0"
  @GOTO :END
)

@IF "%~2" == "" (
  @SET Pattern=*.svn
) ELSE (
  @SET Pattern=%~2
)

@SET SystemVerifyCode=Yes
@ECHO !!Important!!
@ECHO Are you sure you want to delete all "%Pattern%" folders inside "%TargetRootFolder%" ?
@ECHO   Verification Code ^("Yes"^), CaseSensitive:
@SET /P UserVerifyCode=
@SET /A Count=0
@IF "%UserVerifyCode%" == "%SystemVerifyCode%" (
  FOR /F "USEBACKQ TOKENS=*" %%D IN (`"DIR /AD/B/S "%TargetRootFolder%\%Pattern%"| SORT /R"`) DO (@ECHO  Deleting "%%D" & @RD /S /Q "%%D" & @SET /A Count+=1)
) ELSE (
  @ECHO  ^> You have given up to delete "%Pattern%" folders inside "%TargetRootFolder%".
)
@ECHO  ^> Total %Count% "%Pattern%" folders have been deleted.
@GOTO :END

:ShowHelp
  @ECHO.
  @ECHO Delete all folders and sub-folders with specified pattern in specified root folder.
  @ECHO.
  @ECHO %~nx1  [root] [pattern]
  @ECHO   [root]     specifies the root folder.
  @ECHO   [pattern]  specifies the pattern of folder name to be deleted. ^(default^:*.svn^)
  @ECHO  Example:
  @ECHO   %~nx1 "%CD%" "*.svn"
@GOTO :EOF

:END
@ENDLOCAL
@ECHO ON

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章