| 1. |
Solve : Delete files older than 7 days? |
|
Answer» I browsed through the entire forum and saw this question has been asked/answered several times. However, I'm not smart enough to get it working in my case. I'm using xcopy to copy folders every night through a batch job, & rename takes care to name these copied folders on the system date. CD\C:\Backup This little ditty merely sorts a directory listing of files that have the archive bit set. After skipping the first 7 files in the list, it proceeds to delete the rest of them. Quote echo off Not really up-to-date with the forfiles command, I'm guessing this will delete files dated either today or yesterday. This little routine may actually do what you need: Code: [SELECT]Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.GetFolder("c:\backup") Set colSubFolders = f.SubFolders For Each objFolder in colSubFolders ShowFiles objFolder Next Sub ShowFiles(Fld) Set k = fso.GetFolder(Fld) Set s = k.SubFolders Set kf = k.Files For Each objFile In kf If objFile.DateCreated < date - 7 Then Wscript.Echo objFile & " " & objFile.DateCreated End If Next For Each SubFolder In s ShowFiles SubFolder Next End Sub As written the snippet will simply list the files older than a week. Save the file with a vbs extension and run as cscript scriptname.vbs Once your satisfied that these are the files you intend to delete, replace Wscript.Echo objFile & " " & objFile.DateCreated with fso.DeleteFile objFile, True and run the job again. Good luck. Hi Thank you I didn't follow the line 'run as cscript scriptname.vbs". I saved the vbs as test.vbs. What am I exactly supposed to do after this? Another q, the server doesn't allow VB. Is it POSSIBLE to achieve this using a batch file ? Will wait for your response.My machine is Windows 2003 Server. So, do you think this should work? forfiles /p c:\backup /s /m *.* /d -7 /c "cmd /c del @file : date >= 7 days"Yes, you would run the script as cscript test.vbs. When you are satisfied that these are the files to delete, make the code change and re-run the job. I guess scriptname should have been in italics. While your machine may not have VB installed, VBScript is installed on all Windows versions since Win95C. Code: [Select]FORFILES v 1.1 - [emailprotected] - 4/98 Syntax : FORFILES [-pPath] [-mSearch Mask] [-ccommand] [-d<+|-><DDMMYYYY|DD>] [- s] -pPath Path where to start searching -mSearch Mask Search files according to <Search Mask> -cCommand Command to execute on each file(s) -d<+|-><DDMMYYYY|DD> Select files with date >= or <=DDMMYYYY (UTC) or files having date >= or <= (current date - DD days) -s Recurse directories -v Verbose mode The following variables can be used in Command : @FILE, @FNAME_WITHOUT_EXT, @EXT, @PATH, @RELPATH, @ISDIR, @FSIZE, @FDATE, @FTIME To include special hexa characters in the command line : use 0xHH Default : <Directory : .> <Search Mask : *.*> <Command : "CMD /C Echo @FILE"> Examples : FORFILES -pc:\ -s -m*.BAT -c"CMD /C Echo @FILE is a batch file" FORFILES -pc:\ -s -m*.* -c"CMD /C if @ISDIR==TRUE echo @FILE is a directory" FORFILES -pc:\ -s -m*.* -d-100 -c"CMD /C Echo @FILE : date >= 100 days" FORFILES -pc:\ -s -m*.* -d-01011993 -c"CMD /C Echo @FILE is quite old!" FORFILES -pc:\ -s -m*.* -c"CMD /c echo extension of @FILE is [emailprotected]" I have no experience with forfiles, so I may not be the best one to ask. Best I can recommend would be to test out your code in a test directory until it works. Good luck. |
|