Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to copy all files from one directory to another Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
30-Aug-02
Category
Files Operation
Language
Delphi All Versions
Views
91
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

Creating a new directory (folder) is no problem. There is the MkDir() procedure. 
But how does one copy all files from another directory into this new one within 
Delphi run time? I am also concerned that any pseudo DOS command will not be 
available in the future, especially Windows NT 5 (2000).

Answer:

Solve 1:

1   uses
2     shellapi
3   
4   function FileManager(xSourcePath, xDestPath, xPara: string): Boolean;
5   var
6     PFileMsg: TSHFileOpStruct;
7     mNowPath: string;
8   begin
9     Result := False;
10    FillChar(PFileMsg, sizeof(PFileMsg), #0);
11    if pos('.', xpara) = 0 then
12      exit;
13    mNowPath := GetCurrentDir;
14    if xSourcePath <> '' then
15      if not DirectoryExists(xSourcePath) then
16      begin
17        showmessage('The source path does not exist !');
18        exit;
19      end;
20    if xDestPath <> '' then
21      if not DirectoryExists(xDestPath) then
22      begin
23        showmessage('The destination path does not exist !');
24        exit;
25      end;
26    if SetCurrentDirectory(Pchar(xSourcePath)) then
27    begin
28      with PFileMsg do
29      begin
30        if Owner is TForm then
31          Wnd := TForm(Owner).Handle
32        else
33          Wnd := Application.Handle;
34        if xDestPath <> '' then
35        begin
36          wFunc := FO_COPY;
37          PTo := pChar(xDestPath);
38          fFlags := FOF_MULTIDESTFILES + FOF_NOCONFIRMATION;
39        end
40        else
41        begin
42          wFunc := FO_DELETE;
43          fFlags := FOF_ALLOWUNDO + FOF_NOCONFIRMATION;
44        end;
45        pFrom := PChar(xPara + #0#0);
46      end;
47      SHFileOperation(PFileMsg);
48      SetCurrentDirectory(Pchar(mNowPath));
49      Application.ProcessMessages;
50      Result := True;
51    end;
52  end;



Example:

CopyFile: 
53  FileManager('C:\Demo', 'C:\Temp', '*.*');
54  
55  DeleteFile: (delete C: \Demo\ * . * )
56  FileManager('C:\Demo', '', '*.*');



Solve 2:

57  uses
58    ShellAPI;
59  
60  procedure TForm1.BtnCopyClick(Sender: TObject);
61  var
62    fileOp: TShFileOpStruct;
63    fromDir: string;
64    toDir: string;
65  begin
66    FillChar(fileOp, Sizeof(TShFileOpStruct), 0);
67    fromDir := DirectoryListBox1.Directory + '\*.*'#0;
68    toDir := DirectoryListBox2.Directory + #0;
69    with fileOp do
70    begin
71      wnd := Handle;
72      wfunc := FO_COPY;
73      pFrom := PChar(fromDir);
74      pTo := PChar(toDir);
75      fFlags := FOF_ALLOWUNDO;
76      fAnyOperationsAborted := false;
77      hNameMappings := nil;
78      lpszProgressTitle := nil;
79    end;
80    SHFileOperation(fileOp);
81  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC