[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 942
  • Last Modified:

WSH: delete folder

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
tdemeyer
Asked:
tdemeyer
1 Solution
 
AlexFMCommented:
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
 
r_a_j_e_s_hCommented:
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
 
tdemeyerAuthor Commented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
gecko_au2003Commented:
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
 
JesterTooCommented:
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
 
UnFiReCommented:
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

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