Solved

Test if a file is locked in VBscript?

Posted on 2006-10-23
4
7,962 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…

763 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

11 Experts available now in Live!

Get 1:1 Help Now