• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 613
  • Last Modified:

Vbscript

I need to delete all files that start with "df" from a  folder.The following script works fine
if there are files but fails with "File  not Found" error if there aren't any. How can I fix
ths script.


'On Error resume next

Dim objFso

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshShell  = CreateObject("WScript.Shell")

ObjFso.DeleteFile("\\????\anzap$\outboundbak\df*.*")
0
Bianchi928
Asked:
Bianchi928
1 Solution
 
cantorisCommented:
As a simple solution perhaps you can put your
On Error Resume Next
on the line before the deletion and then
On Error Goto 0
after it.

Or for a better but longer way around it, get the parent folder object and then iterate through its Files collection and delete any whose name matches your pattern.
0
 
Bianchi928Author Commented:
Thanks. I don't want to use the "On Error Resume Next" option. I would prefer the iteration through the file collection.
0
 
KGSACMCommented:
Hi, I don't think the script is 'broken' for you to fix. the error you are receiving is actually accurate since there are no files. You might want check and see if there are any files in there first.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
cantorisCommented:
Set objFolder = objFSO.GetFolder("\\server\anzap$\outboundbak")
For Each objFile In objFolder.Files
  If Left(LCase(objFile.Name),2)="df" Then objFile.Delete
Next

Open in new window

0
 
Bianchi928Author Commented:
Good as gold
Thanks. Cheers
0
 
pony10usCommented:
There is also this option for checking for existance of a file:

if fso.FileExists(newpath) then            
fso.DeleteFile newpath        
end if
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now