Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble with FileExists method

Status
Not open for further replies.

vbscoded

Technical User
May 4, 2008
3
US
Hi VBS Forum,

This problem has been plaguing me for quite some time and I'd really like to get to the bottom of it. I really hope someone can help me.

The problem is, whenever I run the following code, I get a false-negative for the first conditional statement. Since I know the file I'm running the FileExists method against is right there on my hard drive and I have administrative rights to it, what could be the problem?
Code:
Set objfso = CreateObject( "Scripting.FileSystemObject" )
   If objfso.FileExists( strsfxcllogsrc ) Then
      If reftpgetsccss.Test( strsfxcllog ) = True Then
         make a subroutine call
      Else
         WScript.Echo "Error: Download failed!"
         objfso = nothing
         WScript.Quit
      End If   
   Else
      WScript.Echo "Error: Download failed!"
      objfso = nothing
      WScript.Quit
   End If

Thanks in advance for your time!
 
try this

Code:
 If Dir( strsfxcllogsrc ) <> "" Then

-David
2006, 2007 & 2008 Microsoft Most Valuable Professional (VB)
2006 Dell Certified System Professional (CSP)
 
>what could be the problem?
[1] Comment out your "on error resume next" in the script. That is the problem. False negative is an illusion.
[2] This line is by itself enough to trigger error of its own. You don't see it means you have "on error" directive as said in [1].
>objfso = nothing
[tt][red]set[/red] objfso = nothing[/tt]
 
wscript.echo strsfxcllogsrc right before you use it. Look carefully at what you are asking the script to find.
 
Also, the way it's written, you wouldn't know which statement was causing the issue, objfso.FileExists or reftpgetsccss.Test( strsfxcllog ) = True. A false from either of these would result in the identical "Error: Download failed!" messages.

Oh, and Tsuji is correct, the code won't even run without cleaning up the syntax of objfso declarations.
 
Thanks for responses! I will try them and post my results.
 
Hi All,

dglienna said:

try this
Code:
If Dir( strsfxcllogsrc ) <> "" Then
Error: [red]Microsoft VBScript runtime error: Variable is undefined: 'Dir'[/red]

tsuji said:

[1] Comment out your "on error resume next" in the script. That is the problem. False negative is an illusion.
Code:
on error resume next
was already commented out, but
Code:
objfso = nothing
had been deleted from my code before I posted.

[2] This line is by itself enough to trigger error of its own. You don't see it means you have "on error" directive as said in [1].
Sorry, my bad, what I posted was not what I had in my code.

Jerz said:

Look carefully at what you are asking the script to find.
Code:
wscript.echo strsfxcllogsrc
This shows the correct value: C:\sfxcllog.txt

Also, the way it's written, you wouldn't know which statement was causing the issue,
Code:
objfso.FileExists
or
Code:
reftpgetsccss.Test( strsfxcllog ) = True
A false from either of these would result in the identical "Error: Download failed!" messages.[/i]
Excellent point! I changed the code to include a
statement to echo the value of the strsfxcllog variable.
When I did this, the first conditional statement was
true and the second false. The output from the
abovementioned echo statement was the same as the
contents of the log file. It's for this reason I
believe, there are characters being added somewhere
-where, I don't know- causing my regular expression to
in the second conditional statement to fail.

Regular Expression:
Code:
reftpgetsccss.Pattern = "\s\d\sfiles"
Text I'm trying to match verbatim, as it is printed on
stdout (straight copy/mark and paste to this post):
2008-05-07 11:23:37, 00001: Transfer(00B7F708): SUMMARY: Transferred 1 files suc
cessfully.
I ran this text through my regular expression using
RegExDesigner.NET and it matched the text perfectly.
But when I run my script, the second conditional
statement is where if fails.

I'm going to try the following:
Code:
if objfso.fileexists( strsfxcllogsrc ) = true then
    set objread = objfso.opentextfile( strsfxcllogsrc, forread )
    do until objread.atendofstream
        strsfxcllog = objread.readline
        if reftpgetsccss.test( strsfxcllog ) = true then
            call mkupscnfg( upssbntmsk, upsgtwy )
        else
            WScript.Echo "Error: Download failed!"
            Set objfso = nothing
            WScript.Quit
        end if
    loop
else
    WScript.Echo "Error: no log file!"
    Set objfso = nothing
    WScript.Quit
end if

If the above doesn't work then I'll modify it with this:
Code:
if objfso.fileexists( strsfxcllogsrc ) = true then
    set objread = objfso.opentextfile( strsfxcllogsrc, forread )
    do until objread.atendofstream
        if reftpgetsccss.test( objread.readline ) = true then
            call mkupscnfg( upssbntmsk, upsgtwy )
        else
            WScript.Echo "Error: Download failed!"
            Set objfso = nothing
            WScript.Quit
        end if
    loop
else
    WScript.Echo "Error: no log file!"
    Set objfso = nothing
    WScript.Quit
end if
 
>Microsoft VBScript runtime error: Variable is undefined: 'Dir'

Quite so. dglienna is confusing VBScript with VB/VBA. VBScript does not support the Dir command
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top