Solved

shfileoperation

Posted on 2003-11-04
9
320 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Communication Between RC4 Delphi <-> PHP 3 101
Delphi: how to implement a User Shortcut mapper? 1 95
How to debug For loops? 3 46
SUM 2 INTEGER ARRAYS INTO 1 10 93
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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now