Jump to content

How can I determine if a file is locked or not?


Recommended Posts

Howdy folks, I'm trying to script a program that will find each profile on a computer, load its userhive, make some changes to that hive.

I've found some hive loading examples from the forums here that I'm working through to understand and modify, but occasionally the machines I'll need to modify have locked ntuser.dat files even if that user is no longer logged on.

Does anyone know a way to either unlock these files, so I can use'em (best option), or at least detect that they are locked so I can ignore'em (acceptable option for now)?

Link to comment
Share on other sites

Kind of brute-force, but effective -- a function to test-open the file. Returns 1 if the files exists and can be opened for write. Returns 0 and sets @error if not:

$MyFile = "C:\Temp\Test.txt"

If _FileTestOpen($MyFile) Then
    MsgBox(64, "File", "File can be used.")
Else
    MsgBox(16, "File", "Error.  Can not use file: @error = " & @error)
EndIf

Func _FileTestOpen($FileIn)
    If Not FileExists($FileIn) Then Return SetError(1, 0, 0)
    Local $hFile = FileOpen($FileIn, 1)
    If $hFile = -1 Then
        Return SetError(2, 0, 0)
    Else
        FileClose($hFile)
        Return 1
    EndIf
EndFunc   ;==>_FileTestOpen

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Kind of brute-force, but effective -- a function to test-open the file. Returns 1 if the files exists and can be opened for write. Returns 0 and sets @error if not:

$MyFile = "C:\Temp\Test.txt"

If _FileTestOpen($MyFile) Then
    MsgBox(64, "File", "File can be used.")
Else
    MsgBox(16, "File", "Error.  Can not use file: @error = " & @error)
EndIf

Func _FileTestOpen($FileIn)
    If Not FileExists($FileIn) Then Return SetError(1, 0, 0)
    Local $hFile = FileOpen($FileIn, 1)
    If $hFile = -1 Then
        Return SetError(2, 0, 0)
    Else
        FileClose($hFile)
        Return 1
    EndIf
EndFunc   ;==>_FileTestOpen

:)

Fantastic thanks! I'll give it a try. B-)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...