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..
pipinana2002Asked:
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.

jcondeCommented:
Hi!

What about using

RenameFile('c:\old_directory_name', 'c:\new_directory_name')

?
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
lopemCommented:
Jconde solution should work. You are just renaminng the name of the directory.

best regards
Manuel Lopez (lopem)
0
pipinana2002Author Commented:
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 :)
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

jcondeCommented:
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 ...
0
pipinana2002Author Commented:
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 :)
0
jcondeCommented:
can you post the code you used to create the folder ?
0
pipinana2002Author Commented:
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..
0
jcondeCommented:
Try using:

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

instead of RenameFile that will probably work better in your case!
0
pipinana2002Author Commented:
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? :'(
0
jcondeCommented:
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.
0
pipinana2002Author Commented:
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.. :'(
0
hacibumbalaCommented:
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);
0
pipinana2002Author Commented:
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?
0
jcondeCommented:
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;
0
jcondeCommented:
BTW, dir1 is being created with read-only attributes

Can you create a new application, place a button and test the above code ?
0
pipinana2002Author Commented:
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...
0
ZhaawZSoftware DeveloperCommented:
are you sure that you don't have any opened file in from that directory or its subdirectories, when you try to rename it?
0
pipinana2002Author Commented:
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 :)
0
TheLeaderCommented:
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.
0
TheLeaderCommented:
ofcourse don't forget to uses shellapi
now you just need to:
call the procedure
RenameDir('C:\NewFolder', 'C:\OldFolder');
0
pipinana2002Author Commented:
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 :)
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.