Solved

Rename directory

Posted on 2003-11-20
21
1,117 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to draw animated progressbar in Tvirtualstringtree 5 117
add combobox item based on numbers 9 125
oracle global variables 4 63
Reconfigure Delphi Install? 2 46
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

862 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now