Disks and Files

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;

HDD Serial Number

From: Christian Piene Gundersen <j.c.p.gundersen@jusstud.uio.no>

> We need to know how can we get the serial number of a HDD, working with
> Delphi 2.0

Try this:


procedure TForm1.Button1Click(Sender: TObject);
var
  SerialNum : pdword;
  a, b : dword;
  Buffer  : array [0..255] of char;
begin
  if GetVolumeInformation('c:\', Buffer, SizeOf(Buffer), SerialNum, a, b, nil, 0) then
    Label1.Caption := IntToStr(SerialNum^);
end;

How to check if drive 'a:' is ready?

From: "Angus Johnson" <ajohnson@rpi.net.au>


function DiskInDrive(const Drive: char): Boolean;
var
  DrvNum: byte;
  EMode: Word;
begin
  result := false;
  DrvNum := ord(Drive);
  if DrvNum >= ord('a') then dec(DrvNum,$20);
  EMode := SetErrorMode(SEM_FAILCRITICALERRORS);
  try
    if DiskSize(DrvNum-$40) <> -1 then result := true
    else messagebeep(0);
  finally
    SetErrorMode(EMode);
  end;
end;

Audio CD

From: "Chris Rankin" <RankinC@Logica.com>

Vincent Oostindie <vincent.oostindie@tip.nl> wrote
> * How do I get the unique number from an audio CD in the CD-ROM drive?

const
  MCI_INFO_PRODUCT                = $00000100;
  MCI_INFO_FILE                   = $00000200;
  MCI_INFO_MEDIA_UPC              = $00000400;
  MCI_INFO_MEDIA_IDENTITY         = $00000800;
  MCI_INFO_NAME                   = $00001000;
  MCI_INFO_COPYRIGHT              = $00002000;

{ parameter block for MCI_INFO command message }
type
  PMCI_Info_ParmsA = ^TMCI_Info_ParmsA;
  PMCI_Info_ParmsW = ^TMCI_Info_ParmsW;
  PMCI_Info_Parms = PMCI_Info_ParmsA;
  TMCI_Info_ParmsA = record
    dwCallback: DWORD;
    lpstrReturn: PAnsiChar;
    dwRetSize: DWORD;
  end;
  TMCI_Info_ParmsW = record
    dwCallback: DWORD;
    lpstrReturn: PWideChar;
    dwRetSize: DWORD;
  end;
  TMCI_Info_Parms = TMCI_Info_ParmsA;

These are the buffers you want: the identifier is returned as a string of decimal digits by the MCI_INFO_MEDIA_IDENTITY function. You should be able to cross-reference this with the online help (Win32 and TMediaPlayer component).

How can I delete a file to the Recycle Bin?

From: "Ed Lagerburg" <lagerbrg@euronet.nl>
program del;

uses
 ShellApi;

//function SHFileOperation(const lpFileOp: TSHFileOpStruct): Integer; stdcall;

Var T:TSHFileOpStruct;
    P:String;
begin
  P:='C:\Windows\System\EL_CONTROL.CPL';
  With T do
  Begin
    Wnd:=0;
    wFunc:=FO_DELETE;
    pFrom:=Pchar(P);
    fFlags:=FOF_ALLOWUNDO
  End;
  SHFileOperation(T);
End.

From: bstowers@pobox.com (Brad Stowers) There are some other quirks you should be aware of, too:
  • Give the complete file path for every file specified. Do not rely on the current directory, even if you change to it right before the call. The SHFileOperation API is not "smart" enough to use the current directory if one is not given for undo information. So, even if you give the FOF_ALLOWUNDO flag, it will not move deleted files to the recycle bin because it doesn't know what path they came from, and thus couldn't restore them to their original location from the recycle bin. It will simply delete the file from the current directory.

  • MS has a documentation correction about the pFrom member. It says that for multiple files, each filename is seperated by a NULL (#0) character, and the whole thing is terminated by double NULLs. You need the double NULL whether or not you are passing multiple filenames. Sometimes it will work correctly without them, but often not. That's because sometimes you get lucky and the memory following the end of your string has a NULL there.

    An example of how to do this would be:

    var
      FileList: string;
      FOS: TShFileOpStruct;
    begin
      FileList := 'c:\delete.me'#0'c:\windows\temp.$$$'#0#0;
      { if you were using filenames in string variables: }
      FileList := Filename1 + #0 + Filename2 + #0#0;
    
      FOS.pFrom := PChar(FileList);
    
      // blah blah blah
    end;
    

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

    This page has been created with