I have a very simple batch script that is trying to utilize the FORFILES
command in command prompt windows on my Windows Server 2012R2 server...
The purpose of this batch file is to look through a bunch of directories (with sub directories) and delete files that are X days old or older.
@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 =================================================================
There are many files in the folder and I'm just trying to get 1 day old files returned. For some reason the command keeps returning the following...
...
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
...
I'm trying to figure out why in the world I'm getting those errors. Any ideas?
I ended up coming up with the following script that fixed my issue. I replaced the FORFILES with a FOR loop looking through each directory, then used FORFILES in each directory. This way FORFILES did not have to dig through subfolders. The following script goes through each folder (including sub folders) and then deletes files and has a lot of verbose information.
@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 =================================================================