Matthew Hipkin

Copy directory structure in Delphi and Lazarus

Here is a simple little procedure that recursively copies the specified directory structure (inDir) and replicates it in the target directory (outDir).

No files are copied, just the directory structure.

procedure CopyDirectoryStructure(inDir: String; outDir: String);
var
  s: TSearchRec;
  nInDir, nOutDir: String;
begin
  if FindFirst(IncludeTrailingPathDelimiter(inDir) + '*',faDirectory, s) = 0 then
  begin
    repeat
      if (s.Name <> '.') and (s.Name <> '..') and ((s.Attr and faDirectory) = faDirectory) then
      begin
        nInDir := IncludeTrailingPathDelimiter(inDir) + s.Name;
        nOutDir := IncludeTrailingPathDelimiter(outDir) + s.Name;
        // Create new subdirectory in outDir
        mkdir(nOutDir);
        // Recurse into subdirectory in inDir
        copyDirectoryStructure(nInDir,nOutDir);
      end;
    until FindNext(s) <> 0;
  end;
  FindClose(s);
end;
blog comments powered by Disqus