?
Solved

Test if a file is locked in VBscript?

Posted on 2006-10-23
4
Medium Priority
?
8,475 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
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…
Suggested Courses
Course of the Month15 days, 7 hours left to enroll

743 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