Files
  1. How to get files "Last Accessed" attribute?
  2. How do I convert "Long File Name.pas" to "longfi~1.pas"?
  3. getting datetime problem
  4. Coping of the files
  5. Drag & Drop with Win95 Explorer[NEW]

How to get files "Last Accessed" attribute?

From: Jon Erik Oterhals <jonoter@stud.ntnu.no>

Brian Fløe Sørensen wrote:

 In Windows 95, you can see when a file was last accessed by right-clicking
 the file and selecting properties.

 How can I get this information in Delphi/API???


procedure TForm1.Button1Click(Sender: TObject);
var
  FileHandle : THandle;
  LocalFileTime : TFileTime;
  DosFileTime : DWORD;
  LastAccessedTime : TDateTime;
  FindData : TWin32FindData;
begin
  FileHandle := FindFirstFile('AnyFile.FIL', FindData);
  if FileHandle <> INVALID_HANDLE_VALUE then
  begin
    Windows.FindClose(Handle);
    if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then
    begin
      FileTimeToLocalFileTime(FindData.ftLastWriteTime, LocalFileTime);
      FileTimeToDosDateTime(LocalFileTime,
      LongRec(DosFileTime).Hi,LongRec(DosFileTime).Lo);
      LastAccessedTime := FileDateToDateTime(DosFileTime);
      Label1.Caption := DateTimeToStr(LastAccessedTime);
    end;
  end;
end;

How do I convert "Long File Name.pas" to "longfi~1.pas"?

From: "DynaSoft." <TimH@onaustralia.com.au>

Here try these procedures.


Function GetShortFileName(Const FileName : String) : String;
var
  aTmp: array[0..255] of char;
begin
  if GetShortPathName(PChar(FileName),aTmp,Sizeof(aTmp)-1)=0 then
     Result:= FileName
  else
     Result:=StrPas(aTmp);
end;

Function GetLongFileName(Const FileName : String) : String;
var
  aInfo: TSHFileInfo;
begin
  if SHGetFileInfo(PChar(FileName),0,aInfo,Sizeof(aInfo),SHGFI_DISPLAYNAME)<>0 then
     Result:= String(aInfo.szDisplayName)
  else
     Result:= FileName;
end;

getting datetime problem

From: laserjet <laserjet@concentric.net>

Try the following function which does not require FindFirst:


 function GetFileDate(TheFileName: string): string;
 var
   FHandle: integer;
 begin
   FHandle := FileOpen(TheFileName, 0);
   result := DateTimeToStr(FileDateToDateTime(FileGetDate(FHandle)));
   FileClose(FHandle);
 end;

From: bziegler@Radix.Net (Ben Ziegler)

One note of caution, some of the Win32 functions return times in GMT time, and you have to convert it to local time. Check your docs to be sure. (FindNextFile does this I believe).

Coping of the files

I have diffculties with coping the files. Delphi don't want to compile LZCopy command.

this way it work very slow


pbBuf := PChar( LocalAlloc(LMEM_FIXED, 1) );

FileSeek(source,0,0);
FileSeek(dest,0,0);
repeat
    cbRead := Fileread(source, pbBuf, 1);
    FileWrite(dest, pbBuf, cbRead);
until (cbRead = 0);

Solution 1

[Niel Calitz, omremcon@iafrica.com]
{  You must add LZExpand to your uses clause  ea. USES LZExpand; }
function CopyFile(SrcF,DestF : string) : boolean;
var
  SFile,
  DFile : integer;
  Res   : longint;
  Msg   : string;

begin
  SFile := FileOpen(SrcF,0);        { Open ReadOnly = 0, Write=1, Readwrite=2}
  DFile := FileCreate(DestF);
  Res := LZCopy(SFile,DFile);
  FileClose(SFile);
  FileClose(DFile);
  if Res < 0 then
  begin
    Msg := 'Unknown error';
    case Res of
      LZERROR_BADINHANDLE   : Msg := 'Invalid Source file handle';
      LZERROR_BADOUTHANDLE  : Msg := 'Invalid Destination file handle';
      LZERROR_BADVALUE      : Msg := 'Input parameter is out of range';
      LZERROR_GLOBALLOC     : Msg := 'Insufficient memory for the required buffers';
      LZERROR_GLOBLOCK      : Msg := 'Internal data structure handle invalid';
      LZERROR_READ          : Msg := 'Source file format is not valid';
      LZERROR_UNKNOWNALG    : Msg := 'The Source file was compressed with an unrecognized compression algorithm';
      LZERROR_WRITE         : Msg := 'There is insufficient space for the output file';
    end;
    MessageDlg(Msg,mtERROR,[mbOK],0);
    result := FALSE
  end else
    result := TRUE;
end;

Solution 2

[Tadas Vizbaras, tavizb@rc.lrs.lt]

I'll bet it's slow! It's reading the file one character at a time... Try allocating 8192 bytes and reading 8192 bytes at a time. That should speed it up a bit...

Solution 3

[Sid Gudes, cougar@roadrunner.com]

The simplest way to copy files is this:


                VAR
                         sI,dI:Longint;
                        sD,sS:TFilename;

                USES LZExpand;
                        ............
                  sI := FileOpen(sS,fmShareDenyWrite);
                 dI := FileCreate(sD);
                  { Copy file }
                   CopyLZFile(sI,dI);
                  {close files}
                 FileClose(sI);
                 FileClose(dI);
                        ............

Drag & Drop with Win95 Explorer[NEW]

From: Reid Roman <rkroman@pacbell.net>

Here's something I got from these forums at an arlier time;


interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
  ComCtrls;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    PROCEDURE FileIsDropped ( VAR Msg : TMessage ) ; Message WM_DropFiles ;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
uses
shellapi;
{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
   DragAcceptFiles( Handle,True ) ;
end;

PROCEDURE TForm1.FileIsDropped ( VAR Msg : TMessage ) ;
VAR
   hDrop : THandle ;
   fName : ARRAY[0..254] OF CHAR ;
   NumberOfFiles : INTEGER ;
   fCounter : INTEGER ;
   Names : STRING ;
BEGIN
   hDrop := Msg.WParam ;
   NumberOfFiles := DragQueryFile(hDrop,-1,fName,254);
   Names := '' ;
   FOR fCounter := 1 TO NumberOfFiles DO BEGIN
      DragQueryFile(hDrop,fCounter,fName,254);
// Here you have your file name 1 by 1
      Names := Names + #13#10 + fName ;
   END ;

   ShowMessage('Droped '+IntToStr(NumberOfFiles) + ' Files : ' + Names );
   DragFinish ( hDrop);
END ;

end.


Please email me and tell me if you liked this page.