Solved

Rename directory

Posted on 2003-11-20
21
1,116 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 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

18 Experts available now in Live!

Get 1:1 Help Now