Saved Bookmarks
| 1. |
Solve : Del files by date? |
|
Answer» I need a batch file that can delete files by date and or time, using current date/time as a gauge..any help would be appreciated....this will be used in a windows 2000 environmentDelete files from where? A single directory? A tree structure? using current date/time as a gaugeUsed how? Are you aging files? More questions than answers. XCOPY has a date parameter. You could be super clever and extract the current date (mm-dd-yyyy) from %date% (%date% has that pesky day of week embedded), copy the files you want to keep to a temp directory, delete all the files in the current directory, then copy all the files from the temp directory back to the original directory. On the other HAND, you could write a script. The following will age files in a tree structure, deleteting all over 30 days old: Code: [Select]Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.GetFolder("c:\temp") '<== change as needed 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 - 30 Then Wscript.Echo objFile & " " & objFile.DateCreated '<== change End If Next For Each SubFolder In s ShowFiles SubFolder Next End Sub Save the script with a vbs extrnsion and run from the command line as cscript scriptname.vbs As written, the script will list the files to be deleted. If you agree with the results change the entire WScript.Echo line to fso.DeleteFile objFile, True. You could also change 30 to any number. Windows has many tools that are far better than batch language. This might just be one of them. Hope this gives you some ideas. 8-) would this script also work in windows 2003, i TRIED it and all i get is the windows script VER. and copy write info. is there something i'm doing wrong?Without knowing what directory you pointed the script to check, it would be difficult to guess what went wrong (if anything). One possibilty would be that none of the files in the directory are over 30 days old. Yes, the script will work on all Window versions starting with Win95C. Note: If you left in the WScript.Echo statement, the script should have listed all files over 30 days old. If you replaced that line with the fso.DeleteFile statement, there would be no output except the copyright info. Posting the script that you actually ran, and a DIR listing of the directory the script points to might be helpful. Let us know. 8-)The following command would delete TheFile only when it's time stamp is equal or older than a given RemoveDate. set TheFile=myfile.txt set RemoveDate=03/01/2006 xcopy "%TheFile%" .. /L /D:%RemoveDate%|find "0 File(s)">NUL&&del /q "%TheFile%" Running xcopy with option /L will only pretend to copy. Running xcopy with option /D:<Date> will only copy files newer than a given date. The find command would only succeed when 0 files would be copied, which means when the file is not newer than the given date. The && ensures that the del command only executes when the find command succeeded, so to say when the file is not newer that the given date. Hope this information is useful |
|