Solved

shfileoperation

Posted on 2003-11-04
9
324 Views
Last Modified: 2010-04-05
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
Comment
Question by:therooster
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 1

Expert Comment

by:Chopinke
ID: 9684385
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
 

Author Comment

by:therooster
ID: 9685161
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
 
LVL 2

Expert Comment

by:Kalroth
ID: 9686041
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
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!

 
LVL 2

Expert Comment

by:Kalroth
ID: 9686076
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
 
LVL 1

Expert Comment

by:Chopinke
ID: 9689041
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
 

Author Comment

by:therooster
ID: 9692299
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
 

Author Comment

by:therooster
ID: 9692326
sorry, the error is cannot copy the parameter is incorrect.. thanks..
0
 
LVL 1

Expert Comment

by:Chopinke
ID: 9694904
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
 
LVL 1

Accepted Solution

by:
Chopinke earned 250 total points
ID: 9694917
sorry in the my listing it is DestDirs[j] in the loop and not DestDirs[i]
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question