?
Solved

WSH: delete folder

Posted on 2005-03-14
6
Medium Priority
?
934 Views
Last Modified: 2012-06-21
Somewhere in our scripts we have a DeleteFolder routine:

Sub DeleteAFolder(filespec)
   Dim fso
   Set fso = CreateObject("Scripting.FileSystemObject")
   fso.DeleteFolder(filespec)
End Sub


This works fine, but once i a while a file in the folder is locked by another user, so de deletefolder method fails, with an errormessage on the screen.

What would be the best way to check for locked files in the to-be-deleted folder, so I can pop-up a message/warning ???
0
Comment
Question by:tdemeyer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 13532947
Sub DeleteAFolder(filespec)
   on error resume next
   Dim fso
   Set fso = CreateObject("Scripting.FileSystemObject")
   fso.DeleteFolder(filespec)

   if Err.Number > 0 then
      MsgBox Err.Description
   end if

End Sub
0
 
LVL 3

Expert Comment

by:r_a_j_e_s_h
ID: 13533022
this is an alternate approach...


Sub DeleteAFolder(filespec)
   on error goto Err
           Dim fso
           Set fso = CreateObject("Scripting.FileSystemObject")
           fso.DeleteFolder(filespec)
           msgbox("File Deleted Successfully")
          exit sub
    Err:
         msgbox("Error:::" & Err.Description);
End Sub
0
 

Author Comment

by:tdemeyer
ID: 13533476
I think there is a slight problem with the above solutions: If, for example, the last file in a set is locked, will I end up with only that single file in the folder? Or is there some sort of check before fso.deletefolder starts?

0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 23

Expert Comment

by:gecko_au2003
ID: 13537145
what about if there are more then one file that are locked ?

wouldnt you need code like this :

  on error goto Err
           Dim fso
           Set fso = CreateObject("Scripting.FileSystemObject")
           fso.DeleteFolder(filespec)
           msgbox("File Deleted Successfully")
          exit sub
    Err:
         msgbox("Error:::" & Err.Description)
         Resume Next   '<--- Line of code added :)
End Sub

basically simmiliar to coding above with the Resume Next. Not sure if that makes any difference :-S
0
 
LVL 22

Accepted Solution

by:
JesterToo earned 300 total points
ID: 13539049
Try renaming the folder... if files are in use (open) anywhere in the folders's tree by any user/process the rename will fail.

Here is a "general purpose" routine to determine if anyone has open files in the folder... you can modify the code so that the "rename back to original" is replaced by the DeleteFolder method if you prefer.

Sub DeleteAFolder(filespec)
   Dim fso
   Set fso = CreateObject("Scripting.FileSystemObject")
   If FolderIsInUse(filespec) Then
      Wscript.Wcho "Folder is being used...'
   Else
      fso.DeleteFolder(filespec)
   End If
End Sub

Function FolderIsInUse(Path)
   Dim Rename
   Rename = 0

   ' find an acceptable new name for the folder...
   Do
      Rename = Rename + 1
      NewPath = Path & Rename
   Loop Until (Not fso.FolderExists(NewPath))

   'then attempt tp rename to new name
   On Error Resume Next
   fso.MoveFolder Path, NewPath
   If Error.Number > 0 Then
      FolderIsInUse = True
   Else
      'not in use... rename it back to original
      fso.MoveFolder NewPath, Path
      FolderIsInUse = False
   End If
   On Error Goto 0

End Function

HTH,
Lynn              
0
 
LVL 1

Expert Comment

by:UnFiRe
ID: 13541170
Try
   Sub DeleteAFolder(filespec)
      Dim fso
      Set fso = CreateObject("Scripting.FileSystemObject")
      fso.DeleteFolder(filespec)
   End Sub
Catch ex As Exception
   MsgBox(ex.message)
End Try
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

777 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