我有一个非常简单的批处理脚本,正在尝试在Windows Server 2012R2服务器上的命令提示符窗口中使用FORFILES
命令...
此批处理文件的目的是浏览一堆目录(包含子目录)并删除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
...
我试图弄清楚为什么我会遇到这些错误。有什么想法吗?
答案 0 :(得分:0)
我最终想到了以下脚本来修复我的问题。我用遍历每个目录的FOR循环替换了FORFILES,然后在每个目录中使用了FORFILES。这样,FILES不必挖掘子文件夹。下面的脚本遍历每个文件夹(包括子文件夹),然后删除文件并包含许多详细信息。
@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 =================================================================