0

我有一个非常简单的批处理脚本,它试图FORFILES在我的 Windows Server 2012R2 服务器上的命令提示符窗口中使用该命令...

此批处理文件的目的是查看一堆目录(带有子目录)并删除 X 天或更早的文件。

@ECHO OFF
CLS
SETLOCAL EnableDelayedExpansion EnableExtensions

SET "MINDAYSOLD=9"
SET "TARGETPATH=E:\archives"

SET "PADDEDTIME=%TIME: =0%"
SET "DATESTAMP=%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%"
SET "LOGFILEPATH=Logs\%~n0-%DATESTAMP%.log"
SET "WORKINGDIR=%~dp0"

CALL :CreateDirectory "%WORKINGDIR%Logs"

ECHO Wiping files that are %MINDAYSOLD% or more days old...
ECHO Target Path: "%TARGETPATH%"
ECHO.

ECHO Searching folder: %TARGETPATH%
FOR /F "usebackq delims=" %%b IN (
        `forfiles /p "%TARGETPATH%" /S /M *.* /D -%MINDAYSOLD% /C "cmd /C ECHO @path" 2^>nul`
) DO (
        SET "filepath=%%~b"
        SET "filename=%%~nxb"
        ECHO   Found !filepath!
        ECHO   Deleting !filename!
        DEL /F /Q /A "!filepath!"
        REM TIMEOUT /NOBREAK /T 1
        IF EXIST "!filepath!" (
                ECHO   Error deleting file^^!
        ) ELSE (
                ECHO   Success.
        )
)
ECHO   Checking if folder %TARGETPATH% is empty...
SET "filesearch="
FOR /F "usebackq delims=" %%c IN (
        `DIR /B /A-D "%%~a" 2^>nul`
) DO (
        SET "filesearch=%%c"
)
IF {!filesearch!}=={} (
        ECHO   Folder is empty, deleting folder...
        RD /Q "%TARGETPATH%"
        REM TIMEOUT /NOBREAK /T 1
        IF EXIST "%TARGETPATH%" (
                ECHO   Error deleting folder^^!
        ) ELSE (
                ECHO   Success.
        )
) ELSE (
        ECHO   Folder is NOT empty^^!  Skipping deleting.
)
ECHO.

EXIT /B %ERRORLEVEL%


REM =================================================================
REM ! FUNCTIONS                                                     !
REM =================================================================

REM == Create directory =============================================
:CreateDirectory
IF NOT EXIST "%~1" (
        MKDIR "%~1"
)
EXIT /B 0
REM =================================================================

文件夹中有很多文件,我只是想返回 1 天前的文件。由于某种原因,该命令不断返回以下内容...

...
ERROR: The parameter is incorrect.
ERROR: The parameter is incorrect.
ERROR: The parameter is incorrect.
ERROR: The parameter is incorrect.
ERROR: The parameter is incorrect.
ERROR: The parameter is incorrect.
ERROR: The parameter is incorrect.
ERROR: The parameter is incorrect.

test
test
...

我试图弄清楚为什么我会遇到这些错误。有任何想法吗?

4

1 回答 1

0

我最终想出了以下脚本来解决我的问题。我用查看每个目录的 FOR 循环替换了 FORFILES,然后在每个目录中使用了 FORFILES。这种方式 FORFILES 不必挖掘子文件夹。以下脚本遍历每个文件夹(包括子文件夹),然后删除文件并包含大量详细信息。

@ECHO OFF
CLS
SETLOCAL EnableDelayedExpansion EnableExtensions

SET "MINDAYSOLD=9"
SET "TARGETPATH=E:\archives"

SET "PADDEDTIME=%TIME: =0%"
SET "DATESTAMP=%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%"
SET "LOGFILEPATH=Logs\%~n0-%DATESTAMP%.log"
SET "WORKINGDIR=%~dp0"

CALL :CreateDirectory "%WORKINGDIR%Logs"

ECHO Wiping files that are %MINDAYSOLD% or more days old...
ECHO Target Path: "%TARGETPATH%"
ECHO.

FOR /F "usebackq delims=" %%a IN (
    `DIR "%TARGETPATH%" /S /B /AD /O:N`
) DO (
    ECHO Searching folder: %%~a
    FOR /F "usebackq delims=" %%b IN (
        `forfiles /p "%%~a" /S /M *.* /D -%MINDAYSOLD% /C "cmd /C ECHO @path" 2^>nul`
    ) DO (
        SET "filepath=%%~b"
        SET "filename=%%~nxb"
        ECHO   Found !filepath!
        ECHO   Deleting !filename!
        DEL /F /Q /A "!filepath!"
        REM TIMEOUT /NOBREAK /T 1
        IF EXIST "!filepath!" (
            ECHO   Error deleting file^^!
        ) ELSE (
            ECHO   Success.
        )
    )
    ECHO   Checking if folder %%~a is empty...
    SET "filesearch="
    FOR /F "usebackq delims=" %%c IN (
        `DIR /B /A-D "%%~a" 2^>nul`
    ) DO (
        SET "filesearch=%%c"
    )
    IF {!filesearch!}=={} (
        ECHO   Folder is empty, deleting folder...
        RD /Q "%%~a"
        REM TIMEOUT /NOBREAK /T 1
        IF EXIST "%%~a" (
            ECHO   Error deleting folder^^!
        ) ELSE (
            ECHO   Success.
        )
    ) ELSE (
        ECHO   Folder is NOT empty^^!  Skipping deleting.
    )
    ECHO.
)

EXIT /B %ERRORLEVEL%


REM =================================================================
REM ! FUNCTIONS                                                     !
REM =================================================================

REM == Create directory =============================================
:CreateDirectory
IF NOT EXIST "%~1" (
    MKDIR "%~1"
)
EXIT /B 0
REM =================================================================
于 2019-02-04T18:00:40.153 回答