Link to home
Start Free TrialLog in
Avatar of pipinana2002
pipinana2002

asked on

Rename directory

Hi all..

I would like to rename a directory.. just say the old directory is C:\Test\old and the new directory is C:\Test\new

The C:\test\old directory consists of a few subdirectories, and the files are kept in these subdirectories.

Can anybody suggest how to do this? I tried to use MoveFile function but I don't know why it's not working.. Thankss..
ASKER CERTIFIED SOLUTION
Avatar of jconde
jconde

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Manuel Lopez-Michelone
Jconde solution should work. You are just renaminng the name of the directory.

best regards
Manuel Lopez (lopem)
Avatar of pipinana2002
pipinana2002

ASKER

Hi...

I have tried it but seems like nothing happened.. Do I need to create or do something first before I use RenameFile function? Maybe I should create the new folder first?

My code is:
RenameFile('C:\Test\old', 'C:\Test\new');

or should I write it for each file? so it would be like:
RenameFile('C:\Test\old\aa.dbf', 'C:\Test\new\aa.dbf');

Thanks for your help :)
Hi!

RenameFile() renames both directories and files ... it works for both!

Just use the function as is ...  If it doesn't work, make sure the directory you're not trying to rename a read-only directory,  that you have adecuate permissions / privileges, etc ...
Hi again..

Hehehe.. can u suggest how to make the folder not a read-only directory? because actually I created the folder using program also.. how to set it not to be read-only? should I do it during the creation of the folder or maybe there's another way? Thankssss :)
can you post the code you used to create the folder ?
Hi again jconde..

when I create the folder, I only use the following code:

ChDir('C:\Test');
CreateDir('NewFolder');

And I think by default it's set to read-only.. :'(

Thanksssss..
Try using:

movefile('C:\Test\old', 'C:\Test\new');

instead of RenameFile that will probably work better in your case!
Hi again jconde..

I already tried using movefile.. but still it's as if nothing had happened.. do i need to set the folder to not read-only also to use movefile function? :'(

Is there someway to set the read only property of the folder to false? :'(
That's pretty strange; I tested using movefile and it worked perfectly on my system (WinXP Pro / D7) ...

I didn't create the directory using createdir but mkdir instead, but mkdir also creates the directory as readonly.
Hi jconde.. I also use winXP, but I use Delphi 6.. But I think it shouldn't be a problem.. Is is because I have many sub directory inside my old directory? But I think it also shouldn't be a problem.. I also have tried using MkDir instead of CreateDir.. but still not working.. very strange.. :'(
I guess why RenameFile did not work, because it worked perfectly even if all the subitems and the directory itself are read-only. Do you open any handles to the directory in your code. If you forget to close any handles you opened previously, you may not be able to modify this folder until the application unloads. For example:

FindFirst('C:\MyFolder', faDirectory, ASearchRec);
...
FindClose(ASearchRec);
Hi again jconde.. I found a pretty strange thing here..

If I create the folder directly (not using program) and I use MoveFile command in the program, the folder can be renamed. But if I create the folder using program, MoveFile function cannot work.. maybe it's the read-only problem, just like when I tried to use RenameFile function?
right, but that doesn't explain why the following code in my system works:

procedure TForm1.Button1Click(Sender: TObject);
begin
  mkdir('c:\dir1');
  movefile('c:\dir1', 'c:\dir2');
end;
BTW, dir1 is being created with read-only attributes

Can you create a new application, place a button and test the above code ?
Hi jconde..

I already tried your code and still doesn't work in my system :'(
Is it because of the delphi version? coz we use the different version...
are you sure that you don't have any opened file in from that directory or its subdirectories, when you try to rename it?
Hi Zhaawz..

yes.. I'm sure that I don't have any opened file.. coz when I only use jconde's code:

procedure TForm1.Button1Click(Sender: TObject);
begin
  mkdir('c:\dir1');
  movefile('c:\dir1', 'c:\dir2');
end;

The folder name won't change..

Anyway, finally I decided to use CopyFileTo function and create a new directory, rather than just changing the folder name, coz I can't find a way to do it..

Thanks for all your help, especiallu jconde who has been very helpful :)
try this "pipinana2002"

procedure RenameDir(DirFrom, DirTo: string);
var
  shellinfo: TSHFileOpStruct;
begin
  with shellinfo do
  begin
    Wnd    := 0;
    wFunc  := FO_RENAME;
    pFrom  := PChar(DirFrom);
    pTo    := PChar(DirTo);
    fFlags := FOF_FILESONLY or FOF_ALLOWUNDO or
              FOF_SILENT or FOF_NOCONFIRMATION;
  end;
  SHFileOperation(shellinfo);
end;

hope it help.
ofcourse don't forget to uses shellapi
now you just need to:
call the procedure
RenameDir('C:\NewFolder', 'C:\OldFolder');
Hi all..

I finally can rename the directory.. My fault.. I forgot to close the program that use it before I rename the dir.. hehe no wonder it cannot be renamed :$

Thanks for all your help :) All of u have been very helpful indeed :)