|
Archives
D Programming
digitalmars.Ddigitalmars.D.bugs digitalmars.D.dtl digitalmars.D.ide digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger D.gnu D C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript electronics |
D - [Help] - Determining if a file or directory exists
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");
}
}
Dec 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...
Dec 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...
Dec 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 Dec 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. Dec 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 Dec 14 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 Dec 15 2003
|