我有一个非常简单的批处理脚本,它试图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
...
我试图弄清楚为什么我会遇到这些错误。有任何想法吗?