Page 1 of 1

In use file/folder

Posted: 18 Nov 2013, 02:20
by Guest
Is there a way to check that?

Re: In use file/folder

Posted: 28 Nov 2013, 01:33
by DataLife

Re: In use file/folder

Posted: 28 Nov 2013, 08:31
by HotKeyIt
For file try this:

Code: Select all

If !f:=FileOpen("C:\Temp\File.txt","w")
  MsgBox Could Not Open File
else f.Close(),f:=""
With regards to Folder, what would be the case when it is in Use? E.g. any of the file is in use?

Re: In use file/folder

Posted: 28 Nov 2013, 08:43
by SKAN
HotKeyIt wrote:With regards to Folder, what would be the case when it is in Use?
An open command prompt on a folder will make it undeletable.

Re: In use file/folder

Posted: 28 Nov 2013, 10:14
by DataLife
This works with folders.

MyScripts is the name of a folder.

Code: Select all

IfWinExist,MyScripts
	MsgBox yes
else
	MsgBox no

Re: In use file/folder

Posted: 28 Nov 2013, 10:24
by DataLife
HotKeyIt wrote:For file try this:

Code: Select all

If !f:=FileOpen("C:\Temp\File.txt","w")
  MsgBox Could Not Open File
else f.Close(),f:=""
I made a folder called temp on the root of c:\
then created a text file called file.txt
The script exited with no message with no regard to if the file was opened or not. I also tried it on an existing text file on my desktop.

I am using windows 7 home premium. Ahk version 1.1.13.01 32-bit Ansi

Re: In use file/folder

Posted: 28 Nov 2013, 15:36
by Guest
Thanks for trying to help guys,

I need to overwrite all files and folders in a folder/dir only if no files inside that folder/dir/sub folder is in use.
None of the above works.
My solution for now is to try and rename that folder then check errorlevel and then rename it back. But thats too intrusive in my opinion.

Re: In use file/folder

Posted: 28 Nov 2013, 18:08
by HotKeyIt
Sorry example was not correct :oops:
See documentation for FileOpen.

Code: Select all

fa:=FileOpen("C:\file.txt","r-rwd") ; comment this line to see the difference
If !(f:=FileOpen("C:\file.txt","r-rwd"))
  MsgBox Could Not Open File
else MsgBox OK

Re: In use file/folder

Posted: 29 Nov 2013, 02:24
by Guest
@ HotKeyIt,

Doesn't work.

Re: In use file/folder

Posted: 14 Dec 2013, 17:05
by HotKeyIt
What do you mean?
Does it not say OK here?

Code: Select all

If !(f:=FileOpen(A_ScriptFullPath,"r-rwd"))
  MsgBox Could Not Open File
else MsgBox OK
And "Could Not Open File" here?

Code: Select all

fa:=FileOpen(A_ScriptFullPath,"r-rwd")
If !(f:=FileOpen(A_ScriptFullPath,"r-rwd"))
  MsgBox Could Not Open File
else MsgBox OK

Re: In use file/folder

Posted: 15 Dec 2013, 03:41
by Guest
@HotKeyIt,

It just doesn't work, not consistent.

Re: In use file/folder

Posted: 15 Dec 2013, 05:18
by noname
You can use sysinternals handle.exe and parse the the result.There are plenty of option parameters to configure it.

http://technet.microsoft.com/en-us/sysi ... 96655.aspx

Re: In use file/folder

Posted: 15 Dec 2013, 06:24
by HotKeyIt
Guest wrote:@HotKeyIt,

It just doesn't work, not consistent.
You probably mean that not every program is locking the file, e.g. You can edit and delete a file while it is open in Notepad.

Re: In use file/folder

Posted: 15 Dec 2013, 12:16
by Wade Hatler
Agree on using handle.exe as mentioned above. I've been using it for years, and it works in most but not all cases. It always seems to work if a file is being held open by the local file, but doesn't always work when a file is open across the network. It is however the best solution I've found.

One handy trick on it is to run it against a folder. If you do that, it will list all files in that folder that are currently open, which is what I do most of the time. I parse the results, and give the user a chance to close the handle or kill the offending process, then try again in a loop until it's done. I'd give you the code but it's 4NT code, so probably not that useful. Pretty easy to run it with RunWait and parse the results in AHK.