?
Solved

shfileoperation

Posted on 2003-11-04
9
Medium Priority
?
327 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 1000 total points
ID: 9694917
sorry in the my listing it is DestDirs[j] in the loop and not DestDirs[i]
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

752 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