StarwarsKid
(Seasoned Scripter)
2006-03-09 07:45 PM
How to check if a file is in use

Can someone give me direction on how to create a script that checks if a file is in use? Thanks

Benny69
(MM club member)
2006-03-09 07:48 PM
Re: How to check if a file is in use

check out EnumProcess()

StarwarsKid
(Seasoned Scripter)
2006-03-09 08:03 PM
Re: How to check if a file is in use

Benny, it looks like this UDF will check for a running process and kill it if needed. Is there a way to check if a file is in use?

Benny69
(MM club member)
2006-03-09 08:08 PM
Re: How to check if a file is in use

if the file in question is a .exe it is a process

StarwarsKid
(Seasoned Scripter)
2006-03-09 08:40 PM
Re: How to check if a file is in use

The files I'm trying to check are various types of CAD data object files.

.dbf, .prj, .xml, .sbx, .sbn, .shp, .shx

I need to check if any of these files are in use on the local computer before I run a script that overwrites them. Any suggestions on how I can do this?

I may also be able to run an EnumProcess on the application that would be using these files. I don't have that information yet, so I'm starting with checking the "used" files first.


Les
(KiX Master)
2006-03-09 08:46 PM
Re: How to check if a file is in use

Good luck. I asked the same question too but my friends let me down. You can do it with an older version of KiX but not the latest.

Les
(KiX Master)
2006-03-09 08:48 PM
Re: How to check if a file is in use

Why do you need to check if they are in use? just flag them for overwrite on boot instead.

Benny69
(MM club member)
2006-03-09 08:48 PM
Re: How to check if a file is in use

if that is the case check out Inuse.exe.
"InUse is a command-line tool that performs on-the-fly replacement of files currently in use by the operating system. You must be a member of the Administrators group to use this tool."


Les
(KiX Master)
2006-03-09 08:49 PM
Re: How to check if a file is in use

The latest KiX has it built-in.

StarwarsKid
(Seasoned Scripter)
2006-03-09 08:57 PM
Re: How to check if a file is in use

Can you elaborate on the "flag for overwrite on boot" suggestion? Thanks for the input.

Mart
(KiX Supporter)
2006-03-09 09:17 PM
Re: How to check if a file is in use

There is a UDF to do this.
UpdateFileOnReboot()- Schedules a file to be replaced on reboot

Or you can put a script to replace the files in the runonce key in the registry.


AllenAdministrator
(KiX Supporter)
2006-03-09 09:34 PM
Re: How to check if a file is in use

I just found this... it's comes with XP...
OpenFiles.exe

For it to check local files you first have to enable it by:
Code:
 openfiles /local on  


Then Reboot.

Once you restart then just issue the following command:
Code:
 openfiles /query 



This is my output:
Quote:


Files Opened Locally:
---------------------

ID Process Name Open File (Path\executable)
===== ==================== ==================================================
12 ati2evxx.exe C:\WINDOWS\system32
12 userinit.exe C:\Documents and Settings\Administrator
96 userinit.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
12 explorer.exe C:\Documents and Settings\Administrator
60 explorer.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
68 explorer.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
96 explorer.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
108 explorer.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
112 explorer.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
156 explorer.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
320 explorer.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
344 explorer.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
504 explorer.exe C:\Documents and Settings\Administrator\Desktop
512 explorer.exe C:\Documents and Settings\All Users\Desktop
516 explorer.exe C:\..\Application Data\Microsoft\CD Burning
596 explorer.exe C:\..\Microsoft\Internet Explorer\Quick Launch
668 explorer.exe C:\Documents and Settings\Administrator\Start Menu
728 explorer.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
752 explorer.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
772 explorer.exe C:\..\History\History.IE5\index.dat
780 explorer.exe C:\..\Content.IE5\index.dat
796 explorer.exe C:\..\Administrator\Cookies\index.dat
900 explorer.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
920 explorer.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
924 explorer.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
1032 explorer.exe C:\Documents and Settings\Administrator\PrintHood
1040 explorer.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
1088 explorer.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
1216 explorer.exe C:\Documents and Settings\All Users\Start Menu
80 CLI.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
228 CLI.exe C:\..\v1.1.4322\CONFIG\security.config.cch
240 CLI.exe C:\..\v1.1.4322\CONFIG\enterprisesec.config.cch
248 CLI.exe C:\..\v1.1.4322\security.config.cch
260 CLI.exe C:\..\cli.exe.c88dbd71.ini.inuse
364 CLI.exe C:\Program Files\ATI Technologies\ATI.ACE
444 CLI.exe C:\..6595b64144ccf1df_1.0.2600.2180_x-ww_522f9f82
884 CLI.exe C:\..\LOCALS~1\Temp\Perflib_Perfdata_774.dat
1368 CLI.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
12 msnmsgr.exe C:\Documents and Settings\Administrator
64 msnmsgr.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
68 msnmsgr.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
160 msnmsgr.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
424 msnmsgr.exe C:\Program Files\MSN Messenger\msnmsgr.exe
432 msnmsgr.exe C:\Program Files\MSN Messenger\msnmsgr.exe
440 msnmsgr.exe C:\Program Files\MSN Messenger\msnmsgr.exe
448 msnmsgr.exe C:\Program Files\MSN Messenger\msnmsgr.exe
632 msnmsgr.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
1148 msnmsgr.exe C:\..\Microsoft\SystemCertificates\My
1236 msnmsgr.exe C:\..\Content.IE5\index.dat
1256 msnmsgr.exe C:\..\Administrator\Cookies\index.dat
1264 msnmsgr.exe C:\..\History\History.IE5\index.dat
1292 msnmsgr.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
1360 msnmsgr.exe C:\..\Microsoft\Network\Connections\Pbk
12 Web2pop.exe C:\Documents and Settings\Administrator
80 Web2pop.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
12 msmsgs.exe C:\Documents and Settings\Administrator
16 msmsgs.exe C:\..6595b64144ccf1df_1.0.2600.2180_x-ww_522f9f82
28 msmsgs.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
68 msmsgs.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
96 msmsgs.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
376 msmsgs.exe C:\Program Files\Messenger\msmsgs.exe
384 msmsgs.exe C:\Program Files\Messenger\msmsgs.exe
392 msmsgs.exe C:\Program Files\Messenger\msmsgs.exe
12 reader_sl.exe C:\Documents and Settings\Administrator
60 reader_sl.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
12 SafeCfg.exe C:\..\NETGEAR\NETGEAR ProSafe VPN Client
64 SafeCfg.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
12 qbupdate.exe C:\..\Common Files\Intuit\QuickBooks\QBUpdate
68 qbupdate.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
92 qbupdate.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
468 qbupdate.exe C:\..\Content.IE5\index.dat
484 qbupdate.exe C:\..\Administrator\Cookies\index.dat
492 qbupdate.exe C:\..\History\History.IE5\index.dat
520 qbupdate.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
588 qbupdate.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
700 qbupdate.exe C:\..\Microsoft\Network\Connections\Pbk
12 SyncToy.exe C:\Program Files\Microsoft\SyncToy
80 SyncToy.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
228 SyncToy.exe C:\..\v1.1.4322\CONFIG\security.config.cch
240 SyncToy.exe C:\..\v1.1.4322\CONFIG\enterprisesec.config.cch
248 SyncToy.exe C:\..\v1.1.4322\security.config.cch
620 SyncToy.exe C:\..\Microsoft\SystemCertificates\My
656 SyncToy.exe C:\..\SyncToy.exe.7d71c9a.ini.inuse
660 SyncToy.exe C:\..6595b64144ccf1df_1.0.2600.2180_x-ww_522f9f82
676 SyncToy.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
712 SyncToy.exe C:\..\LOCALS~1\Temp\Perflib_Perfdata_70.dat
1232 SyncToy.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
84 OUTLOOK.EXE C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
292 OUTLOOK.EXE C:\..\Microsoft\Outlook\outcmd.dat
380 OUTLOOK.EXE C:\My Documents\Email\outlook.pst
568 OUTLOOK.EXE C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\spambayes1.log
572 OUTLOOK.EXE C:\PROGRA~1\MICROS~2\Office\MSOUTL9.OLB
600 OUTLOOK.EXE C:\Program Files\Microsoft Office\Office\MSO9.DLL
652 OUTLOOK.EXE C:\..\Email\SpamBayes\default_bayes_database.db
656 OUTLOOK.EXE C:\..\Email\SpamBayes\default_message_database.db
660 OUTLOOK.EXE C:\Program Files\Common Files\System\Mapi\1033\NT
688 OUTLOOK.EXE C:\My Documents\Email\outlook.pst
752 OUTLOOK.EXE C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
64 MAPISP32.EXE C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
324 MAPISP32.EXE C:\Program Files\Common Files\System\Mapi\1033\NT
348 MAPISP32.EXE C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
364 MAPISP32.EXE C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
372 MAPISP32.EXE C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
456 MAPISP32.EXE C:\..\Microsoft\Outlook\MSIN5300.RHC
480 MAPISP32.EXE C:\..\Microsoft\Outlook\MSIN2849.RHC
544 MAPISP32.EXE C:\My Documents\Email\outlook.pst
12 devldr32.exe C:\Documents and Settings\Administrator
60 devldr32.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
12 cmd.exe C:\Documents and Settings\Administrator
96 cmd.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9
12 openfiles.exe C:\Documents and Settings\Administrator
1924 openfiles.exe C:\..6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9


Files Opened Remotely via local share points:
---------------------------------------------
INFO: No shared open files found.





NTDOCAdministrator
(KiX Master)
2006-03-09 09:43 PM
Re: How to check if a file is in use

OPENFILES /parameter [arguments]

Description:
Enables an administrator to list or disconnect files and folders
that have been opened on a system.

Parameter List:
/Disconnect Disconnects one or more open files.

/Query Displays files opened locally or from shared
folders.

/Local Enables / Disables the display of local open files.

/? Displays this help message.

Examples:
OPENFILES /Disconnect /?
OPENFILES /Query /?
OPENFILES /Local /?


As for the replacement KiXtart 4.52 Beta 2 supports this. The UDF by Jens does not properly support it.
 

Changes in 4.52 Beta 2:

CompareFileTimes has been enhanced so it can operate on open files.
RD further enhanced such that it continues deleting files & folders, even if an error occurs.
Fixed the handling of F10, F11, F12, etc keys by SendKeys.
Added a '/p' (Postpone) option to MOVE and DEL to enable you to postpone the actions until the next reboot. This can be useful to replace files that are normally in use.


AllenAdministrator
(KiX Supporter)
2006-03-09 09:57 PM
Re: How to check if a file is in use

Wsing WSHPipe I was able to check and see if my PST file was open.

Code:
 
break on



$output=wshpipe('openfiles /query',1)
for each $line in $output
if instr($line,"pst")
? $line
endif
next

Function WshPipe($ShellCMD, OPTIONAL $NoEcho)
Dim $oExec, $Output
$oExec = CreateObject("WScript.Shell").Exec($ShellCMD)
If Not VarType($oExec)=9 $WshPipe="WScript.Shell Exec Unsupported" Exit 10 EndIf
$Output = $oExec.StdOut.ReadAll + $oExec.StdErr.ReadAll
If Not $NoEcho $Output Endif
$WshPipe=Split(Join(Split($Output,CHR(13)),''),CHR(10))
Exit($oExec.ExitCode)
EndFunction



AllenAdministrator
(KiX Supporter)
2006-03-09 10:08 PM
Re: How to check if a file is in use

I hate making UDFs that are dependent on external EXEs, but this works too...

Code:
 
break on
 
if isfileopen("outlook.pst")
"File is Open"
else
"File is not Open"
endif
 
 

function isfileopen($file)
dim $output, $line
$output=wshpipe('openfiles /query',1)
for each $line in $output
if right($line,len($file))=$file
$isfileopen=1
endif
next
endfunction
 

Function WshPipe($ShellCMD, OPTIONAL $NoEcho)
Dim $oExec, $Output
$oExec = CreateObject("WScript.Shell").Exec($ShellCMD)
If Not VarType($oExec)=9 $WshPipe="WScript.Shell Exec Unsupported" Exit 10 EndIf
$Output = $oExec.StdOut.ReadAll + $oExec.StdErr.ReadAll
If Not $NoEcho $Output Endif
$WshPipe=Split(Join(Split($Output,CHR(13)),''),CHR(10))
Exit($oExec.ExitCode)
EndFunction



LonkeroAdministrator
(KiX Master Guru)
2006-03-10 01:21 PM
Re: How to check if a file is in use

mart, yes, there is a udf but like les said before you, kixtart already supports it.
thus the udf is partly obsolete.

also, checking if file is in use is the simplest to do with simple move.
if move fails, it's in use, right?


Richard H.Administrator
(KiX Supporter)
2006-03-10 01:37 PM
Re: How to check if a file is in use

Quote:

if move fails, it's in use, right?



If the failure reason (@ERROR) is 32 or 33 then it's a reasonable assumption.

That's not a check if the file is in use though. A check would be non-intrusive.


LonkeroAdministrator
(KiX Master Guru)
2006-03-10 01:49 PM
Re: How to check if a file is in use

another simple test is to try open the file for writing.

richie, not sure if one can really "check" it.
is there some db that OS keeps and a coder can query about inuse files?


Richard H.Administrator
(KiX Supporter)
2006-03-10 04:54 PM
Re: How to check if a file is in use

Logic suggests that there must be a table in the kernel somewhere - whether it is exposed as a scriptable API or not is another question

LonkeroAdministrator
(KiX Master Guru)
2006-03-10 08:14 PM
Re: How to check if a file is in use

hmm...
I doubt your logic.
mine does not follow it


Howard Bullock
(KiX Supporter)
2006-03-10 08:41 PM
Re: How to check if a file is in use

Not sure if this helps, but you might be able to output the data and parse the file.

http://technet2.microsoft.com/WindowsServer/en/Library/3d240ddf-09a4-4b9c-b2fb-b4e057f049dc1033.mspx


LonkeroAdministrator
(KiX Master Guru)
2006-03-10 11:28 PM
Re: How to check if a file is in use

hoby, that's for shared files.

Sealeopard
(KiX Master)
2006-03-11 03:45 AM
Re: How to check if a file is in use

You could also use WMI:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/cim_datafile.asp
Attribute InUseCount

Supposedly, it is not 100% reliable.