1.

Solve : Batch file delete file by file name?

Answer»

I'm trying to create a batch file that will delete files by a certain name. The files will all be similar to this. a20081010.txt or a20081011.txt or a20081012.txt... etc. Then if the files are older then 30 days for datemodified then I WANT to delete the file. Here is what I have so far. OBVIOUSLY, it doesn't work in it's current state. If I comment out the IF EXISTS it works if the file is older then 30 days. So I just need to work in the file name PART. Any help would be greatly appreciated!

SET objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\Documents and Settings\Username\My Documents\batch file\test")
For Each efile in objFolder.Files
If exist a********.txt Then
If DateDiff("d",eFile.DateLastModified,Now) >= 30 Then
objFSO.DeleteFile(eFile)
End If
Next


just one wildcard will do. Code: [Select] if exist a*.txt...
FBOk I added just one wildcard to it but when I try to run it I get an ERROR with line 4 saying it's expecting a 'Then'. If I comment line 4 out it works fine. So I'm guessing something is wrong with that line, just don't know what......The file existence test is redundant, it's also incorrect syntax. A file would not be in the files collection if it did not exist. VBScript doesn't use wildcards, but you can improvise.

Code: [Select]Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\Documents and Settings\Username\My Documents\batch file\test")
For Each efile in objFolder.Files
If Mid(eFile.Name, 1, 1) = "a" And objFSO.GetExtensionName(eFile) = "txt" Then
If DateDiff("d",eFile.DateLastModified,Now) >= 30 Then
objFSO.DeleteFile(eFile)
End If
End If
Next

why is this in MSDOS?

FB



Discussion

No Comment Found