Solved

shfileoperation

Posted on 2003-11-04
9
318 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 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

14 Experts available now in Live!

Get 1:1 Help Now