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!
detecting if a file is open?
Moderators: JRL, Dorian (MJT support)
-
- Pro Scripter
- Posts: 149
- Joined: Tue Mar 23, 2004 9:11 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.
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.
Code: Select all
Let>WLN_NOCRLF=1
WriteLn>path\filename,wres,
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.
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â€
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 -
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 -

- Phil Pendlebury
- Automation Wizard
- Posts: 543
- Joined: Tue Jan 16, 2007 9:00 am
- Contact:
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.
It takes a fraction of second. Of course if your file is millions of lines of text it may take longer.
Phil Pendlebury - Linktree
- CyberCitizen
- Automation Wizard
- Posts: 724
- Joined: Sun Jun 20, 2004 7:06 am
- Location: Adelaide, South Australia
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.
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
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.
When used as described, this method can do no harm.
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.My only concern is by doing this it will change the file properties (due to data being written)
When used as described, this method can do no harm.