MKoD - D Programming Language

Win32 API FindFirstFile() example in D - code-name findfirstfile.d

Basic Newbie Stuff Win32 API FindFirstFile() example:

/+
MSDN Online Library - FindFirstFile Example
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/findfirstfile.asp
#define _WIN32_WINNT 0x0400

#include <windows.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
  WIN32_FIND_DATA FindFileData;
  HANDLE hFind;

  printf ("Target file is %s.\n", argv[1]);
  hFind = FindFirstFile(argv[1], &FindFileData);
  if (hFind == INVALID_HANDLE_VALUE) 
  {
    printf ("Invalid File Handle. GetLastError reports %d\n", GetLastError ());
    return (0);
  } 
  else 
  {
    printf ("The first file found is %s\n", FindFileData.cFileName);
    FindClose(hFind);
    return (1);
  }
}
+/

// Win32 API example of FindFirstFile() - last tested with D v1.0
// To Compile: C:\dmd\MKoD>dmd FindFirstFile.d
private import std.c.windows.windows;
private import std.string;
private import std.stdio;

int main( char[][] args )
{
    WIN32_FIND_DATA data;
    HANDLE          hFind;
    SYSTEMTIME      systime;
    SYSTEMTIME*     lpSystemTime = &systime;

    if ( args.length <= 1 )
    {
        writefln( "Parameter missing, try the following...FindFirstFile \"C:\\Test.txt\"" );
        return 1;
    }
        
    hFind = FindFirstFileA( cast(char *)args[ 1 ], &data );
    
    if ( hFind == INVALID_HANDLE_VALUE ) 
    {
        writefln( "File not found or Invalid File Handle. GetLastError reports %d", GetLastError() );
        return 0;
    } 
    else 
    {
        writefln( "The first file found is %s", toString( cast(char *)data.cFileName ) );
        //writefln( "%08x %d", data.dwFileAttributes, data.dwFileAttributes.sizeof );
        FileTimeToSystemTime( &data.ftCreationTime, lpSystemTime);
        writefln( "Created=%02d/%02d/%04d %d", 
           systime.wDay, systime.wMonth, systime.wYear, data.ftCreationTime.sizeof );

        FileTimeToSystemTime( &data.ftLastAccessTime, lpSystemTime);
        writefln( "Last Access=%02d/%02d/%04d %d", 
           systime.wDay, systime.wMonth, systime.wYear, data.ftLastAccessTime.sizeof );

        FileTimeToSystemTime( &data.ftLastWriteTime, lpSystemTime);		
        writefln( "Last Written=%02d/%02d/%04d %d", 
           systime.wDay, systime.wMonth, systime.wYear, data.ftLastWriteTime.sizeof );

        writefln( "%08x %d", data.nFileSizeLow,     data.nFileSizeLow.sizeof );
        writefln( "%s %d", toString( cast(char *)data.cFileName ), data.cFileName.sizeof );

        FindClose( hFind );
        return 1;
    }
    
    return 0;   
}
C:\dmd\MKOD_ex>..\bin\dmd findfirstfile.d
C:\dmd\bin\..\..\dm\bin\link.exe findfirstfile,,,user32+kernel32/noi;

C:\dmd\MKOD_ex>findfirstfile "C:\test.txt"
The first file found is Test.txt
Created=12/04/2005 8
Last Access=12/04/2005 8
Last Written=12/04/2005 8
0000000e 4
Test.txt 260

C:\dmd\MKOD_ex>findfirstfile "C:\*.*"
The first file found is outlook.doc
Created=03/02/2005 8
Last Access=01/04/2005 8
Last Written=03/02/2005 8
000048d2 4
outlook.doc 260

C:\dmd\MKOD_ex>findfirstfile "C:\*.txt"
The first file found is Test Print.txt
Created=01/12/2004 8
Last Access=18/01/2005 8
Last Written=01/12/2004 8
0001147d 4
Test Print.txt 260

C:\dmd\MKOD_ex>findfirstfile "C:\*.lic"
File not found or Invalid File Handle. GetLastError reports 2

C:\dmd\MKOD_ex>
Mars: fourth Rock from the Sun.