Solved

Test if a file is locked in VBscript?

Posted on 2006-10-23
4
8,164 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 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

827 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