Solved

Test if a file is locked in VBscript?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
LINQ return type for nested group query 6 74
Excel object stays open 19 71
Sending a email via excel using vba 6 72
How does CurrentUser work? 10 31
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…
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

864 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

24 Experts available now in Live!

Get 1:1 Help Now