Recent

Author Topic: problem by moving folders  (Read 4187 times)

Ericktux

  • Sr. Member
  • ****
  • Posts: 345
problem by moving folders
« on: August 28, 2016, 07:12:23 am »
hi my friends, I use this code for move folders or files:

Code: Pascal  [Select][+][-]
  1. uses
  2.   windows, ShellApi;
  3.  
  4. function ShellFileOperation(hWndOwner: HWND; const fromFileOrFolder, toFileOrFolder: string;
  5.   Flag: integer): boolean;
  6. var
  7.   shellinfo: TSHFileOpStruct;
  8.   str: ansistring;
  9. begin
  10.   FillChar(shellinfo, sizeof(shellinfo), 0);
  11.   with shellinfo do
  12.   begin
  13.     wnd := hWndOwner;
  14.     wFunc := Flag; //FO_MOVE, FO_COPY, FO_DELETE or FO_RENAME
  15.     pFrom := PAnsiChar(fromFileOrFolder+ #0#0);
  16.     pTo := PAnsiChar(toFileOrFolder+ #0#0);
  17.     fFlags := FOF_SIMPLEPROGRESS;
  18.   end;
  19.   Result := SHFileOperation(shellinfo) = 0;
  20. end;

use:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.       // moving folders
  4.       if not ShellFileOperation(Handle, 'C:\Users\juanito\Desktop\bebe', 'e:\bebe', FO_MOVE) then  
  5.       ShowMessage('error');
  6. end;

working well, but if use ñ or accent (á, é, í, etc):

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.       // moving folders
  4.       if not ShellFileOperation(Handle, 'C:\Users\juanito\Desktop\acción', 'e:\acción', FO_MOVE) then  
  5.       ShowMessage('error');
  6. end;

then it shows me error.

any help  :(

balazsszekely

  • Guest
Re: problem by moving folders
« Reply #1 on: August 28, 2016, 07:52:29 am »
Here you go:

Code: Pascal  [Select][+][-]
  1. uses
  2.   windows, ShellApi;
  3.  
  4. function ShellFileOperation(hWndOwner: HWND; const fromFileOrFolder, toFileOrFolder: WideString; Flag: Integer): Boolean;
  5. var
  6.   ShellInfo: TSHFileOpStructW;
  7. begin
  8.   FillChar(ShellInfo, SizeOf(ShellInfo), 0);
  9.   with ShellInfo do
  10.   begin
  11.     wnd := hWndOwner;
  12.     wFunc := Flag;
  13.     pFrom := PWideChar(fromFileOrFolder);
  14.     pTo := PWideChar(toFileOrFolder);
  15.     fFlags := FOF_SIMPLEPROGRESS;
  16.   end;
  17.   Result := SHFileOperationW(@ShellInfo) = 0;
  18. end;
  19.  
  20. procedure TForm1.Button1Click(Sender: TObject);
  21. begin
  22.    if not ShellFileOperation(Handle, WideString('C:\Users\juanito\Desktop\acción'), WideString('e:\acción'), FO_MOVE) then
  23.       ShowMessage('error');
  24. end;

Ericktux

  • Sr. Member
  • ****
  • Posts: 345
Re: problem by moving folders
« Reply #2 on: August 28, 2016, 08:16:34 am »
thanks friend getmem, but get same error  :(

Code: Pascal  [Select][+][-]
  1. function ShellFileOperation(hWndOwner: HWND; const fromFileOrFolder, toFileOrFolder: WideString; Flag: Integer): Boolean;
  2. var
  3.   ShellInfo: TSHFileOpStructW;
  4. begin
  5.   FillChar(ShellInfo, SizeOf(ShellInfo), 0);
  6.   with ShellInfo do
  7.   begin
  8.     wnd := hWndOwner;
  9.     wFunc := Flag;
  10.     pFrom := PWideChar(fromFileOrFolder);
  11.     pTo := PWideChar(toFileOrFolder);
  12.     fFlags := FOF_SIMPLEPROGRESS;
  13.   end;
  14.   Result := SHFileOperationW(@ShellInfo) = 0;
  15. end;
  16.  
  17. procedure TForm1.Button1Click(Sender: TObject);
  18. begin
  19.    if not ShellFileOperation(Handle, WideString('C:\Users\juanito\Desktop\acción'), WideString('e:\acción'), FO_MOVE) then
  20.       ShowMessage('error');
  21. end;

help  :(

balazsszekely

  • Guest
Re: problem by moving folders
« Reply #3 on: August 28, 2016, 08:27:20 am »
And what is the exact error?
Code: Pascal  [Select][+][-]
  1.   ShowMessage(SysErrorMessage(GetLastError));
or
Code: Pascal  [Select][+][-]
  1. ShowMessage(SysErrorMessage(GetLastOSError));

Ericktux

  • Sr. Member
  • ****
  • Posts: 345
Re: problem by moving folders
« Reply #4 on: August 28, 2016, 08:36:07 am »
this is the error:

"Controlador no v?lido"

in english:

"driver not valid"

balazsszekely

  • Guest
Re: problem by moving folders
« Reply #5 on: August 28, 2016, 08:44:15 am »
Ok,  try this one. This will work if the paths are valid:
Code: Pascal  [Select][+][-]
  1. uses
  2.   windows, ShellApi, LazUTF8;
  3.  
  4. function ShellFileOperation(hWndOwner: HWND; const fromFileOrFolder, toFileOrFolder: WideString; Flag: Integer): Boolean;
  5. var
  6.   ShellInfo: TSHFileOpStructW;
  7. begin
  8.   FillChar(ShellInfo, SizeOf(ShellInfo), 0);
  9.   with ShellInfo do
  10.   begin
  11.     wnd := hWndOwner;
  12.     wFunc := Flag;
  13.     pFrom := PWideChar(fromFileOrFolder);
  14.     pTo := PWideChar(toFileOrFolder);
  15.     fFlags := FOF_SIMPLEPROGRESS;
  16.   end;
  17.   Result := SHFileOperationW(@ShellInfo) = 0;
  18. end;
  19.  
  20. procedure TForm1.Button1Click(Sender: TObject);
  21. begin
  22.   if not ShellFileOperation(Handle, UTF8ToUTF16('C:\Users\juanito\Desktop\acción'), UTF8ToUTF16('e:\acción'), FO_MOVE) then
  23.      ShowMessage(SysErrorMessage(GetLastOSError));
  24. end;

Ericktux

  • Sr. Member
  • ****
  • Posts: 345
Re: problem by moving folders
« Reply #6 on: August 28, 2016, 09:04:08 am »
thank for help me:

I get the following error
(attached image)

Ericktux

  • Sr. Member
  • ****
  • Posts: 345
Re: problem by moving folders
« Reply #7 on: August 28, 2016, 09:11:20 am »
testing the following modification, this works  :)

Code: Pascal  [Select][+][-]
  1. uses
  2.   windows, ShellApi, LazUTF8;
  3.  
  4. function ShellFileOperation(hWndOwner: HWND; const fromFileOrFolder, toFileOrFolder: WideString; Flag: Integer): Boolean;
  5. var
  6.   ShellInfo: TSHFileOpStructW;
  7. begin
  8.   FillChar(ShellInfo, SizeOf(ShellInfo), 0);
  9.   with ShellInfo do
  10.   begin
  11.     wnd := hWndOwner;
  12.     wFunc := Flag;
  13.     pFrom := PWideChar(fromFileOrFolder+ #0#0);  // edited here
  14.     pTo := PWideChar(toFileOrFolder+ #0#0);      // edited here
  15.     fFlags := FOF_SIMPLEPROGRESS;
  16.   end;
  17.   Result := SHFileOperationW(@ShellInfo) = 0;
  18. end;  
  19.  
  20. procedure TForm1.Button1Click(Sender: TObject);
  21. begin
  22.      if not ShellFileOperation(Handle, UTF8ToUTF16('C:\Users\juanito\Desktop\acción'), UTF8ToUTF16('e:\acción'), FO_MOVE) then
  23.      ShowMessage(SysErrorMessage(GetLastOSError));
  24. end;

It works for now  :)

balazsszekely

  • Guest
Re: problem by moving folders
« Reply #8 on: August 28, 2016, 09:23:49 am »
You should also add the FOF_NOCONFIRMMKDIR flag. Please test this:
Code: Pascal  [Select][+][-]
  1. uses
  2.   windows, ShellApi, LazUTF8;
  3.  
  4. function ShellFileOperation(hWndOwner: HWND; const fromFileOrFolder, toFileOrFolder: WideString; Func, Flag: Integer): Boolean;
  5. var
  6.   ShellInfo: TSHFileOpStructW;
  7. begin
  8.   FillChar(ShellInfo, SizeOf(ShellInfo), 0);
  9.   with ShellInfo do
  10.   begin
  11.     wnd := hWndOwner;
  12.     wFunc := Func;
  13.     fFlags := Flag;
  14.     pFrom := PWideChar(fromFileOrFolder+ #0#0);
  15.     pTo := PWideChar(toFileOrFolder+ #0#0);
  16.   end;
  17.   Result := SHFileOperationW(@ShellInfo) = 0;
  18. end;
  19.  
  20. procedure TForm1.Button1Click(Sender: TObject);
  21. begin
  22.    if not ShellFileOperation(Handle,
  23.                              UTF8ToUTF16('C:\Users\juanito\Desktop\acción'),
  24.                              UTF8ToUTF16('e:\acción'),
  25.                              FO_MOVE, FOF_NOCONFIRMMKDIR or FOF_SIMPLEPROGRESS) then
  26.       ShowMessage(SysErrorMessage(GetLastOSError));
  27. end;            

Fungus

  • Sr. Member
  • ****
  • Posts: 353
Re: problem by moving folders
« Reply #9 on: August 28, 2016, 11:27:43 am »
The pFrom and pTo members of the SHFILEOPSTRUCT can contain multiple null-terminated file names and they are terminated with an empty string, like this: path#0path#0path#0#0

https://msdn.microsoft.com/en-us/library/windows/desktop/bb759795%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396

That is why adding #0#0 to the file name works :-)

 

TinyPortal © 2005-2018