Thread overview
[Help] - Determining if a file or directory exists
Dec 14, 2003
Andrew Edwards
Dec 14, 2003
Lars Ivar Igesund
Dec 14, 2003
Charles
Dec 14, 2003
Andrew Edwards
Dec 14, 2003
Andrew Edwards
Dec 14, 2003
Charles
Dec 15, 2003
Sean L. Palmer
December 14, 2003
The following program does not work because there is no exist() defined. How would I go about defining such a function or can this be included in std.file? Thanks for your assistance.

Andrew

==================

import std.stream;

void main ()
{
  char[] dirname = "pubs";
  char[] filename = "monitor.bat";

  chdir("c:\\");
  if(exist(dirname) && exist(dirname~"\\"~filename)) //can we have this in
file.d?
  {
    stdout.writeString("The pubs directory exists and ");
    stdout.writeLine("monitor.dat exists in the pubs directory.");
  }
  else
  {
    mkdir(dirname);
    chdir(dirname);
    write(filename, "ViewSonic GS790");
  }
}


December 14, 2003
With the current functions in std, I think it is easiest to implement exist by trying to read the file and catch the exception.

bool exist(char [] file)
{
  bool result = true;
  try {
    read(file);
  }
  catch (FileException e) {
    result = false;
  }
  return result;
}

Lars Ivar Igesund

"Andrew Edwards" <edwardsac@spamfreeusa.com> wrote in message news:bri5v9$2l06$1@digitaldaemon.com...
> The following program does not work because there is no exist() defined.
How
> would I go about defining such a function or can this be included in std.file? Thanks for your assistance.
>
> Andrew
>
> ==================
>
> import std.stream;
>
> void main ()
> {
>   char[] dirname = "pubs";
>   char[] filename = "monitor.bat";
>
>   chdir("c:\\");
>   if(exist(dirname) && exist(dirname~"\\"~filename)) //can we have this in
> file.d?
>   {
>     stdout.writeString("The pubs directory exists and ");
>     stdout.writeLine("monitor.dat exists in the pubs directory.");
>   }
>   else
>   {
>     mkdir(dirname);
>     chdir(dirname);
>     write(filename, "ViewSonic GS790");
>   }
> }
>
>


December 14, 2003
Don't mean to sound snotty, but seeing MW's report on D's speed of exception handling, I think a better alternative would be to use stat on linux, and GetFileAttributes on Windows.

Pseudo code for windows, I'm actually installing a Linux box now so I'll try to work this out on Linux as well:

alias std.toStrinz c_str;
bit fileExists(char [] name ) { return (GetFileAttributes( c_str(name) ) !=
0xFFFFFFFF); }

This actually compiles and works as expected ( it also works for
directories )

C



"Lars Ivar Igesund" <larsivar@igesund.net> wrote in message news:bri9o6$2qj7$1@digitaldaemon.com...
> With the current functions in std, I think it is easiest to implement exist by trying to read the file and catch the exception.
>
> bool exist(char [] file)
> {
>   bool result = true;
>   try {
>     read(file);
>   }
>   catch (FileException e) {
>     result = false;
>   }
>   return result;
> }
>
> Lars Ivar Igesund
>
> "Andrew Edwards" <edwardsac@spamfreeusa.com> wrote in message news:bri5v9$2l06$1@digitaldaemon.com...
> > The following program does not work because there is no exist() defined.
> How
> > would I go about defining such a function or can this be included in std.file? Thanks for your assistance.
> >
> > Andrew
> >
> > ==================
> >
> > import std.stream;
> >
> > void main ()
> > {
> >   char[] dirname = "pubs";
> >   char[] filename = "monitor.bat";
> >
> >   chdir("c:\\");
> >   if(exist(dirname) && exist(dirname~"\\"~filename)) //can we have this
in
> > file.d?
> >   {
> >     stdout.writeString("The pubs directory exists and ");
> >     stdout.writeLine("monitor.dat exists in the pubs directory.");
> >   }
> >   else
> >   {
> >     mkdir(dirname);
> >     chdir(dirname);
> >     write(filename, "ViewSonic GS790");
> >   }
> > }
> >
> >
>
>


December 14, 2003
First of all, thanks to both of you for your suggestions.

"Charles" wrote ...
> Don't mean to sound snotty, but seeing MW's report on D's speed of
exception
> handling, I think a better alternative would be to use stat on linux, and GetFileAttributes on Windows.
>

Where is GetFileAttributes defined? I assume it's defined in Kernel32.dll but am unable to compile the program as the compiler keeps barking about undefined identifier GetFileAttributes.


December 14, 2003
> Where is GetFileAttributes defined? I assume it's defined in Kernel32.dll but am unable to compile the program as the compiler keeps barking about undefined identifier GetFileAttributes.

Never mind. I've located it in std.c.windows.windows. Thanks a million.

Andrew


December 14, 2003
No problemo,

also YT's win32 wrappers are great, i havent ever use phobos windows. http://hp.vector.co.jp/authors/VA028375/contents/D_windows.h.html

C !

"Andrew Edwards" <edwardsac@spamfreeusa.com> wrote in message news:brii04$4m4$1@digitaldaemon.com...
> > Where is GetFileAttributes defined? I assume it's defined in
Kernel32.dll
> > but am unable to compile the program as the compiler keeps barking about undefined identifier GetFileAttributes.
>
> Never mind. I've located it in std.c.windows.windows. Thanks a million.
>
> Andrew
>
>


December 15, 2003
AFAIK this is the fastest way to check if a file exists on Windows.  Good call Charles!

Sean

"Charles" <sanders-consulting@comcast.net> wrote in message news:brichi$2ug8$1@digitaldaemon.com...
> Don't mean to sound snotty, but seeing MW's report on D's speed of
exception
> handling, I think a better alternative would be to use stat on linux, and GetFileAttributes on Windows.
>
> Pseudo code for windows, I'm actually installing a Linux box now so I'll
try
> to work this out on Linux as well:
>
> alias std.toStrinz c_str;
> bit fileExists(char [] name ) { return (GetFileAttributes( c_str(name) )
!=
> 0xFFFFFFFF); }
>
> This actually compiles and works as expected ( it also works for
> directories )
>
> C