1.

Solve : Select and Delete Files older than 2 Weeks and 1 Month?

Answer»
Hello.

Would anyone know how to :

1. do a count of files in a DIRECTORY
2. output count to a txt file
3. and then delete files that are older than 2 WEEKS? 1 month?

Thanks.The first 2 are easy, the last really needs to be done with VBScript - batch files really dont do date manipulation.

The count can be taken using the FIND command, COUNTING the output from a DIR

dir /b /a-d|find /c /v "" > CountOfFiles.txt

the dir shows basic info about all files that do not have the attribute Directory
The find then provides a count of all the lines that do not contain a blank (ie those that have something in)
It then directs the count into the file CountOfFiles.txt

GrahamQuote
The first 2 are easy, the last really needs to be done with VeBScript

Might as well get the VBScript while you're here.

Code: [Select]Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("c:\scripts") 'Start directory
Set colSubFolders = f.SubFolders

For Each objFolder in colSubFolders
AgeFiles objFolder
Next

Sub AgeFiles(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 'Age factor
Wscript.Echo objFile & " " & objFile.DateCreated
End If
Next

For Each SubFolder In s
AgeFiles SubFolder
Next
End Sub

Change the start directory and age factor to your needs. The script uses recursion, any subdirectories of the start directory will also be processed.

As a safetly PRECAUTION the script reports back the aged files. When you're ready replace Wscript.Echo objFile & " " & objFile.DateCreated with fso.DeleteFile objFile


Discussion

No Comment Found