riccohb
asked on
Moving files & folders
I have the following code in my app:
with FOS do
begin
Wnd := Self.Handle;
wFunc := FO_MOVE;
pFrom := PChar(UserDrive);
pTo := PChar(Archive);
fFlags := FOF_ALLOWUNDO;
end;
SHFileOperation(FOS);
The variable UserDrive points to a network folder (ie. \\server1\users\testuser) and Archive points to a local folder (ie D:\Archive). This procedure fails whatever is in the source folder with the error message "Cannot move I: Cannot find the file specified"
Needless to say, there isn't a file called I. Can anyone help?
with FOS do
begin
Wnd := Self.Handle;
wFunc := FO_MOVE;
pFrom := PChar(UserDrive);
pTo := PChar(Archive);
fFlags := FOF_ALLOWUNDO;
end;
SHFileOperation(FOS);
The variable UserDrive points to a network folder (ie. \\server1\users\testuser) and Archive points to a local folder (ie D:\Archive). This procedure fails whatever is in the source folder with the error message "Cannot move I: Cannot find the file specified"
Needless to say, there isn't a file called I. Can anyone help?
dir you try to use it for files instead of folders?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I test it using real PCHar and it works:
procedure TForm1.Button1Click(Sender : TObject);
var
FOS: _SHFILEOPSTRUCTA;
UserDrive, Archive: PChar;
begin
UserDrive:= StrAlloc(200);
Archive:= StrAlloc(200);
StrCopy(Archive, 'C:\Motaz\Temp');
StrCopy(UserDrive, 'd:\test');
with FOS do
begin
Wnd := Self.Handle;
wFunc := FO_MOVE;
pFrom := UserDrive;
pTo := Archive;
fFlags := FOF_ALLOWUNDO;
end;
SHFileOperation(FOS);
end;
procedure TForm1.Button1Click(Sender
var
FOS: _SHFILEOPSTRUCTA;
UserDrive, Archive: PChar;
begin
UserDrive:= StrAlloc(200);
Archive:= StrAlloc(200);
StrCopy(Archive, 'C:\Motaz\Temp');
StrCopy(UserDrive, 'd:\test');
with FOS do
begin
Wnd := Self.Handle;
wFunc := FO_MOVE;
pFrom := UserDrive;
pTo := Archive;
fFlags := FOF_ALLOWUNDO;
end;
SHFileOperation(FOS);
end;
I read this in MSDN documentation for SHFileOperation... I could have made a mistake, sorry. I'm quite busy at the moment, perhaps I didn't read carefully
ASKER
I think it might be something to do with the fact that I'm copying from a network drive specified by \\servername\d$\foldername . If I manually map a drive to this address and run SHFileOperation via the mapped drive letter it works.
I've used Motaz's amended code without using a mapped drive letter, and the error comes back.
I've used Motaz's amended code without using a mapped drive letter, and the error comes back.
Hi,
There are two possible reasons at least:
1. You do not write the correct path '\\server\path'.
2 You do not have permisions on that folder.
The code works fine with such syntax.
Regards, Geo
There are two possible reasons at least:
1. You do not write the correct path '\\server\path'.
2 You do not have permisions on that folder.
The code works fine with such syntax.
Regards, Geo
ASKER
It's definitely the correct path, and I have Domain Admin permissions. Domain Admins have full control of the folder. The code works in so far that it successfully moves all the files and folders from the source to the destination - it's just that once it's done that it looks like it tries to copy one extra file with a nonsense filename (eg |A?).
TOndrej is right about the double termination. Often your string will be followed by zeros because of uninitialized memory, but you really should add that extra #0.