Posted on 2012-08-21
Last Modified: 2012-08-21
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")

Question by:Bianchi928
    LVL 16

    Expert Comment

    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.

    Author Comment

    Thanks. I don't want to use the "On Error Resume Next" option. I would prefer the iteration through the file collection.

    Expert Comment

    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.
    LVL 16

    Accepted Solution

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

    Open in new window


    Author Closing Comment

    Good as gold
    Thanks. Cheers
    LVL 25

    Expert Comment

    There is also this option for checking for existance of a file:

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
    Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  ( Here (http…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now