Discussion:
Check if file is in use
(too old to reply)
Justin
2005-05-26 15:00:10 UTC
Permalink
Is there a way to check if a file is in use with vbscript?
D.R.
2005-05-27 14:58:22 UTC
Permalink
Maybe something like...



On Error Resume Next
Set go_log_chan = go_fso.OpenTextFile( gs_log_spec, ci_for_appending,
True )
Select Case Err.Number
Case 0
Case 70
Call s_error( cs_fac & "Failed to open log file `" & gs_log_spec & "`
for appending, another instance of this script is probably already
running..." )
Case Else
Call s_error( cs_fac & "Failed to open log file `" & gs_log_spec & "`
for appending..." )
End Select
On Error Goto 0
Post by Justin
Is there a way to check if a file is in use with vbscript?
Roland Hall
2005-05-27 17:29:41 UTC
Permalink
"D.R." <***@a.b.c> wrote in message news:%***@tk2msftngp13.phx.gbl...
: Maybe something like...
:
:
:
: On Error Resume Next
: Set go_log_chan = go_fso.OpenTextFile( gs_log_spec, ci_for_appending,
: True )
: Select Case Err.Number
: Case 0
: Case 70
: Call s_error( cs_fac & "Failed to open log file `" & gs_log_spec & "`
: for appending, another instance of this script is probably already
: running..." )
: Case Else
: Call s_error( cs_fac & "Failed to open log file `" & gs_log_spec & "`
: for appending..." )
: End Select
: On Error Goto 0

I'm wondering why you chose Error number 70, which is permission denied.

This will tell you the possibilities of errors for vbscript but I couldn't
find a 'File in use' explicitly. They don't seem to run past 32812. I
tested it out to a million.

On Error Resume Next
dim errnum, i
for i = 0 to 32812
errnum = i
Err.Raise errnum, "SomeObject"
if Err.Description <> "Unknown runtime error" and Err.Description <>
"Invalid procedure call or argument" then
wscript.echo "Error # " & CStr(Err.Number) & " " & Err.Description & "
"' & Err.Source
end if
Err.Clear ' Clear the error.
next

Here's the list:
Error # 6 Overflow
Error # 7 Out of memory
Error # 9 Subscript out of range
Error # 10 This array is fixed or temporarily locked
Error # 11 Division by zero
Error # 13 Type mismatch
Error # 14 Out of string space
Error # 17 Can't perform requested operation
Error # 28 Out of stack space
Error # 35 Sub or Function not defined
Error # 48 Error in loading DLL
Error # 51 Internal error
Error # 52 Bad file name or number
Error # 53 File not found
Error # 54 Bad file mode
Error # 55 File already open
Error # 57 Device I/O error
Error # 58 File already exists
Error # 61 Disk full
Error # 62 Input past end of file
Error # 67 Too many files
Error # 68 Device unavailable
Error # 70 Permission denied
Error # 71 Disk not ready
Error # 74 Can't rename with different drive
Error # 75 Path/File access error
Error # 76 Path not found
Error # 91 Object variable not set
Error # 92 For loop not initialized
Error # 94 Invalid use of Null
Error # 322 Can't create necessary temporary file
Error # 424 Object required
Error # 429 ActiveX component can't create object
Error # 430 Class doesn't support Automation
Error # 432 File name or class name not found during Automation operation
Error # 438 Object doesn't support this property or method
Error # 440 Automation error
Error # 445 Object doesn't support this action
Error # 446 Object doesn't support named arguments
Error # 447 Object doesn't support current locale setting
Error # 448 Named argument not found
Error # 449 Argument not optional
Error # 450 Wrong number of arguments or invalid property assignment
Error # 451 Object not a collection
Error # 453 Specified DLL function not found
Error # 455 Code resource lock error
Error # 457 This key is already associated with an element of this
collection
Error # 458 Variable uses an Automation type not supported in VBScript
Error # 462 The remote server machine does not exist or is unavailable
Error # 481 Invalid picture
Error # 500 Variable is undefined
Error # 501 Illegal assignment
Error # 502 Object not safe for scripting
Error # 503 Object not safe for initializing
Error # 504 Object not safe for creating
Error # 505 Invalid or unqualified reference
Error # 506 Class not defined
Error # 507 An exception occurred
Error # 1001 Out of memory
Error # 1002 Syntax error
Error # 1003 Expected ':'
Error # 1005 Expected '('
Error # 1006 Expected ')'
Error # 1007 Expected ']'
Error # 1010 Expected identifier
Error # 1011 Expected '='
Error # 1012 Expected 'If'
Error # 1013 Expected 'To'
Error # 1014 Expected 'End'
Error # 1015 Expected 'Function'
Error # 1016 Expected 'Sub'
Error # 1017 Expected 'Then'
Error # 1018 Expected 'Wend'
Error # 1019 Expected 'Loop'
Error # 1020 Expected 'Next'
Error # 1021 Expected 'Case'
Error # 1022 Expected 'Select'
Error # 1023 Expected expression
Error # 1024 Expected statement
Error # 1025 Expected end of statement
Error # 1026 Expected integer constant
Error # 1027 Expected 'While' or 'Until'
Error # 1028 Expected 'While', 'Until' or end of statement
Error # 1029 Expected 'With'
Error # 1030 Identifier too long
Error # 1031 Invalid number
Error # 1032 Invalid character
Error # 1033 Unterminated string constant
Error # 1034 Unterminated comment
Error # 1037 Invalid use of 'Me' keyword
Error # 1038 'loop' without 'do'
Error # 1039 Invalid 'exit' statement
Error # 1040 Invalid 'for' loop control variable
Error # 1041 Name redefined
Error # 1042 Must be first statement on the line
Error # 1043 Cannot assign to non-ByVal argument
Error # 1044 Cannot use parentheses when calling a Sub
Error # 1045 Expected literal constant
Error # 1046 Expected 'In'
Error # 1047 Expected 'Class'
Error # 1048 Must be defined inside a Class
Error # 1049 Expected Let or Set or Get in property declaration
Error # 1050 Expected 'Property'
Error # 1051 Number of arguments must be consistent across properties
specification
Error # 1052 Cannot have multiple default property/method in a Class
Error # 1053 Class initialize or terminate do not have arguments
Error # 1054 Property set or let must have at least one argument
Error # 1055 Unexpected 'Next'
Error # 1056 'Default' can be specified only on 'Property' or 'Function' or
'Sub'
Error # 1057 'Default' specification must also specify 'Public'
Error # 1058 'Default' specification can only be on Property Get
Error # 4096 Microsoft VBScript compilation error
Error # 4097 Microsoft VBScript runtime error
Error # 5016 Regular Expression object expected
Error # 5017 Syntax error in regular expression
Error # 5018 Unexpected quantifier
Error # 5019 Expected ']' in regular expression
Error # 5020 Expected ')' in regular expression
Error # 5021 Invalid range in character set
Error # 30000 EN
Error # 32766 True
Error # 32767 False
Error # 32768 OK
Error # 32769 Cancel
Error # 32770 Help
Error # 32811 Element not found
Error # 32812 The specified date is not available in the current locale's
calendar
--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
MSDN Library - http://msdn.microsoft.com/library/default.asp
Joe Earnest
2005-05-27 18:10:01 UTC
Permalink
Hi Roland,
Post by Roland Hall
: Maybe something like...
: On Error Resume Next
: Set go_log_chan = go_fso.OpenTextFile( gs_log_spec, ci_for_appending,
: True )
: Select Case Err.Number
: Case 0
: Case 70
: Call s_error( cs_fac & "Failed to open log file `" & gs_log_spec & "`
: for appending, another instance of this script is probably already
: running..." )
: Case Else
: Call s_error( cs_fac & "Failed to open log file `" & gs_log_spec & "`
: for appending..." )
: End Select
: On Error Goto 0
I'm wondering why you chose Error number 70, which is permission denied.
This will tell you the possibilities of errors for vbscript but I couldn't
find a 'File in use' explicitly. They don't seem to run past 32812. I
tested it out to a million.
On Error Resume Next
dim errnum, i
for i = 0 to 32812
errnum = i
Err.Raise errnum, "SomeObject"
if Err.Description <> "Unknown runtime error" and Err.Description <>
"Invalid procedure call or argument" then
wscript.echo "Error # " & CStr(Err.Number) & " " & Err.Description & "
"' & Err.Source
end if
Err.Clear ' Clear the error.
next
Error # 6 Overflow
Error # 7 Out of memory
Error # 9 Subscript out of range
Error # 10 This array is fixed or temporarily locked
Error # 11 Division by zero
Error # 13 Type mismatch
Error # 14 Out of string space
Error # 17 Can't perform requested operation
Error # 28 Out of stack space
Error # 35 Sub or Function not defined
Error # 48 Error in loading DLL
Error # 51 Internal error
Error # 52 Bad file name or number
Error # 53 File not found
Error # 54 Bad file mode
Error # 55 File already open
Error # 57 Device I/O error
Error # 58 File already exists
Error # 61 Disk full
Error # 62 Input past end of file
Error # 67 Too many files
Error # 68 Device unavailable
Error # 70 Permission denied
Error # 71 Disk not ready
Error # 74 Can't rename with different drive
Error # 75 Path/File access error
Error # 76 Path not found
Error # 91 Object variable not set
Error # 92 For loop not initialized
Error # 94 Invalid use of Null
Error # 322 Can't create necessary temporary file
Error # 424 Object required
Error # 429 ActiveX component can't create object
Error # 430 Class doesn't support Automation
Error # 432 File name or class name not found during Automation operation
Error # 438 Object doesn't support this property or method
Error # 440 Automation error
Error # 445 Object doesn't support this action
Error # 446 Object doesn't support named arguments
Error # 447 Object doesn't support current locale setting
Error # 448 Named argument not found
Error # 449 Argument not optional
Error # 450 Wrong number of arguments or invalid property assignment
Error # 451 Object not a collection
Error # 453 Specified DLL function not found
Error # 455 Code resource lock error
Error # 457 This key is already associated with an element of this
collection
Error # 458 Variable uses an Automation type not supported in VBScript
Error # 462 The remote server machine does not exist or is unavailable
Error # 481 Invalid picture
Error # 500 Variable is undefined
Error # 501 Illegal assignment
Error # 502 Object not safe for scripting
Error # 503 Object not safe for initializing
Error # 504 Object not safe for creating
Error # 505 Invalid or unqualified reference
Error # 506 Class not defined
Error # 507 An exception occurred
Error # 1001 Out of memory
Error # 1002 Syntax error
Error # 1003 Expected ':'
Error # 1005 Expected '('
Error # 1006 Expected ')'
Error # 1007 Expected ']'
Error # 1010 Expected identifier
Error # 1011 Expected '='
Error # 1012 Expected 'If'
Error # 1013 Expected 'To'
Error # 1014 Expected 'End'
Error # 1015 Expected 'Function'
Error # 1016 Expected 'Sub'
Error # 1017 Expected 'Then'
Error # 1018 Expected 'Wend'
Error # 1019 Expected 'Loop'
Error # 1020 Expected 'Next'
Error # 1021 Expected 'Case'
Error # 1022 Expected 'Select'
Error # 1023 Expected expression
Error # 1024 Expected statement
Error # 1025 Expected end of statement
Error # 1026 Expected integer constant
Error # 1027 Expected 'While' or 'Until'
Error # 1028 Expected 'While', 'Until' or end of statement
Error # 1029 Expected 'With'
Error # 1030 Identifier too long
Error # 1031 Invalid number
Error # 1032 Invalid character
Error # 1033 Unterminated string constant
Error # 1034 Unterminated comment
Error # 1037 Invalid use of 'Me' keyword
Error # 1038 'loop' without 'do'
Error # 1039 Invalid 'exit' statement
Error # 1040 Invalid 'for' loop control variable
Error # 1041 Name redefined
Error # 1042 Must be first statement on the line
Error # 1043 Cannot assign to non-ByVal argument
Error # 1044 Cannot use parentheses when calling a Sub
Error # 1045 Expected literal constant
Error # 1046 Expected 'In'
Error # 1047 Expected 'Class'
Error # 1048 Must be defined inside a Class
Error # 1049 Expected Let or Set or Get in property declaration
Error # 1050 Expected 'Property'
Error # 1051 Number of arguments must be consistent across properties
specification
Error # 1052 Cannot have multiple default property/method in a Class
Error # 1053 Class initialize or terminate do not have arguments
Error # 1054 Property set or let must have at least one argument
Error # 1055 Unexpected 'Next'
Error # 1056 'Default' can be specified only on 'Property' or 'Function' or
'Sub'
Error # 1057 'Default' specification must also specify 'Public'
Error # 1058 'Default' specification can only be on Property Get
Error # 4096 Microsoft VBScript compilation error
Error # 4097 Microsoft VBScript runtime error
Error # 5016 Regular Expression object expected
Error # 5017 Syntax error in regular expression
Error # 5018 Unexpected quantifier
Error # 5019 Expected ']' in regular expression
Error # 5020 Expected ')' in regular expression
Error # 5021 Invalid range in character set
Error # 30000 EN
Error # 32766 True
Error # 32767 False
Error # 32768 OK
Error # 32769 Cancel
Error # 32770 Help
Error # 32811 Element not found
Error # 32812 The specified date is not available in the current locale's
calendar
--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation -
http://msdn.microsoft.com/downloads/list/webdev.asp
MSDN Library - http://msdn.microsoft.com/library/default.asp
Runtime error #70 appears to be the error generated when permission is
denied (through the system) by an app that has the file open for exclusive
use and denies other apps access to it while it's open. I picked this up
from a script and discussion posted by Torgeir, several years back (sorry,
don't have a link for that). I've used it (and suggested it here on this NG
and in the WordPerfect NG), opening for both read and append, to test for
restrictions that the controlling app is placing through the system on the
open file. (Some apps completely bar access to a file in use, others let
you read, but not write to it, while others, such as many text editors, let
you open and modify the file with another program, even while its opened by
the app.) I suspect that you're correct, that error #70 could also be
generated for other permission-denied conditions (such as security
settings).

Regards,
Joe Earnest
Roland Hall
2005-05-27 21:52:30 UTC
Permalink
"Joe Earnest" wrote in message news:%***@tk2msftngp13.phx.gbl...
: Hi Roland,

Hey Joe...

: Runtime error #70 appears to be the error generated when permission is
: denied (through the system) by an app that has the file open for exclusive
: use and denies other apps access to it while it's open. I picked this up
: from a script and discussion posted by Torgeir, several years back (sorry,
: don't have a link for that). I've used it (and suggested it here on this
NG
: and in the WordPerfect NG), opening for both read and append, to test for
: restrictions that the controlling app is placing through the system on the
: open file. (Some apps completely bar access to a file in use, others let
: you read, but not write to it, while others, such as many text editors,
let
: you open and modify the file with another program, even while its opened
by
: the app.) I suspect that you're correct, that error #70 could also be
: generated for other permission-denied conditions (such as security
: settings).

I read an article that talked about x-referencing system errors (hex 8
characters) with vbscript errors. Is there really no way to determine an
actual 'File in use' error with vbscript where it cannot be another error?

From the example posted, it appears you just report the error if not 70 to
let the user/developer know what it is and then go searching.
--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
MSDN Library - http://msdn.microsoft.com/library/default.asp
Joe Earnest
2005-05-28 13:03:23 UTC
Permalink
Hi Roland,

[snipped]
Post by Roland Hall
: Hi Roland,
Hey Joe...
...
Post by Roland Hall
I read an article that talked about x-referencing system errors (hex 8
characters) with vbscript errors. Is there really no way to determine an
actual 'File in use' error with vbscript where it cannot be another error?
From the example posted, it appears you just report the error if not 70 to
let the user/developer know what it is and then go searching.
--
Roland Hall
I'm not aware of another way, but that certainly doesn't mean that there's
not a better way out there. I've only used the test where I have an
apparent problem with an app, and I'm troubleshooting whether it's that app
or a different app/service that's causing the problem. It provides enough
information in those circumstances.

Over the years I've noticed Windows Explorer reporting "permission denied"
for a host of in-use situations, so I've presumed that it is the generic
system error for in-use files, and that seems to be confirmed by MS's rather
obscure KB articles.

SysInternals' freeware FileMon and Handle (ProcessExplorer incorporates a
GUI version of Handle) provide much more detailed information, but I have no
idea how to get to that information with traditional script resources,
without the utilities.

http://www.sysinternals.com/ntw2k/utilities.shtml

I presume that one could capture Handle's output stream. The SysInternal
site notes that MS recommends Handle for file diagnosis in two KB articles:

KB 245068
Err Msg: Access is Denied.
You Don't Have Permissions or the File is in Use

(sounds good, but no real information)

http://support.microsoft.com/kb/q245068/

Even less useful for this point --

KB 276525:
Your Computer May Stop Responding
When Your Monitor Open Handles

http://support.microsoft.com/kb/q276525/

Joe Earnest

Loading...