SXI Forum

A place to collect usefull tips, tricks and implementation strategies.

You are not logged in.

#1 22-11-2018 07:13:38

SeanR
Administrator
Registered: 20-11-2018
Posts: 147

DOS command to delete files of a certain age

I need to clear the SuccessStore and Exceptions list of files that are older than a week. What is the best way to do this?

Offline

#2 22-11-2018 08:28:19

SeanR
Administrator
Registered: 20-11-2018
Posts: 147

Re: DOS command to delete files of a certain age

This should be used with discretion as it could take a very long time to execute depending on the amount of files to be deleted.

This can be done using the Windows Command "ForFiles" from the command line or a batch file.  Stephanus brought this to my attention and on further research, I found this Microsoft link with more detail on the command.

Below is the batch file that he sent to me modified to delete files older than 30 days from the whole SuccessStore and subfolders.

The "ATTRIB" command removes the readonly attribute from all the files and a "/s" parameter will apply both the "ATTRIB" & "FORFILES" command to all matching files in the current directory and all of its subdirectories.  Using this parameter, you could remove the "\NEW_INCIDENT" from the example below and that would then apply to all SuccessStore files irrespective of "Action" performed.

This batch file could be executed by X-Scheduler.

@echo off
SET Days=30

@echo Clears all Read-only file attribute...
@ Change to SuccessStore folder
cd /d E:\Program Files\Southern X Integrators\X-ServiceBroker\SuccessStore
@ Remove ReadOnly attribute to enable delete
ATTRIB -R /S *.*
@echo Deleting files older than %Days% days in all subdirectories ...
forfiles /p "E:\Program Files\Southern X Integrators\X-ServiceBroker\SuccessStore" /s /m *.* /D -

30 /C "cmd /c del @path"
exit 0

Offline

#3 22-11-2018 08:29:32

SeanR
Administrator
Registered: 20-11-2018
Posts: 147

Re: DOS command to delete files of a certain age

The "DEL" command can delete "Read Only" files by adding a /F switch.  This eliminates the need to use the "ATTRIB" command and saves considerable amount of time.

Unfortunately the "DEL" command sometimes result in a question where it asks "if ok to delete on global wildcard" this causes the automatic delete process to fail.  This can be overcome by using the /Q (Quiet) switch.

I use the below config in X-Scheduler to execute the "Auto" delete of files older than a certain number of days.

 <Schedule name="ClearOldFiles">
  <AT>30 00 * * *</AT> 
  <Action>
     <Path>E:\Program Files\Southern X Integrators\X-Scheduler\CMD Files</Path> 
     <Command>ClearOldFiles.cmd</Command> 
     <Arguments>10</Arguments> 
   </Action>
  </Schedule>

The "ClearOldFiles.cmd" is copied below and the way I've set it up is to delete all Success Store files older than 10 days and all Error Store files older than 21 days (11 days more than Success or the 10 day argument)

@Echo Off

SET Days=%1

IF "%Days%"=="" goto NoDays
SET Errors=11
SET /A ErrorDays=%Days%+%Errors%
echo Days = %Days%
echo ErrorDays = %ErrorDays%

@Rem Save curent Folder
Pushd .

@Rem Change to SuccessStore folder
cd /d E:\Program Files\Southern X Integrators\X-ServiceBroker\SuccessStore

@Echo Deleting files older than %Days% days in all subfolders of SuccessStore ...

forfiles /s /m *.* /D -%Days% /C "cmd /c DEL /F /Q @path"

@Rem Change to FileSigregErrorStore folder
cd /d E:\Program Files\Southern X Integrators\X-ServiceBroker\FileSigregErrorStore

@Echo Deleting files older than %ErrorDays% days in all subfolders of FileSigregErrorStore ...

forfiles /s /m *.* /D -%ErrorDays% /C "cmd /c DEL /F /Q @path"

@Rem Restore original folder
Popd

Pause
goto END

:NoDays

@Echo *****************************************************
@Echo *****************************************************
@Echo ***                                               ***
@Echo *** Please provide number of days as a parameter. ***
@Echo ***                                               ***
@Echo *****************************************************
@Echo *****************************************************

Pause
Echo Exit 1

:END
Echo Exit 0

Offline

Board footer

Powered by FluxBB