Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Delphi DLL) Iterate over Files and Directories in Filesystem Directory TreeDemonstrates how to use the Chilkat DirTree object/class to iterate over the names of files and sub-directories in a directory tree. This example iterates over a directory tree that contains these files and sub-directories: a images (sub-directory) dudeA.gif Thumbs.db hamlet.xml pigs.xml Setup.exe b yyy (sub-directory) zzz (sub-directory) HelloWorld123.txt test123.txt dude.gif dudeC.gif hello.txt Thumbs.db test123.txtThe output for the code below looks like this: (sub-directory) Relative Path: a Absolute Path: c:\temp\abc123\a Absolute UNC Path: \\?\c:\temp\abc123\a ---- (sub-directory) Relative Path: b Absolute Path: c:\temp\abc123\b Absolute UNC Path: \\?\c:\temp\abc123\b ---- Size in bytes: 15 Relative Path: test123.txt Absolute Path: c:\temp\abc123\test123.txt Absolute UNC Path: \\?\c:\temp\abc123\test123.txt ---- Size in bytes: 279658 Relative Path: a\hamlet.xml Absolute Path: c:\temp\abc123\a\hamlet.xml Absolute UNC Path: \\?\c:\temp\abc123\a\hamlet.xml ---- (sub-directory) Relative Path: a\images Absolute Path: c:\temp\abc123\a\images Absolute UNC Path: \\?\c:\temp\abc123\a\images ---- Size in bytes: 8463 Relative Path: a\pigs.xml Absolute Path: c:\temp\abc123\a\pigs.xml Absolute UNC Path: \\?\c:\temp\abc123\a\pigs.xml ---- Size in bytes: 24576 Relative Path: a\Setup.exe Absolute Path: c:\temp\abc123\a\Setup.exe Absolute UNC Path: \\?\c:\temp\abc123\a\Setup.exe ---- Size in bytes: 6221 Relative Path: b\dude.gif Absolute Path: c:\temp\abc123\b\dude.gif Absolute UNC Path: \\?\c:\temp\abc123\b\dude.gif ---- Size in bytes: 6221 Relative Path: b\dudeC.gif Absolute Path: c:\temp\abc123\b\dudeC.gif Absolute UNC Path: \\?\c:\temp\abc123\b\dudeC.gif ---- Size in bytes: 13 Relative Path: b\hello.txt Absolute Path: c:\temp\abc123\b\hello.txt Absolute UNC Path: \\?\c:\temp\abc123\b\hello.txt ---- Size in bytes: 10240 Relative Path: b\Thumbs.db Absolute Path: c:\temp\abc123\b\Thumbs.db Absolute UNC Path: \\?\c:\temp\abc123\b\Thumbs.db ---- (sub-directory) Relative Path: b\yyy Absolute Path: c:\temp\abc123\b\yyy Absolute UNC Path: \\?\c:\temp\abc123\b\yyy ---- (sub-directory) Relative Path: b\zzz Absolute Path: c:\temp\abc123\b\zzz Absolute UNC Path: \\?\c:\temp\abc123\b\zzz ---- Size in bytes: 6221 Relative Path: a\images\dudeA.gif Absolute Path: c:\temp\abc123\a\images\dudeA.gif Absolute UNC Path: \\?\c:\temp\abc123\a\images\dudeA.gif ---- Size in bytes: 6144 Relative Path: a\images\Thumbs.db Absolute Path: c:\temp\abc123\a\images\Thumbs.db Absolute UNC Path: \\?\c:\temp\abc123\a\images\Thumbs.db ---- Size in bytes: 6080 Relative Path: b\zzz\HelloWorld123.txt Absolute Path: c:\temp\abc123\b\zzz\HelloWorld123.txt Absolute UNC Path: \\?\c:\temp\abc123\b\zzz\HelloWorld123.txt ---- Size in bytes: 15 Relative Path: b\zzz\test123.txt Absolute Path: c:\temp\abc123\b\zzz\test123.txt Absolute UNC Path: \\?\c:\temp\abc123\b\zzz\test123.txt ----
uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, DirTree; ... procedure TForm1.Button1Click(Sender: TObject); var dirTree: HCkDirTree; success: Boolean; doneIterating: Boolean; begin dirTree := CkDirTree_Create(); // Specify the root of the directory tree to be traversed. CkDirTree_putBaseDir(dirTree,'c:/temp/abc123'); // Indicate that we want to recursively traverse the tree. CkDirTree_putRecurse(dirTree,True); // Begin the directory tree traversal. success := CkDirTree_BeginIterate(dirTree); if (success <> True) then begin Memo1.Lines.Add(CkDirTree__lastErrorText(dirTree)); Exit; end; doneIterating := CkDirTree_getDoneIterating(dirTree); while doneIterating <> True do begin // Examine the file or directory at the current traversal position: if (CkDirTree_getIsDirectory(dirTree) = True) then begin Memo1.Lines.Add('(sub-directory)'); end else begin Memo1.Lines.Add('Size in bytes: ' + IntToStr(CkDirTree_getFileSize32(dirTree))); // If the file size is larger than 2GB, then get the size as a decimal string. // (Chilkat provides a FileSize64 method in *some* programming languages. The FileSizeStr property is added in Chilkat v9.5.0.88) Memo1.Lines.Add('Size in bytes: ' + CkDirTree__fileSizeStr(dirTree)); end; Memo1.Lines.Add('Relative Path: ' + CkDirTree__relativePath(dirTree)); Memo1.Lines.Add('Absolute Path: ' + CkDirTree__fullPath(dirTree)); Memo1.Lines.Add('Absolute UNC Path: ' + CkDirTree__fullUncPath(dirTree)); Memo1.Lines.Add('----'); // Advance to the next file or sub-directory in the tree traversal. success := CkDirTree_AdvancePosition(dirTree); if (success <> True) then begin if (CkDirTree_getDoneIterating(dirTree) <> True) then begin Memo1.Lines.Add(CkDirTree__lastErrorText(dirTree)); Exit; end; end; doneIterating := CkDirTree_getDoneIterating(dirTree); end; CkDirTree_Dispose(dirTree); end; |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.