Sign in to follow this  
Followers 0
speedy6

File in use

14 posts in this topic

Can i check if a file is being used?

Share this post


Link to post
Share on other sites



Can i check if a file is being used?

There might be simpler ways to do it so see what other replies you get, but here is a link to a way to do it with WMI

page 548 on. Looks like this could be done in AutoIt quite easily.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

This version of it works, but only if it's open via a CIFS share, because it accesses the object via LanManServer, not the local FileSystemObject:

$varFile= "C:\Temp\Test.txt"
$objFS = ObjGet("WinNT://" & @ComputerName & "/lanmanserver", "fileservice")
$varFoundNone = True
$sMsg = ""
For $objRes in $objFS.Resources
    If $objRes.Path = $varFile Then
        $varFoundNone = False
        $sMsg &= $objRes.User & ", "
    EndIf
Next
If $varFoundNone Then
      MsgBox(64, "No Result", "Didn't find that file opened by anyone.")
  Else
      MsgBox(64, "Result", "Found file in use by: " & StringTrimRight($sMsg, 2))
EndIf

Third party apps can tell if a file is in use, but I don't see how to get that in the scripting environment without doing something that will CHANGE the file, like attempting to open it for append then closing it immediately.

:)


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

Share this post


Link to post
Share on other sites

So there's no other way...

Beacause i wanne check if an other application is using a xml file, if so, stay away from it...

Is that possible with you're code?

Share this post


Link to post
Share on other sites

without doing something that will CHANGE the file, like attempting to open it for append then closing it immediately.

try that..?


tolle indicium

Share this post


Link to post
Share on other sites

try that..?

Whill do thx

Share this post


Link to post
Share on other sites

Try this:

I think it's from larry, do a search and figure out the things you need to know...

$a = _RealFileOpen("D:\Daten\Text\UltimateFAQ.cc3")
MsgBox(4096,"",$a)
_RealFileClose($a)

Func _RealFileOpen( $szFile )
   Local $GENERIC_READ = 0x80000000 
   Local $OPEN_EXISTING = 3 
   Local $FILE_ATTRIBUTE_NORMAL = 0x00000080
   Local $RFO_h
   $RFO_h = DllCall( "kernel32.dll", "hwnd", "CreateFile", _
                "str", $szFile, _
                "long", $GENERIC_READ, _
                "long", 0, _
                "ptr", 0, _
                "long", $OPEN_EXISTING, _
                "long", $FILE_ATTRIBUTE_NORMAL, _
                "long", 0 )
   Return $RFO_h[0]
EndFunc

Func _RealFileClose( $hFile )
   Local $RFC_r

   $RFC_r = DllCall( "kernel32.dll", "int", "CloseHandle", _
                     "hwnd", $hFile )

   Return $RFC_r[0]
EndFunc

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Try this:

I think it's from larry, do a search and figure out the things you need to know...

$a = _RealFileOpen("D:\Daten\Text\UltimateFAQ.cc3")
MsgBox(4096,"",$a)
_RealFileClose($a)

Func _RealFileOpen( $szFile )
   Local $GENERIC_READ = 0x80000000 
   Local $OPEN_EXISTING = 3 
   Local $FILE_ATTRIBUTE_NORMAL = 0x00000080
   Local $RFO_h
   $RFO_h = DllCall( "kernel32.dll", "hwnd", "CreateFile", _
                "str", $szFile, _
                "long", $GENERIC_READ, _
                "long", 0, _
                "ptr", 0, _
                "long", $OPEN_EXISTING, _
                "long", $FILE_ATTRIBUTE_NORMAL, _
                "long", 0 )
   Return $RFO_h[0]
EndFunc

Func _RealFileClose( $hFile )
   Local $RFC_r

   $RFC_r = DllCall( "kernel32.dll", "int", "CloseHandle", _
                     "hwnd", $hFile )

   Return $RFC_r[0]
EndFunc

This will tell you if it doesn't exist, is ReadOnly, or is already opened exclusively by another app. Notepad.exe opening a file isn't detected because it only works with an memory image of the file and doesn't hold it in an open state.

:)

Edit: Corrected note about Notepad.exe.

Edited by PsaltyDS

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

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Note this will not tell you if the file is opened non-exclusively for Read (i.e. Notepad is viewing it). But will tell you if it doesn't exist, is ReadOnly, or is already opened exclusively by another app.

Not exactly. That CreateFile should return error if a file already has a handle opened in any access mode and any share rights, because you specify that you don't want to share it, which makes unimportant which share rights the other app has opened it with.

Notepad in this case is a bad test subject, because it does not keep the file open. It opens the file, reads it into memory and then closes the file. So technically the file is not open = unused.

Edited by Siao

"be smart, drink your wine"

Share this post


Link to post
Share on other sites

Not exactly. That CreateFile should return error if a file already has a handle opened in any access mode and any share rights, because you specify that you don't want to share it, which makes unimportant which share rights the other app has opened it with.

Notepad in this case is a bad test subject, because it does not keep the file open. It opens the file, reads it into memory and then closes the file. So technically the file is not open = unused.

Fair enough... it still works! ;)

Thanks for the correction, I edited the post.

:)


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

Share this post


Link to post
Share on other sites

I am having a problem with this. I'm trying to detect if an audio file is being used by another app and this isn't working....at least with the particular apps I have that are using the audio file. Not all audio apps show that they are using an audio file it seems, maybe they are operating like notepad...although I can't see why an audio app would do that. I even tried to see if they are creating temp copies and using those, but that isn't the case. Are there any other solutions or workarounds? Is there a way to see if a file is in memory? But along with that question comes, if it is read to memory to play back, is it deleted from memory after playback?...which is what I would need for that scenario.

So anything?

Thanks.

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

@speedy6 - tell us more about the situation: are you writing a script for general use or only for you? If it is more certainly another audio player the name of which is known, you could check for the the existence of its process, and nest some code that would check its "playlist now-playing" file using some COM objects:

If ProcessExists("wmplayer.exe") Then
    If COM_Code() Then
    ;...
    EndIf
EndIf
Edited by Squirrely1

Das Häschen benutzt Radar

Share this post


Link to post
Share on other sites

bump

Share this post


Link to post
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
Sign in to follow this  
Followers 0