Move a directory

How can I  move a directory?

For example

I have a directory

c:\MyFolder\ABC

that I want to move to the folrde

c:\New

(so I have it as c:\New\ABC)
hulkenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HillGrooverCommented:
Hi hulken

this function should do what you need

uses
  ShellAPI;

function MoveDir(const fromDir, toDir: string): Boolean;
var
  fos: TSHFileOpStruct;
begin
  ZeroMemory(@fos, SizeOf(fos));
  with fos do
  begin
    wFunc  := FO_MOVE;
    fFlags := FOF_FILESONLY;
    pFrom  := PChar(fromDir + #0);
    pTo    := PChar(toDir)
  end;
  Result := (0 = ShFileOperation(fos));
end;

Use it like so

procedure TForm1.Button1Click(Sender: TObject);
begin
   MoveDir('c:\MyFolder\ABC',  'c:\New\ABC');
end;

hope that helps
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mikelittlewoodCommented:
Marking for my own interest  ;o)
0
Pierre CorneliusCommented:
You could also use the MoveFile API function (in Windows unit):

procedure TForm1.Button1Click(Sender: TObject);
var Dir, MoveToDir: pchar;
begin
  dir:= 'C:\Temp\a\';
  MoveToDir:= 'C:\Temp\testMove\';
  if NOT MoveFile(dir, MoveToDir) then RaiseLastWin32Error;
end;

Only thing is the MoveTodir must not already exist and must be on the same drive

Regards
Pierre
0
bernaniCommented:
Also:

Function FolderOperations(
  Action                    : String;  {COPY, DELETE, MOVE, RENAME}
  RenameOnCollision : Boolean; {Renames if directory exists}
  NoConfirmation       : Boolean; {Responds "Yes to All" to any dialogs}
  Silent                     : Boolean; {No progress dialog is shown}
  ShowProgress         : Boolean; {displays progress dialog but no filenames}
  FromDir                  : String;  {From directory}
  ToDir                      : String   {To directory}
  ): Boolean;

var
  SHFileOpStruct : TSHFileOpStruct;
  FromBuf, ToBuf: Array [0..255] of Char;

begin
  Try
    If Not DirectoryExists(FromDir) Then
    Begin
      Result := False;
      Exit;
    End;
    Fillchar(SHFileOpStruct, Sizeof(SHFileOpStruct), 0 );
    FillChar(FromBuf,        Sizeof(FromBuf),        0 );
    FillChar(ToBuf,          Sizeof(ToBuf),          0 );
    StrPCopy(FromBuf,        FromDir);
    StrPCopy(ToBuf,          ToDir);
    With SHFileOpStruct Do
    Begin
      Wnd    := 0;
      If UpperCase(Action) = 'COPY'   Then wFunc := FO_COPY;
      If UpperCase(Action) = 'DELETE' Then wFunc := FO_DELETE;
      If UpperCase(Action) = 'MOVE'   Then wFunc := FO_MOVE;
      If UpperCase(Action) = 'RENAME' Then wFunc := FO_RENAME;
      pFrom  := @FromBuf;
      pTo    := @ToBuf;
      fFlags := FOF_ALLOWUNDO;
      If RenameOnCollision Then fFlags := fFlags or FOF_RENAMEONCOLLISION;
      If NoConfirmation    Then fFlags := fFlags or FOF_NOCONFIRMATION;
      If Silent            Then fFlags := fFlags or FOF_SILENT;
      If ShowProgress      Then fFlags := fFlags or FOF_SIMPLEPROGRESS;
    End;
    Result := (SHFileOperation(SHFileOpStruct) = 0);
  Except
    Result := False;
  End;
end;


To move call:
if FolderOperations('MOVE',true,true,true,true, 'c:\xyz', 'd:\xyz') then ...
To copy call:
if FolderOperations('COPY',true,true,true,true, 'c:\xyz', 'd:\xyz') then ...
To delete call:
if FolderOperations('DELETE',true,true,true,true, 'c:\xyz', 'd:\xyz') then ...
To rename call:
if FolderOperations('RENAME',true,true,true,true, 'c:\xyz', 'd:\xyz') then ...

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.