1. How check for "path/file exists"?

Wolf, all,

How can I check for whether a specified "path/file" exists??

I'm trying to use Wolf's "jumpto" (or perhaps his "ShellExecute2") to run
another program from within my program (to jump to it); when this is a
program with a "stable" path, no problem.  But I'm wanting to be able to
jump to Wolf's tutorial, which isn't always at a known place in the Euphoria
folder, so I need to be able to *test* for whether the path/file I
tentatively specify actually exists or not, so I can activate the "open
file" dialog if it doesn't.

I changed Wolf's "jumpto" to a function, thinking to use the return as a
test, but while it worked if the path was correct, when the path/file wasn't
correct it just bombed & output an error message about "can't open file".

Any thoughts?

Dan Moyer

new topic     » topic index » view message » categorize

2. Re: How check for "path/file exists"?

On 3 Sep 2000, at 19:13, Dan B Moyer wrote:

> Wolf, all,
>
> How can I check for whether a specified "path/file" exists??
>
> I'm trying to use Wolf's "jumpto" (or perhaps his "ShellExecute2") to run
> another program from within my program (to jump to it); when this is a
> program with a "stable" path, no problem.  But I'm wanting to be able to
> jump to Wolf's tutorial, which isn't always at a known place in the Euphoria
> folder, so I need to be able to *test* for whether the path/file I
> tentatively specify actually exists or not, so I can activate the "open
> file" dialog if it doesn't.
>
> I changed Wolf's "jumpto" to a function, thinking to use the return as a
> test, but while it worked if the path was correct, when the path/file wasn't
> correct it just bombed & output an error message about "can't open file".
>
> Any thoughts?

Try opening the file for a read before you try to exec it. If it cannot be
opened, then it's
not there or it's locked,, either way, you can't run it.

Kat

new topic     » goto parent     » topic index » view message » categorize

3. Re: How check for "path/file exists"?

Kat,

Ok, I did consider that, but thought there might be a better or more
"elegant" way.  Thanks.

Dan


----- Original Message -----
From: "Kat" <gertie at PELL.NET>
To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
Sent: Sunday, September 03, 2000 8:35 PM
Subject: Re: How check for "path/file exists"?


> On 3 Sep 2000, at 19:13, Dan B Moyer wrote:
>
> > Wolf, all,
> >
> > How can I check for whether a specified "path/file" exists??
> >
> > I'm trying to use Wolf's "jumpto" (or perhaps his "ShellExecute2") to
run
> > another program from within my program (to jump to it); when this is a
> > program with a "stable" path, no problem.  But I'm wanting to be able to
> > jump to Wolf's tutorial, which isn't always at a known place in the
Euphoria
> > folder, so I need to be able to *test* for whether the path/file I
> > tentatively specify actually exists or not, so I can activate the "open
> > file" dialog if it doesn't.
> >
> > I changed Wolf's "jumpto" to a function, thinking to use the return as a
> > test, but while it worked if the path was correct, when the path/file
wasn't
> > correct it just bombed & output an error message about "can't open
file".
> >
> > Any thoughts?
>
> Try opening the file for a read before you try to exec it. If it cannot be
opened, then it's
> not there or it's locked,, either way, you can't run it.
>
> Kat

new topic     » goto parent     » topic index » view message » categorize

4. Re: How check for "path/file exists"?

Kat,

Tried it, but when no file exists, I get a dos window with message "can't
open" filename instead of going on through the if-then.

Here's what I wrote:

--<code>
integer filethere

filethere = open("c:\\Euphoria\\Win32Lib\\A new Tutorial\\Tutor3.exw","r")
if filethere != -1 then  -- is a file there
    close(filethere)
    Jump_to(
    -- passing YOUR main window's id:
    MainWindow,
    -- with the program to call:
    "exw.exe",
    -- this is MY current 'tutorials' default path !!
    "c:\\Euphoria\\Win32Lib\\A new Tutorial\\",
    -- the parameter to pass to exw.exe:
    "xTutor3.exw",  -- for purpose of test, there is no such file
    -- and last, the 'show' parameter:
    SW_SHOWDEFAULT )
else

end if
<code ends>

Dan

----- Original Message -----
From: "Kat" <gertie at PELL.NET>
To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
Sent: Sunday, September 03, 2000 8:35 PM
Subject: Re: How check for "path/file exists"?


> On 3 Sep 2000, at 19:13, Dan B Moyer wrote:
>
> > Wolf, all,
> >
> > How can I check for whether a specified "path/file" exists??
> >
> > I'm trying to use Wolf's "jumpto" (or perhaps his "ShellExecute2") to
run
> > another program from within my program (to jump to it); when this is a
> > program with a "stable" path, no problem.  But I'm wanting to be able to
> > jump to Wolf's tutorial, which isn't always at a known place in the
Euphoria
> > folder, so I need to be able to *test* for whether the path/file I
> > tentatively specify actually exists or not, so I can activate the "open
> > file" dialog if it doesn't.
> >
> > I changed Wolf's "jumpto" to a function, thinking to use the return as a
> > test, but while it worked if the path was correct, when the path/file
wasn't
> > correct it just bombed & output an error message about "can't open
file".
> >
> > Any thoughts?
>
> Try opening the file for a read before you try to exec it. If it cannot be
opened, then it's
> not there or it's locked,, either way, you can't run it.
>
> Kat

new topic     » goto parent     » topic index » view message » categorize

5. Re: How check for "path/file exists"?

Nevermind, I found it, sigh.  Tested for a file that *does* exist, so it
then tried to open file that *doesn't*, which failed. argh.

Dan


----- Original Message -----
From: "Dan B Moyer" <DANMOYER at prodigy.net>
To: "Euphoria Programming for MS-DOS" <EUPHORIA at LISTSERV.MUOHIO.EDU>
Sent: Sunday, September 03, 2000 9:29 PM
Subject: Re: How check for "path/file exists"?


> Kat,
>
> Tried it, but when no file exists, I get a dos window with message "can't
> open" filename instead of going on through the if-then.
>
> Here's what I wrote:
>
> --<code>
> integer filethere
>
> filethere = open("c:\\Euphoria\\Win32Lib\\A new Tutorial\\Tutor3.exw","r")
> if filethere != -1 then  -- is a file there
>     close(filethere)
>     Jump_to(
>     -- passing YOUR main window's id:
>     MainWindow,
>     -- with the program to call:
>     "exw.exe",
>     -- this is MY current 'tutorials' default path !!
>     "c:\\Euphoria\\Win32Lib\\A new Tutorial\\",
>     -- the parameter to pass to exw.exe:
>     "xTutor3.exw",  -- for purpose of test, there is no such file
>     -- and last, the 'show' parameter:
>     SW_SHOWDEFAULT )
> else
>
> end if
> <code ends>
>
> Dan
>
> ----- Original Message -----
> From: "Kat" <gertie at PELL.NET>
> To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
> Sent: Sunday, September 03, 2000 8:35 PM
> Subject: Re: How check for "path/file exists"?
>
>
> > On 3 Sep 2000, at 19:13, Dan B Moyer wrote:
> >
> > > Wolf, all,
> > >
> > > How can I check for whether a specified "path/file" exists??
> > >
> > > I'm trying to use Wolf's "jumpto" (or perhaps his "ShellExecute2") to
> run
> > > another program from within my program (to jump to it); when this is a
> > > program with a "stable" path, no problem.  But I'm wanting to be able
to
> > > jump to Wolf's tutorial, which isn't always at a known place in the
> Euphoria
> > > folder, so I need to be able to *test* for whether the path/file I
> > > tentatively specify actually exists or not, so I can activate the
"open
> > > file" dialog if it doesn't.
> > >
> > > I changed Wolf's "jumpto" to a function, thinking to use the return as
a
> > > test, but while it worked if the path was correct, when the path/file
> wasn't
> > > correct it just bombed & output an error message about "can't open
> file".
> > >
> > > Any thoughts?
> >
> > Try opening the file for a read before you try to exec it. If it cannot
be
> opened, then it's
> > not there or it's locked,, either way, you can't run it.
> >
> > Kat
>

new topic     » goto parent     » topic index » view message » categorize

6. Re: How check for "path/file exists"?

--returns 1 if file exists, 0 if it doesn't
global function file_exists(sequence fpath)
        integer a
        a=open(fpath,"r")
        if a=-1 then
           --not found
           a=0
        else
           --found
           close(a)
           a=1
        end if
        return a
end function

new topic     » goto parent     » topic index » view message » categorize

7. Re: How check for "path/file exists"?

TESTING FOR A VALID PATH

Dan,

  The problem with opening a file to test for validity is that you
  can't open a directory name, so here's what i use:

--------------------------------------------------------------

   --  Path Validation Check  --

--  checks for valid directory or filename

--------------------------------------------------------------

include file.e
include get.e --for this sample only

atom retv,ink

function ValidatePath(sequence TargetPathName,sequence TargetFileName)

  object DirEntries

  DirEntries=dir(TargetPathName&TargetFileName)
  if atom(DirEntries) then
    --doesnt exist
    return 0
  else
    return 1
  end if
end function

--testing for a valid specific filename:
?retv

--testing for a valid directory name:
?retv


printf(1,"%s\n",{"Press a key..."})

ink=wait_key()

----------------------------------------------------------------

Good luck.
  --Al

new topic     » goto parent     » topic index » view message » categorize

8. Re: How check for "path/file exists"?

Huh?

I did it & it worked as far as I could tell.  I did this:

fileHandle = open(DefaultPath & DefaultFileName, "r")
if fileHandle != -1 then  -- tutorial is in default directory:

Dan



----- Original Message -----
From: "Al Getz" <xaxo at AOL.COM>
To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
Sent: Monday, September 04, 2000 3:46 AM
Subject: Re: How check for "path/file exists"?


> TESTING FOR A VALID PATH
>
> Dan,
>
>   The problem with opening a file to test for validity is that you
>   can't open a directory name, so here's what i use:
>
> --------------------------------------------------------------
>
>    --  Path Validation Check  --
>
> --  checks for valid directory or filename
>
> --------------------------------------------------------------
>
> include file.e
> include get.e --for this sample only
>
> atom retv,ink
>
> function ValidatePath(sequence TargetPathName,sequence TargetFileName)
>
>   object DirEntries
>
>   DirEntries=dir(TargetPathName&TargetFileName)
>   if atom(DirEntries) then
>     --doesnt exist
>     return 0
>   else
>     return 1
>   end if
> end function
>
> --testing for a valid specific filename:
> retv=ValidatePath("c:\\euphoria\\projects\\tests\\","test.txt")
> ?retv
>
> --testing for a valid directory name:
> retv=ValidatePath("c:\\euphoria\\projects\\emptydir\\","*.*")
> ?retv
>
>
> printf(1,"%s\n",{"Press a key..."})
>
> ink=wait_key()
>
> ----------------------------------------------------------------
>
> Good luck.
>   --Al

new topic     » goto parent     » topic index » view message » categorize

9. Re: How check for "path/file exists"?

Skoda,

Thanks, that's close to what I did, except I didn't put it into a function,
just put it directely in the code since I only used it once.

Dan

----- Original Message -----
From: "Skoda" <tone.skoda at SIOL.NET>
To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
Sent: Monday, September 04, 2000 2:17 AM
Subject: Re: How check for "path/file exists"?


> --returns 1 if file exists, 0 if it doesn't
> global function file_exists(sequence fpath)
>         integer a
>         a=open(fpath,"r")
>         if a=-1 then
>            --not found
>            a=0
>         else
>            --found
>            close(a)
>            a=1
>         end if
>         return a
> end function

new topic     » goto parent     » topic index » view message » categorize

10. Re: How check for "path/file exists"?

Dan,

  Glad it worked for you.  My code was used to test for a valid
filename OR directory name.  You can't open a directory name with
'open()' so you need another way to test for that.  If your only
testing for valid filenames i guess it doesnt matter.

--Al

new topic     » goto parent     » topic index » view message » categorize

11. Re: How check for "path/file exists"?

Al,

Oh, ok, now I understand: you're providing for making a specific check for
whether a *directory* exists or not.  I didn't really need that, as what I
am doing is asserting a "default" pathname for Wolf's Tutorial, and if it
isn't there (which I can & do check with open filename), then I open the
"open file" dialog to find it & then use it.

But that *is* an interesting routine you shared with us!  :)

Dan


----- Original Message -----
From: "Al Getz" <xaxo at AOL.COM>
To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
Sent: Monday, September 04, 2000 3:46 AM
Subject: Re: How check for "path/file exists"?


> TESTING FOR A VALID PATH
>
> Dan,
>
>   The problem with opening a file to test for validity is that you
>   can't open a directory name, so here's what i use:
>
> --------------------------------------------------------------
>
>    --  Path Validation Check  --
>
> --  checks for valid directory or filename
>
> --------------------------------------------------------------
>
> include file.e
> include get.e --for this sample only
>
> atom retv,ink
>
> function ValidatePath(sequence TargetPathName,sequence TargetFileName)
>
>   object DirEntries
>
>   DirEntries=dir(TargetPathName&TargetFileName)
>   if atom(DirEntries) then
>     --doesnt exist
>     return 0
>   else
>     return 1
>   end if
> end function
>
> --testing for a valid specific filename:
> retv=ValidatePath("c:\\euphoria\\projects\\tests\\","test.txt")
> ?retv
>
> --testing for a valid directory name:
> retv=ValidatePath("c:\\euphoria\\projects\\emptydir\\","*.*")
> ?retv
>
>
> printf(1,"%s\n",{"Press a key..."})
>
> ink=wait_key()
>
> ----------------------------------------------------------------
>
> Good luck.
>   --Al

new topic     » goto parent     » topic index » view message » categorize

12. Re: How check for "path/file exists"?

A simpler version:

function ValidatePath(sequence TargetPathName)
    return sequence(dir(TargetPathName))
end function

No need to separate file and path just so the function can recombine them.

-- Mike Nelson

----- Original Message -----
From: "Al Getz" <xaxo at AOL.COM>
To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
Sent: Monday, September 04, 2000 3:46 AM
Subject: Re: How check for "path/file exists"?


> TESTING FOR A VALID PATH
>
> Dan,
>
>   The problem with opening a file to test for validity is that you
>   can't open a directory name, so here's what i use:
>
> --------------------------------------------------------------
>
>    --  Path Validation Check  --
>
> --  checks for valid directory or filename
>
> --------------------------------------------------------------
>
> include file.e
> include get.e --for this sample only
>
> atom retv,ink
>
> function ValidatePath(sequence TargetPathName,sequence TargetFileName)
>
>   object DirEntries
>
>   DirEntries=dir(TargetPathName&TargetFileName)
>   if atom(DirEntries) then
>     --doesnt exist
>     return 0
>   else
>     return 1
>   end if
> end function
>
> --testing for a valid specific filename:
> retv=ValidatePath("c:\\euphoria\\projects\\tests\\","test.txt")
> ?retv
>
> --testing for a valid directory name:
> retv=ValidatePath("c:\\euphoria\\projects\\emptydir\\","*.*")
> ?retv
>
>
> printf(1,"%s\n",{"Press a key..."})
>
> ink=wait_key()
>
> ----------------------------------------------------------------
>
> Good luck.
>   --Al

new topic     » goto parent     » topic index » view message » categorize

13. Re: How check for "path/file exists"?

Al,

Turns out I did need have them separate, because Wolf's "jump to" has
separate parameter fields for path & file .

Dan
----- Original Message -----
From: "Michael Nelson" <MichaelANelson at WORLDNET.ATT.NET>
To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
Sent: Monday, September 04, 2000 7:32 AM
Subject: Re: How check for "path/file exists"?


> A simpler version:
>
> function ValidatePath(sequence TargetPathName)
>     return sequence(dir(TargetPathName))
> end function
>
> No need to separate file and path just so the function can recombine them.
>
> -- Mike Nelson
>
> ----- Original Message -----
> From: "Al Getz" <xaxo at AOL.COM>
> To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
> Sent: Monday, September 04, 2000 3:46 AM
> Subject: Re: How check for "path/file exists"?
>
>
> > TESTING FOR A VALID PATH
> >
> > Dan,
> >
> >   The problem with opening a file to test for validity is that you
> >   can't open a directory name, so here's what i use:
> >
> > --------------------------------------------------------------
> >
> >    --  Path Validation Check  --
> >
> > --  checks for valid directory or filename
> >
> > --------------------------------------------------------------
> >
> > include file.e
> > include get.e --for this sample only
> >
> > atom retv,ink
> >
> > function ValidatePath(sequence TargetPathName,sequence TargetFileName)
> >
> >   object DirEntries
> >
> >   DirEntries=dir(TargetPathName&TargetFileName)
> >   if atom(DirEntries) then
> >     --doesnt exist
> >     return 0
> >   else
> >     return 1
> >   end if
> > end function
> >
> > --testing for a valid specific filename:
> > retv=ValidatePath("c:\\euphoria\\projects\\tests\\","test.txt")
> > ?retv
> >
> > --testing for a valid directory name:
> > retv=ValidatePath("c:\\euphoria\\projects\\emptydir\\","*.*")
> > ?retv
> >
> >
> > printf(1,"%s\n",{"Press a key..."})
> >
> > ink=wait_key()
> >
> > ----------------------------------------------------------------
> >
> > Good luck.
> >   --Al

new topic     » goto parent     » topic index » view message » categorize

14. Re: How check for "path/file exists"?

Dan:

(that's me, always late)

----- Original Message -----
From: Dan B Moyer <DANMOYER at PRODIGY.NET>
To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
Sent: Monday, September 04, 2000 1:29 AM
Subject: Re: How check for "path/file exists"?


> --<code>
> integer filethere
>
> filethere = open("c:\\Euphoria\\Win32Lib\\A new Tutorial\\Tutor3.exw","r")
> if filethere != -1 then  -- is a file there
>     close(filethere)
>     Jump_to(
>     -- passing YOUR main window's id:
>     MainWindow,
>    .....etc. etc.....
> <code ends>
>
> Dan
>

Try it the other way round:
  if filethere = -1 then screamforhelp() else processhappily();

or maybe even the Perl way:
  open(file) or die "Someone swiped my file!";

Gerardo E. Brandariz


_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu