rr.bat

批处理实现递归删除指定路径下的指定目录或文件。

@echo off
rem rr.bat(Recursive Remover).
rem t0nsha@20090730_2313<liaodunxia#gmail.com>
SETLOCAL EnableDelayedExpansion
SET WRK_DIR=%1
SET RM_TYPE=%2
SET RM_NAME=%3
IF "%RM_NAME%"=="" GOTO :USAGE
IF "%RM_TYPE%" == "-d" (
    SET RM_ACTN=RD /S/Q
) ELSE IF "%RM_TYPE%" == "-f" (
    SET RM_ACTN=DEL
) ELSE (
    GOTO :USAGE
)
FOR /R %WRK_DIR% %%R IN (.) DO (
    SET RM_PATH=%%R
rem drop the last dot.  
    SET RM_PATH=!RM_PATH:~0,-1!
    IF %RM_TYPE% == -d (
        FOR /F "usebackq delims==" %%D IN (`dir /a:d/b %%R`) DO (
            IF "%%D" == "%RM_NAME%" (
                ECHO %RM_ACTN% "!RM_PATH!%%D"
                %RM_ACTN% "!RM_PATH!%%D"
            )
        )
    ) ELSE IF %RM_TYPE% == -f (
        FOR /F "usebackq delims==" %%F IN (`dir /a:-d/b %%R`) DO (
            IF "%%F" == "%RM_NAME%" (
                ECHO %RM_ACTN% "!RM_PATH!%%F"
                %RM_ACTN% "!RM_PATH!%%F"
            )
        )
    )
)
EXIT /B 0
:USAGE
ECHO Recursive remove directory or file.
ECHO rr path {-d directory ^| -f filename}
ECHO.
ECHO    path           Specifies the search path.
ECHO    -d directory   Remove directory.
ECHO    -f filename    Remove file.


原文链接: http://blog.csdn.net/t0nsha/article/details/4395779

你可能感兴趣的:(rr.bat)