Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.
PurgeFile.Bat echo off set pgmpath="c:\Windows\Scripts" REM This batch file is used to purge files in a specified directory REM that are over a given age. The arguments to this batch file are: REM %1 = The path to the directory containing the files to be purged. REM %2 = The pattern of the files to be selected within the directory. REM The pattern may contain wild cards as needed. REM %3 = The name of the file that this batch file will create to contain REM the list of candidate files. REM %4 = The age above which files should be purged. This argument is not REM used directly in the batch file, but is passed to the VB script REM called to perform the tests and actual purge. set fpath=%1\%2 dir %fpath% > %1\%3 /B /A:-D-S cscript %pgmpath%\purgefilescript.vbs %1 %3 %4 ------------------------------- below is another script-- purgefilescript.vbs 'This script is used to purge files over a specified age in days. 'It uses a file containing a list of candidate files as input. 'The arguments to this script are: ' (0) the path to the directory to be checked and purged. ' No files are specified here, just the directory ' (1) the name of the file containing the candidate list of files. ' Must be in the above directory ' (2) the age in days above which files should be purged. ' 'To execute this script, use a command line entry like: ' cscript PurgeFiles.vbs "c:\My Directory\My Subdirectory" 'Dirfile.txt 7 ' 'This will purge all files older than seven days with an entry in 'Dirfile.txt from c:\My Directory\My Subdirectory Dim fso, f, path, candidates, cpath, fpath, retstring Const ForReading = 1 Set objArgs = Wscript.Arguments 'Prepare to parse the arguments path = objArgs(0) 'This is the path without the file names candidates= objArgs(1) 'This is the name of the candidate list file. 'Any file older than age days old will be purged. 'The argument must be converted to integer. age = CInt(objArgs(2)) cpath = path & "\" & candidates Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.OpenTextFile(cpath, ForReading) Wscript.Echo "Processing files in " & cpath Do While f.AtEndOfStream <> True retstring = f.ReadLine fpath = path & "\" & retstring Set f2 = fso.GetFile(fpath) 'Extract the file's creation date-time dcr = f2.DateCreated 'Look for the first space in the creation date string i = InStr(1,dcr," ") 'Extract just the date from the date-time string dcr = left(dcr, i-1) 'Calculate the number of days between today and the create date d = datediff("d", dcr, Date) 'Compare age and file name If (d > age) and (retstring <> candidates) then Wscript.Echo "Deleting " & retstring & " which is " & d & " days old." 'Remove the comment quote from the following line to really delete. fso.DeleteFile(fpath, true) 'True means force the delete if read only. End If Loop f.Close
Join the community of 500,000 technology professionals and ask your questions.