Solved

Rename directory

Posted on 2003-11-20
21
1,126 Views
Last Modified: 2010-04-05
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..
0
Comment
Question by:pipinana2002
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 7
  • 2
  • +3
21 Comments
 
LVL 7

Accepted Solution

by:
jconde earned 50 total points
ID: 9793784
Hi!

What about using

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

?
0
 
LVL 3

Expert Comment

by:lopem
ID: 9793836
Jconde solution should work. You are just renaminng the name of the directory.

best regards
Manuel Lopez (lopem)
0
 

Author Comment

by:pipinana2002
ID: 9793940
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:jconde
ID: 9794114
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
 

Author Comment

by:pipinana2002
ID: 9794810
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
 
LVL 7

Expert Comment

by:jconde
ID: 9794892
can you post the code you used to create the folder ?
0
 

Author Comment

by:pipinana2002
ID: 9794949
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
 
LVL 7

Expert Comment

by:jconde
ID: 9795196
Try using:

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

instead of RenameFile that will probably work better in your case!
0
 

Author Comment

by:pipinana2002
ID: 9795250
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
 
LVL 7

Expert Comment

by:jconde
ID: 9795286
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
 

Author Comment

by:pipinana2002
ID: 9795342
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
 
LVL 1

Expert Comment

by:hacibumbala
ID: 9795369
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
 

Author Comment

by:pipinana2002
ID: 9795374
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
 
LVL 7

Expert Comment

by:jconde
ID: 9795379
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
 
LVL 7

Expert Comment

by:jconde
ID: 9795384
BTW, dir1 is being created with read-only attributes

Can you create a new application, place a button and test the above code ?
0
 

Author Comment

by:pipinana2002
ID: 9795566
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
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 9796036
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
 

Author Comment

by:pipinana2002
ID: 9797772
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
 
LVL 2

Expert Comment

by:TheLeader
ID: 9801050
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
 
LVL 2

Expert Comment

by:TheLeader
ID: 9801062
ofcourse don't forget to uses shellapi
now you just need to:
call the procedure
RenameDir('C:\NewFolder', 'C:\OldFolder');
0
 

Author Comment

by:pipinana2002
ID: 9807858
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

729 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