Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to delete old files 2

Status
Not open for further replies.
May 11, 2004
37
US
Does anyone know how to script the deletion of old files? I specifically want to delete files that contain "LPT$VPN" in the file name and are older than 3 days. Thanks in advance.

Rich
 
Hello richardrekos,

You can consider using forfiles.exe commandline utility coming with resource kits. Look at its help file or issue
forfiles -?
for syntax.

For instance, to [1] delete *.txt files [2] in certain path d:\test [3] including subdirectories for files [4] with date more than 30 days old from today, the line in the batch is something like this:
[tt]
forfiles -pd:\test -s -m*.txt -d-30 -c"%comspec% /c del @FILE"
[/tt]
regards - tsuji
 

Code:
Path1 = "E:\Program Files\Exchsrvr\Mailroot\vsi 1\BadMail"

Dim fso 
Dim oFolder
Dim oFile
Dim oSubFolder

  Set fso = createobject("Scripting.FileSystemObject")
  
   Set oFolder = fso.GetFolder(Path1)
  
   For Each oFile In oFolder.files

   	If DateDiff("d", oFile.DateCreated,Now) > 3 Then
            If Instr(1,oFile.Name,"LPT$VPN",1) 
              oFile.Delete True
            End If        
      	End If
  Next

I hope you find this post helpful. Please let me know if it was.

Regards,

Mark
 
Hi Mark, Thanks for your response. This doesn't seem to work. Maybe the issue is that the file names are like LPT$VPN.100, LPT$VPN.101 and so on. Is there a way to specify a wildcard to get rid if files containing LPT$VPN?

Thank again,

Rich
 
That should be doing it. Try adding some error checking here like this:

If DateDiff("d", oFile.DateCreated,Now) > 3 Then
Wscript.echo "This file is older than 3 days:" & oFile.Name
If Instr(1,oFile.Name,"LPT$VPN",1)
Wscript.echo "This file contains LPT$VPN:" & oFile.Name
oFile.Delete True
End If
End If


I hope you find this post helpful. Please let me know if it was.

Regards,

Mark
 
Be very careful to applying this.
[tt]
forfiles -pd:\test -s -m*.* -d-30 -c"%comspec% /c if ISDIR==TRUE deltree /y @FILE"
[/tt]
- tsuji
 
Further note:
There are os-dependent factors on the commands used such as deltree here. In any case, something equivalent has to be deviced if it is not supported. Silence mode for del, and rd may be needed to achieve equivalent functionality of deltree.
- tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top