[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Move a directory

Posted on 2006-04-04
4
Medium Priority
?
363 Views
Last Modified: 2010-04-05
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)
0
Comment
Question by:hulken
4 Comments
 
LVL 4

Accepted Solution

by:
HillGroover earned 800 total points
ID: 16371293
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
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 16372112
Marking for my own interest  ;o)
0
 
LVL 14

Assisted Solution

by:Pierre Cornelius
Pierre Cornelius earned 400 total points
ID: 16373046
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
 
LVL 9

Assisted Solution

by:bernani
bernani earned 800 total points
ID: 16375831
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month17 days, 23 hours left to enroll

831 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