[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Test if a file is locked in VBscript?

Posted on 2006-10-23
4
Medium Priority
?
8,743 Views
Last Modified: 2008-01-09
I got this vbscript:

Set FSO = CreateObject("Scripting.FileSystemobject")
' my objective the windows temp folder
Set objFsoFolder = FSO.GetFolder("C:\Windows\Temp\")

Set objFsoWindowsTemp = objFsoFolder.SubFolders   'collection of subfolders I don't use

On Error Resume Next
For Each file In objFsoFolder.files
       wscript.echo file
                '  IF FILE NOT locked THEN
               FSO.DeleteFile(file)
               END IF
Next

msgbox("Done deleting batch files")

Set FSO = nothing
Set objFsoFolder = nothing
Set objFsoWindowsTemp = Nothing



I want to get rid of the On Error Resume next line and test the status of the file if it is locked before removing it.

How can i test for a file lock?
 
0
Comment
Question by:howart
  • 2
4 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 17791033
If the file is locked does it actually let you delete it?  Also, by locked do you mean exclusively opened by another user/process?  I wouldn't imagine it let you delete it otherwise.
0
 
LVL 12

Accepted Solution

by:
jkaios earned 1500 total points
ID: 17791800
Just keep the "On Error Resume Next" statement in your script, but add the following 4 lines below the FSO.DeleteFile method.

On Error Resume Next
For Each file In objFsoFolder.files
     wscript.echo file
     FSO.DeleteFile(file)
     If Err Then                        '<-- ADD THE FOLLOWING LINES
        wscript.echo "Unable to delete file '" & file & "'. The file is currently locked by another process."
        Err.Clear
     End If
Next
0
 
LVL 1

Author Comment

by:howart
ID: 17794152
You cannot delete a locked file you will get an error like
C:\WINDOWS\Temp\QUERY.LOG
C:\WINDOWS\batch\delete-temp-files.vbs(11, 2) Microsoft VBScript runtime error: Permission denied

Tested the script from jkaios, it works but you can only log that the file cannot be deleted for some reason.

Got now:
Set FSO = CreateObject("Scripting.FileSystemobject")
' our objective the windows temp folder
Set objFsoFolder = FSO.GetFolder("C:\Windows\Temp\")

Set objFsoWindowsTemp = objFsoFolder.SubFolders   'collection of subfolders

On Error Resume Next
For Each file In objFsoFolder.files
       strResult = strResult & file.name    
       
      FSO.DeleteFile(file)
           If Err Then
             strResult = strResult & VBTab & "Unable to delete file!"
              Err.Clear
      Else
           strResult = strResult & VBTab & "File deleted"      
           End If
        wscript.echo strResult
       
       ' reset strResult to an emtpy string
      strResult = "" 
Next

msgbox("Done deleting files in C:\Windows\Temp")

Set FSO = nothing
Set objFsoFolder = nothing
Set objFsoWindowsTemp = Nothing


Will wait for some guru who can tell me how I can test a file if it is locked by another file or not.



0
 
LVL 28

Expert Comment

by:AzraSound
ID: 17797826
>>it works but you can only log that the file cannot be deleted for some reason

What do you want to happen?  What does ignoring an error because you are unable to delete the file versus checking if it is locked and then not even trying to delete it?

There is nothing in FSO that provides checking if a file is locked that I am aware of, and FSO is the only mechanism by which you can access the file system, barring rolling your own COM object.  You can test for a locked file via the Windows API, but the API is not accessible to you from VBScript unless you, again, roll your own component.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

829 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