Board index » delphi » Find out if a string is valid filename with path

Find out if a string is valid filename with path

Hi!

I have a little problem in one of my programs: I'm asking the user for
a filename to save data. Now I get a string, use it as an Argument in
ASSIGN, and if the string was a valid filename with path (or even
without), everything's fine. Of course, if the user gives something
like C:\\\\, the program crashes. How can I find out, if the user gave
a wrong string?

Thanks in advance...Philipp

 

Re:Find out if a string is valid filename with path


Hi,

on Tue, 11 Apr 2000 at 15:50:16 o'clock, Philipp Paland wrote:

Quote
> I have a little problem in one of my programs: I'm asking the user for
> a filename to save data. Now I get a string, use it as an Argument in
> ASSIGN, and if the string was a valid filename with path (or even
> without), everything's fine. Of course, if the user gives something
> like C:\\\\, the program crashes. How can I find out, if the user gave
> a wrong string?

Switch off runtime error checking, try to open the file, and see what
happens:

   Assign(f, fName);
   {$I-}
   Rewrite(f);
   {$I+}
   Err := IoResult;
   if Err <> 0 then
   begin
      Writeln('Could not open file ', fName, ', Error code = ', Err);
      { Exit, Halt, whatever... }
   end;
   { Proceed }

Structured programming fanatics will not like the Exit part. You could
replace it by an if..then..else construct, but IMHO the technique
described above is more readable.

 - Sebastian

Re:Find out if a string is valid filename with path


Quote
> > I have a little problem in one of my programs: I'm asking the user for
> > a filename to save data. Now I get a string, use it as an Argument in
> > ASSIGN, and if the string was a valid filename with path (or even
> > without), everything's fine. Of course, if the user gives something
> > like C:\\\\, the program crashes. How can I find out, if the user gave
> > a wrong string?

> Switch off runtime error checking, try to open the file, and see what
> happens:
>    Assign(f, fName);
>    {$I-}
>    Rewrite(f);
>    {$I+}
>    Err := IoResult;
>    if Err <> 0 then
>    begin
>       Writeln('Could not open file ', fName, ', Error code = ', Err);
>       { Exit, Halt, whatever... }
>    end;
>    { Proceed }

   He could also use FindFirst (with the returned string), and that could
have the advantage of not requiring the file to be in the execution's
Path.  It would, however, require that the file is _somewhere_ in the
Path...

Re:Find out if a string is valid filename with path


Quote
> > Switch off runtime error checking, try to open the file, and see what
> > happens:
> >    Assign(f, fName);
> >    {$I-}
> >    Rewrite(f);
> >    {$I+}
> >    Err := IoResult;
> >    if Err <> 0 then
> >    begin
> >       Writeln('Could not open file ', fName, ', Error code = ', Err);
> >       { Exit, Halt, whatever... }
> >    end;
> >    { Proceed }

>    He could also use FindFirst (with the returned string), and that could
> have the advantage of not requiring the file to be in the execution's
> Path.  It would, however, require that the file is _somewhere_ in the
> Path...

Best way is to use disksize on the drive first (fsplit to get the
driveletter), and if that goes ok then findfirst.

This will avoid problems with the DOS critical error handler.

--
Marco van de Voort (Mar...@stack.nl)

Pascal page on www.stack.nl/~marcov/xtdfpc.htm
Fortuneclone (written in Pascal with humor archive ) on
    www.stack.nl/~marcov/cookie.htm

Other Threads