Hi Gary,
After a little break from that task, I ended up with basically the
approach you recommended. It includes logging and some rudimentary
error checking of parms.
Thanks all for the varied suggestions.
-mark
_____________
DelFiles.bat
------------
@ECHO ON
@REM _______________________________________________________
@REM Delete files in given path that are older than x days.
@REM _______________________________________________________
@REM Parm 1: Number of days equal or older than to delete.
@REM Parm 2: Path to look in.
@REM Parm 3: Should be either TEST or DELETE.
@REM If Parm 3 is not DELETE, only the log entries are output.
The actual delete is not done.
@REM Parm 4: Specifies whether to recurse subdirectories. Allowable
values: RECURSE or NORECURSE.
@REM Notes: Folders are not inspected for age, they are skipped.
@REM - The path (parm 2) should be enclosed in double quotes, in
case there are embedded blanks.
@REM _______________________________________________________
SET DELFILESLOG=DelFiles.log
IF %1x==x GOTO ERROR1
IF %2x==x GOTO ERROR2
SET RECURSE=
IF /I %4.==RECURSE. SET RECURSE=/S
echo ***___ DELFILES started: %date% %time%: %3 mode: %2 %4 ___*** >>
%DELFILESLOG%
@REM This run is to output to the log.
forfiles /P %2 %RECURSE% /D -%1 /C "cmd /c if @isdir==FALSE echo @fdate
0x09 @path is at least %1 days old" >> %DELFILESLOG%
echo ***___ DELFILES ended: %date% %time%: %3 mode: %2 %4 ___*** >>
%DELFILESLOG%
echo . >> %DELFILESLOG%
IF /I %3. NEQ DELETE. GOTO END
@REM This run is to do the actual delete.
forfiles /P %2 %RECURSE% /D -%1 /C "cmd /c if @isdir==FALSE DEL @path"
GOTO END
::__________________________________________________
:ERROR1
PAUSE Number of days old (*GE) is mandatory.
GOTO END
:ERROR2
PAUSE Path to look in is mandatory.
GOTO END
:END
EXIT
__________
Here's the driver batch file:
DelFilesDriver.bat
------------------
@Rem _______________________________________________________
@Rem Delete files in given path that are older than x days - driver.
@Rem _______________________________________________________
@REM Parm 1: Number of days equal or older than to delete.
@REM Parm 2: Path to look in.
@REM Parm 3: Should be either TEST or DELETE.
@REM If Parm 3 is not DELETE, only the log entries are output.
The actual delete is not done.
@REM Parm 4: Specifies whether to recurse subdirectories. Allowable
values: RECURSE or NORECURSE.
@Rem _______________________________________________________
start /B /WAIT DelFiles 183 "\MyDir\Dir1\ARCHIVE" DELETE NORECURSE
start /B /WAIT DelFiles 060 "\MyDir\Dir2\ARCHIVE" DELETE NORECURSE
start /B /WAIT DelFiles 030 "\MyDir\Dir3\ARCHIVE" DELETE NORECURSE
On 9/30/2018 4:28 PM, Gary Kuznitz wrote:
You can also use this:
forfiles /p "C:\what\ever" /s /m *.* /D -<number of
days> /C "cmd /c del @path"
@PATH is the full path, including name. Note that if
you want files OLDER than 10 days, you need to specify
-d "-10". -ve means "older than", +ve means "newer
than". You can also specify DDMMYY or -DDMMYY format
as the parameter to -d.
If you are using Windows 7, you can separate @PATH
with just the path, and @FILE with just the file name.
Then you can use @PATH\@FILE for the del command.
This syntax works on Win Server 2008: forfiles /P
"C:\Mysql_backup" /S /M *.sql /D -30 /C "cmd /c del
@PATH"
Enjoy,
Gary
On 29 Sep 2018 at 21:41, mlazarus (mlazarus
<pctech@xxxxxxxxxxxx>) commented about [PCTECH]
Recommendations for a Windows file delet:
I am looking for recommendations for a file delete utility to be run
on a Windows server. The delete criteria would be the age of the files
within a list of folders. Each folder might have a different retention
value. It would need to run via the Windows scheduler.
A nice to have feature would be the ability to move to a different
folder instead of deleting, but not required. TIA.
-mark
As an Amazon Associate we earn from qualifying purchases.