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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete file if more than one is prezent in folder

Status
Not open for further replies.

LievenV

MIS
Dec 3, 2002
40
GB
Hi,
A directory contains X files of which one or more follow the patern abc*.* . I need a script to see if more than one file follow that patern and if so, the most recent file has to be left alone, the rest has to be deleted.
Thanks for your help!
 
Set objFolder = FSO.GetFolder("c:\test")
intCounter = 0
datModified = 0
For Each aFile In objFolder.Files
If LCase(Left(aFile.Name)) = "abc" Then
If intCounter > 1 Then
'we have more than one abc file
If aFile.DateModified > datModified Then
datModified = aFile.DateLastModified
strNewestFile = aFile.Name
Else
aFile.Delete
End If
Else
datModified = aFile.DateLastModified
strNewestFile = aFile.Name
End If
intCounter = intCounter + 1
End If
Next


might help you, might even work but its a bit early in the day for me!! ;-)
 
Thanks for your help mrmovie, had to make a change here and there but you certainly set me off in the right direction. Thanks!!
FYI, the changed code:

Set FSO = createobject("Scripting.FileSystemObject")

Set objFolder = FSO.GetFolder("c:\test")
intCounter = 0
datModified = 0
For Each aFile In objFolder.Files
If LCase(Left(aFile.Name,3)) = "abc" Then
If intCounter > 0 Then
'we have more than one abc file
If aFile.DateLastModified > datModified Then
FSO.DeleteFile(objFolder & "\" & strNewestFile)
datModified = aFile.DateLastModified
strNewestFile = aFile.Name
Else
aFile.Delete
End If
Else
datModified = aFile.DateLastModified
strNewestFile = aFile.Name

End If
intCounter = intCounter + 1
End If
Next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top