Hi All,
I'm trying to write a small batch script that will delete files old files from a directory. The first parameter is the directory for the script to look into, and the second is the number of files to preserve.
rem @ECHO OFF
rem %1 is the path in which to look for the files
rem %2 is the number of recent files to preserve
if [%1] EQU [] (
echo ERROR: Missing Required Paramater directory path.
goto :eof
)
if [%2] EQU [] (
echo ERROR: Missing Required Paramater, number of files to preserve
goto :eof
)
if %2 LSS 0 (
echo ERROR: Number of files to preserve provided was negative
goto :eof
)
set FolderPath=%1
set SafeNumber=%2
cd %FolderPath%
for /f %%f in ('dir /O-D /A-D /B') do call :delete %%f
goto :eof
:delete
if %SafeNumber% LEQ 0 (
del %1
) else (
set /a SafeNumber-=1
)
goto :eof
:eof
Essentially what I have here is a dir that outputs a list of filenames ordered from newest to oldest. Depending on what SafeNumber is, it will skip the first few files and then procede with deletion once SafeNumber <= 0.
The problem I'm having right now, is if the filename is "Test File.txt" (as in contains a space. "Test" is passed into the delete as %1, rather then "Test File.txt".
Any ideas on how to get my script working, or perhaps someone has a better written solution?