detecting if a file is open?

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
rullbandspelare
Pro Scripter
Posts: 149
Joined: Tue Mar 23, 2004 9:11 pm

detecting if a file is open?

Post by rullbandspelare » Tue Oct 15, 2013 7:10 pm

Hi!
I have many many files that end up in a folder. The files are to be manipulated and moved to another location.

The problem is that some of theese files are open and beeing written to.
So i only whant to manipulate files that are closed.

What is the best way to do this?


Thanks for any input!

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Tue Oct 15, 2013 9:36 pm

Write "nothing" to the file and look at the result. Note in the following code there is nothing after the last comma in the Writeln> statement.

Code: Select all

Let>WLN_NOCRLF=1
WriteLn>path\filename,wres,
If the value of variable wres is anything except "0". The file is inaccessable.

Be absolutely certain to set WLN_NOCRLF=1 otherwise you will write a CRLF to the file and thus alter it.

This would have been a good puzzler.

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Fri Oct 18, 2013 4:34 pm

Hi JRL,

I agree. This little check could be written into a handy subroutine to provide the equivalent of an IfFileAccessible> function.

Ideally, you would run the subroutine and not have to worry about what the system variable WLN_NOCRLF was set to... what I mean is... running the subroutine should not change it from what it was before running the subroutine.

However, we NEED to be sure we have WLN_NOCRLF=1 before we run this or we'll be writing a CRLF to the file so... inside the subroutine, I would:

- save the current value of system variable WLN_NOCRLF
- set the value of WLN_NOCRLF to 1
- run the line: WriteLn>path\filename,wres,
- set the value of WLN_NOCRLF back to whatever it was before, using the saved value
- inspect/return the value of variable wres to indicate the file is Accessible or not

However, I also note that the system variable WLN_NOCRLF is not defined automatically when a script runs... so we'd have to use the Assigned> command to determine first if WLN_NOCRLF has actually been assigned. If not, then we'd set WLN_NOCRLF to 0... then record it was 0 (so we can restore it later)... then we set it to 1, run the WriteLn>, then restore it to 0.

Hmm... now I’m thinking that a new function that just does all this automatically for us would be handy...

IfFileAccessible>

Any thoughts anyone?

Here’s another thought... what happens if we run any of the following lines?

Let>WLN_NOCRLF=2
Let>WLN_NOCRLF=-1
Let>WLN_NOCRLF=
Let>WLN_NOCRLF={“A STRINGâ€
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

User avatar
Phil Pendlebury
Automation Wizard
Posts: 543
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Post by Phil Pendlebury » Sat Oct 19, 2013 8:14 pm

I do this by writing specific txt to the file and then checking for that text. If the text I wrote exists then delete that line and continue.

It takes a fraction of second. Of course if your file is millions of lines of text it may take longer.
Phil Pendlebury - Linktree

User avatar
CyberCitizen
Automation Wizard
Posts: 724
Joined: Sun Jun 20, 2004 7:06 am
Location: Adelaide, South Australia

Post by CyberCitizen » Sun Oct 20, 2013 11:57 pm

JRL's script is an excellent idea. And I agree this would make a wonderful command for us.

My only concern is by doing this it will change the file properties (due to data being written). This might not matter for some, but it could have an effect with programs that use this information for backups etc.

Just a concern but I still like the idea of it as a command. At present I have saved it as a snippet.
FIREFIGHTER

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Mon Oct 21, 2013 5:04 am

The method is from MSDN so not really an original idea. They of course used vbscript but the WriteLn> function is way easier to use in Macro Scheduler.
My only concern is by doing this it will change the file properties (due to data being written)
What this does is attempt to open the file for writing, if the attempt succeeds the result variable is zero, if the attempt fails the result variable is greater than zero. But since no data is written to the file, nothing is updated. The date / time remains unchanged, if you have the archive bit turned off that remains unchanged. The file is really untouched. Also when the WriteLn> function line is complete, the file is no longer open for writing by Macro Scheduler.

When used as described, this method can do no harm.

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts