[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 334
  • Last Modified:

shfileoperation

Hi, i was wondering if it is possible to do this in a shfileoperation api function.. i want to copy a file name.txt
to several folders found in drive a lets say
f.tfrom = 'c:\name.txt'
f.tto = 'a:\backup1'
f.tto = 'a:\backup2'
f.tto = 'a:\backup2'

thanks..
0
therooster
Asked:
therooster
  • 4
  • 3
  • 2
1 Solution
 
ChopinkeCommented:
You can do it only with  as many calls to ShFileOperation() as many places you want to copy the file(s) to.

You cannot do it in one single step.

Tell me if you want an example for using the ShFileOperations() function itself.

Charlie
0
 
theroosterAuthor Commented:
charlie,
            all i can say with is one is that accoriding to the description of the to and from of the structure it can support.. but yes you can give an example...  thanks
0
 
KalrothCommented:
I believe you just have to seperate the source and/or destination paths with semicolon, I haven't tested it though, sorry.

eg: f.tto := 'a:\backup1;a:\backup2;a:\backup3'

Good luck.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
KalrothCommented:
Cancel that.  It seems that the seperator is #0 and it's #0 terminated as well.

eg: f.tto := 'a:\backup1'+#0+'a:\backup2'+#0+'a:\backup3'+#0;

My previous post was a bit hasty, sorry.
0
 
ChopinkeCommented:
Sorry, my mistake. You can copy multiple files in one step, but you have to put the source file name as many times as you put the detination file names. Here is the example, that I tested and working okay:

It copies
C:\TEMP\TEST\a.txt
to
C:\TEMP\TEST\1\a.txt
and
C:\TEMP\TEST\2\a.txt

procedure TForm1.Button1Click(Sender: TObject);
var  SHFileOpStruct : TSHFileOpStruct;
begin
    Fillchar(SHFileOpStruct, Sizeof(SHFileOpStruct), 0 );
    with SHFileOpStruct do
    begin
      Wnd    := 0;
      wFunc := FO_COPY;
      pFrom  := PChar('c:\temp\test\a.txt'#0'c:\temp\test\a.txt'#0#0);
      pTo    := PChar('c:\temp\test\1\a.txt'#0'c:\temp\test\2\a.txt'#0#0);
      fFlags := FOF_ALLOWUNDO or FOF_MULTIDESTFILES or FOF_SIMPLEPROGRESS;
//      fFlags := fFlags or FOF_RENAMEONCOLLISION;
    end;
    if SHFileOperation(SHFileOpStruct) = 0 then ShowMessage('OK');
end;

Note that you have to put #0 after every file or directory and #0#0 to the very end of the list.

Cheers,

Charlie

0
 
theroosterAuthor Commented:
Hi, i have this code but it displays an error
"Cannot Copy file         Filesystem Error(1026)"


procedure TForm1.Button1Click(Sender: TObject);
begin

  if MessageDlg('Please insert diskette in dirve A:\', mtInformation, [mbOK,mbCancel],0) =  mrOk then
  begin


  F.wnd := handle;
  F.wfunc := FO_COPY;
  F.pfrom := PChar('c:\tools\diher1.exe'#0'c:\tools\dipher2.exe'#0#0);
  F.pto := Pchar('a:\'#0'a:\'#0#0);
  F.hNameMappings := NIL;
  f.Fflags := FOF_MULTIDESTFILES OR FOF_NOCONFIRMATION OR FOF_NOCONFIRMMKDIR;
  if shfileoperation(F) <>0 then showmessage('copy failed');

 end;


thanks...

0
 
theroosterAuthor Commented:
sorry, the error is cannot copy the parameter is incorrect.. thanks..
0
 
ChopinkeCommented:
The problem is the logic, not the function.

You can do 2 kinds of copy:

1) Copy list of files to one specific dir

2) Copy file by file from source to destination, by specifying both source and destination filenames. In this case you must have the same number of files in the pFrom as in pTo.

The 2) can be achieved by specifying the FOF_MULTIDESTFILES flag.

In your example you did not specify the destination filenames!

I know you want to just list 1 file and copy it to 3 dir, but it is not much effort to list all destination files:

Copy both
c:\sample.exe and c:\other.exe
to both
a:\backup_1_ and a:\backup_2_

pFrom := 'c:\sample.exe'#0'c:\sample.exe'#0'c:\other.exe'#0'c:\other.exe'#0#0
pTo := 'a:\backup_1_\sample.exe'#0'a:\backup_2)\sample.exe'#0'a:\backup_1_\other.exe'#0'a:\backup_2_\other.exe'#0#0

If you have the list of original files and the list of destination directories then:

var
  FilesToBackup: TStrings; //list of original filenames
  DestDirs: TStrings; //list of destination directories
  src, dst: String;

... //fill up the two string array above

src := ''; dst := '';
for i := 0 to FilesToBackup.Count - 1 do
  for j := 0 to DestDirs.Count - 1 do
  begin
    src := src + FilesToBackup[i] + #0;
    dst := dst + DestDirs[i] + ExtractFileName(FilesToBackup[i]) + #0;
  end;
src := src + #0; dst := dst + #0; //to have the #0#0 at the end

... //you can use pFrom := PChar(src) and pTo := PChar(dst)

Hope this clarifies the problem.

Charlie
0
 
ChopinkeCommented:
sorry in the my listing it is DestDirs[j] in the loop and not DestDirs[i]
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now