Search results for 'Delphi File System Control'. 10 post(s) found.

  1. 2007/09/10 Reading a directory content
  2. 2007/09/10 Path shortener: c:\AB\C...DE\F.ghi
  3. 2007/09/10 How to Split and Merge Files
  4. 2007/09/10 Get File 'Last Modified' attribute
  5. 2007/09/10 From/to the 8.3 (short) format to/from the long format
  6. 2007/09/10 Delete folders recursively
  7. 2007/09/10 Delete files with the ability to UNDO
  8. 2007/09/10 Does my CD-ROM drive contain an audio CD?
  9. 2007/09/10 Convert a mapped drive to a full UNC path
  10. 2007/09/10 Checking If File Is In Use
2007/09/10 16:44

Reading a directory content


The following example lists all files and subdirectories of the C:\Windows directory into a TListBox called ListBox1:

Usage:
FindAll('C:\Windows\*.*',faAnyFile,ListBox1.Items)


Followings are the source code for FindAll procedure.
procedure FindAll (const Path: String;
                          Attr: Integer;
                          List: TStrings) ;
var
   Res: TSearchRec;
   EOFound: Boolean;
begin
   EOFound:= False;
   if FindFirst(Path, Attr, Res) < 0 then
     exit
   else
     while not EOFound do begin
       List.Add(Res.Name) ;
       EOFound:= FindNext(Res) <> 0;
     end;
   FindClose(Res) ;
end;

Trackback 3 Comment 0

Trackback : Cannot send a trackbact to this post.

  1. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 05:04 delete

    moneyideas

  2. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 13:42 delete

    moneyideas

  3. Subject different money making ideas

    Tracked from moneyideas 2010/01/31 16:39 delete

    moneyideas

2007/09/10 16:42

Path shortener: c:\AB\C...DE\F.ghi


Sometimes when a huge path [any long string] is to be displayed in a small space, it is desirable to see the start and the end of the path with ellipses in-between, rather than truncating one of the ends.

For example
"C:\Program Files\Delphi\DDrop\TargetDemo\main.pas"
is desired to be seen as
"C:\Program F....Demo\main.pas"
then the following "Mince" function could be used.

function Mince(PathToMince :String; InSpace :Integer): String;
var TotalLength, FLength : Integer;
begin
  TotalLength := Length(PathToMince) ;
  if TotalLength > InSpace then
  begin
   FLength := (Inspace Div 2) - 2;
   Result := Copy(PathToMince, 0, fLength)
             + '...'
             + Copy(PathToMince,
                   TotalLength-fLength,
                   TotalLength) ;
  end
  else
    Result := PathToMince;
end;

The "InSpace" is an approximate number of charecters the intended space can hold.
Trackback 3 Comment 0

Trackback : Cannot send a trackbact to this post.

  1. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 02:23 delete

    moneyideas

  2. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 10:38 delete

    moneyideas

  3. Subject different money making ideas

    Tracked from moneyideas 2010/01/31 16:39 delete

    moneyideas

2007/09/10 16:41

How to Split and Merge Files


There are times when you need to split a large file into several smaller ones (for whatever the purpose is). The following two methods use Delphi TStream objects to break a file into several files with predefined size; and to merge those files back to the original.

procedure SplitFile(FileName : TFileName; FilesByteSize : Integer) ;
// FileName == file to split into several smaller files
// FilesByteSize == the size of files in bytes
var
   fs, ss: TFileStream;
   cnt : integer;
   SplitName: String;
begin
   fs := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite) ;
   try
     for cnt := 1 to Trunc(fs.Size / FilesByteSize) + 1 do
     begin
       SplitName := ChangeFileExt(FileName, Format('%s%d', ['._',cnt])) ;
       ss := TFileStream.Create(SplitName, fmCreate or fmShareExclusive) ;
       try
         if fs.Size - fs.Position < FilesByteSize then
           FilesByteSize := fs.Size - fs.Position;
         ss.CopyFrom(fs, FilesByteSize) ;
       finally
         ss.Free;
       end;
     end;
   finally
     fs.Free;
   end;
end;


Note: a 3 KB file 'myfile.ext' will be split into 'myfile._1', 'myfile._2','myfile._3' if FilesByteSize parameter equals 1024 (1 KB).
Trackback 3 Comment 0

Trackback : Cannot send a trackbact to this post.

  1. Subject different money making ideas

    Tracked from moneyideas 2010/01/28 23:32 delete

    moneyideas

  2. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 07:48 delete

    moneyideas

  3. Subject different money making ideas

    Tracked from moneyideas 2010/01/31 16:39 delete

    moneyideas

2007/09/10 16:24

Get File 'Last Modified' attribute


This function is useful to get file attribute.

Usage:
label1.Caption:=FileLastModified('c:\autoexec.bat') ;


In addition, below function must be included in order to use above function.
function FileLastModified(const TheFile: string): string;
var
  FileH : THandle;
  LocalFT : TFileTime;
  DosFT : DWORD;
  LastAccessedTime : TDateTime;
  FindData : TWin32FindData;
begin
  Result := '';
  FileH := FindFirstFile(PChar(TheFile), FindData) ;
  if FileH <> INVALID_HANDLE_VALUE then begin
   Windows.FindClose(Handle) ;
   if (FindData.dwFileAttributes AND
       FILE_ATTRIBUTE_DIRECTORY) = 0 then
    begin
     FileTimeToLocalFileTime
      (FindData.ftLastWriteTime,LocalFT) ;
     FileTimeToDosDateTime
      (LocalFT,LongRec(DosFT).Hi,LongRec(DosFT).Lo) ;
     LastAccessedTime := FileDateToDateTime(DosFT) ;
     Result := DateTimeToStr(LastAccessedTime) ;
    end;
  end;
end;
Trackback 3 Comment 0

Trackback : Cannot send a trackbact to this post.

  1. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 00:00 delete

    moneyideas

  2. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 08:12 delete

    moneyideas

  3. Subject different money making ideas

    Tracked from moneyideas 2010/01/31 16:39 delete

    moneyideas

2007/09/10 16:21

From/to the 8.3 (short) format to/from the long format


This unit provides two functions that convert filenames from the long format to the 8.3 format, and from the 8.3 format to the long format.

LFN_ALT.pas
unit LFN_ALT;
interface
function AlternateToLFN(AltName:String):String;
function LFNToAlternate(LongName:String):String;

implementation

uses Windows;

function AlternateToLFN(AltName:String):String;
var
  temp: TWIN32FindData;
  searchHandle: THandle;
begin
  searchHandle:=FindFirstFile(PChar(AltName),temp) ;
  if searchHandle <> ERROR_INVALID_HANDLE then
    result := String(temp.cFileName)
  else
    result := '';
  Windows.FindClose(searchHandle) ;
end;

function LFNToAlternate(LongName:String):String;
var
  temp: TWIN32FindData;
  searchHandle: THandle;
begin
  searchHandle:=FindFirstFile(PChar(LongName),temp) ;
  if searchHandle <> ERROR_INVALID_HANDLE then
    result := String(temp.cALternateFileName)
  else
    result := '';
  Windows.FindClose(searchHandle) ;
end;
end.{unit}
Trackback 3 Comment 0

Trackback : Cannot send a trackbact to this post.

  1. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 05:11 delete

    moneyideas

  2. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 13:57 delete

    moneyideas

  3. Subject different money making ideas

    Tracked from moneyideas 2010/01/31 16:39 delete

    moneyideas

2007/09/10 16:19

Delete folders recursively


The following function completely deletes a directory regardless of whether the directory is filled or has subdirectories. No confirmation is requested so be careful. If the operation is successful then True is returned, False otherwise.

Usage
if DelTree('c:\TempDir') then
ShowMessage('Directory deleted!')
else
ShowMessage('Errors occured!') ;


In order to use above usage, you must include below functions.
uses ShellAPI;

Function DelTree(DirName : string): Boolean;
var
  SHFileOpStruct : TSHFileOpStruct;
  DirBuf : array [0..255] of char;
begin
  try
   Fillchar(SHFileOpStruct,Sizeof(SHFileOpStruct),0) ;
   FillChar(DirBuf, Sizeof(DirBuf), 0 ) ;
   StrPCopy(DirBuf, DirName) ;
   with SHFileOpStruct do begin
    Wnd := 0;
    pFrom := @DirBuf;
    wFunc := FO_DELETE;
    fFlags := FOF_ALLOWUNDO;
    fFlags := fFlags or FOF_NOCONFIRMATION;
    fFlags := fFlags or FOF_SILENT;
   end;
    Result := (SHFileOperation(SHFileOpStruct) = 0) ;
   except
    Result := False;
  end;
end;
Trackback 3 Comment 0

Trackback : Cannot send a trackbact to this post.

  1. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 04:22 delete

    moneyideas

  2. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 13:23 delete

    moneyideas

  3. Subject different money making ideas

    Tracked from moneyideas 2010/01/31 16:39 delete

    moneyideas

2007/09/10 13:43

Delete files with the ability to UNDO


Here's a Delphi procedure that can delete a file with the ability to undo by sending the file to the "Recycle Bin."

Function FileDeleteRB will return True if the operation was successful.

uses ShellAPI;

function FileDeleteRB( AFileName:string): boolean;
var Struct: TSHFileOpStruct;
    pFromc: array[0..255] of char;
    Resultval: integer;
begin
   if not FileExists(AFileName) then begin
      Result := False;
      exit;
   end
   else begin
      fillchar(pfromc,sizeof(pfromc),0) ;
      StrPcopy(pfromc,expandfilename(AFileName)+#0#0) ;
      Struct.wnd := 0;
      Struct.wFunc := FO_DELETE;
      Struct.pFrom := pFromC;
      Struct.pTo := nil;
      Struct.fFlags:= FOF_ALLOWUNDO or FOF_NOCONFIRMATION
         or FOF_SILENT;
      Struct.fAnyOperationsAborted := false;
      Struct.hNameMappings := nil;
      Resultval := ShFileOperation(Struct) ;
      Result := (Resultval = 0) ;
   end;
end;
Trackback 3 Comment 0

Trackback : Cannot send a trackbact to this post.

  1. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 01:36 delete

    moneyideas

  2. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 09:51 delete

    moneyideas

  3. Subject different money making ideas

    Tracked from moneyideas 2010/01/31 16:38 delete

    moneyideas

2007/09/10 13:30

Does my CD-ROM drive contain an audio CD?


We can use the Windows API function GetDriveType() to test if the drive is a CD-ROM drive then use the Windows API function GetVolumeInformation() to test if the VolumeName is 'Audio CD'.

function IsAudioCD(Drive : char) : bool;
var
   DrivePath : string;
   MaximumComponentLength : DWORD;
   FileSystemFlags : DWORD;
   VolumeName : string;
begin
   Result := false;
   DrivePath := Drive + ':\';
   if GetDriveType(PChar(DrivePath))
   <> DRIVE_CDROM then exit;
   SetLength(VolumeName, 64) ;
   GetVolumeInformation(PChar(DrivePath),
                        PChar(VolumeName),
                        Length(VolumeName),
                        nil,
                        MaximumComponentLength,
                        FileSystemFlags,
                        nil,
                        0) ;
   if lStrCmp(PChar(VolumeName),'Audio CD') = 0
   then result := true;
end;



Usage:
procedure TForm1.Button1Click(Sender: TObject) ;
begin
  if not IsAudioCD('D') then
   ShowMessage('Not an Audio CD in drive D') ;
end;
Trackback 4 Comment 0

Trackback : Cannot send a trackbact to this post.

  1. Subject different money making ideas

    Tracked from moneyideas 2010/01/25 08:37 delete

    moneyideas

  2. Subject different money making ideas

    Tracked from moneyideas 2010/01/28 21:56 delete

    moneyideas

  3. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 06:22 delete

    moneyideas

  4. Subject different money making ideas

    Tracked from moneyideas 2010/01/31 16:38 delete

    moneyideas

2007/09/10 12:19

Convert a mapped drive to a full UNC path


Universal/Uniform Naming Convention. A UNC Path describes the location of a volume, directory, or file.

The format for a UNC Path is \\server\volume\directory\file and is not case-sensitive. For example:

    \\Shared1_svr\Shared1\WGroups\Network\Orders.xls

Rather than describe the location of a file or directory by drive letter, the Network Group will typically communicate a UNC Path to describe the actual location of a file or directory. Windows drive letter mappings are arbitrary, whereas a UNC Path is specific and applies to all operating systems.

Note: The UNC method started with the UNIX operating system. UNIX uses the forward-slash character as a path separator. Many network services (ex. FTP) have their origins in the UNIX operating system, so they use forward-slashes instead of the backslashes that DOS/Windows uses. It is important to recognize this distinction when using these services.

Usage:
UNCLabel.Caption := ConvertToUNCPath(ExtractFileDrive(Edit1.Text)) ;

Followings are the source can get UNC Path.
function ConvertToUNCPath(MappedDrive: string) : string;
var
   RemoteString : array[0..255] of char;
   lpRemote : PChar;
   StringLen : Integer;
begin
  lpRemote := @RemoteString;
  StringLen := 255;
   If WNetGetConnection(Pchar(ExtractFileDrive(MappedDrive)) ,
                        lpRemote,
                        StringLen) = NO_ERROR Then
     Result := RemoteString
   Else
     Result:=''; // Alternatively return an errorcode, Raise an exception or something like end;
end;

Trackback 3 Comment 0

Trackback : Cannot send a trackbact to this post.

  1. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 04:40 delete

    moneyideas

  2. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 13:32 delete

    moneyideas

  3. Subject different money making ideas

    Tracked from moneyideas 2010/01/31 16:38 delete

    moneyideas

2007/09/10 12:15

Checking If File Is In Use


IsFileInUse will return true if the file is locked for exclusive access. It would fail if the file doesn't exist at all.

function IsFileInUse(fName : string) : boolean;
var
    HFileRes : HFILE;
begin
    Result := false;
    if not FileExists(fName) then exit;
    HFileRes :=
      CreateFile(pchar(fName),
                 GENERIC_READ or GENERIC_WRITE,
                 0, nil, OPEN_EXISTING,
                 FILE_ATTRIBUTE_NORMAL,
                 0) ;
    Result := (HFileRes = INVALID_HANDLE_VALUE) ;
    if not Result then
    CloseHandle
end;

Trackback 3 Comment 0

Trackback : Cannot send a trackbact to this post.

  1. Subject different money making ideas

    Tracked from moneyideas 2010/01/28 23:31 delete

    moneyideas

  2. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 07:50 delete

    moneyideas

  3. Subject different money making ideas

    Tracked from moneyideas 2010/01/31 16:38 delete

    moneyideas