Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to change the filename of a file

Status
Not open for further replies.

Bachatero

Programmer
Aug 30, 2006
36
NL
Hi all,

I would like to rename a lot of file names.

I use the following command to extract the file name:
Code:
MyFileName = ExtractFileName(OpenDialog->FileName);

So, if I change the variable MyFileName, how can I update the filename of the file itself.

My idea is:

1) Open the file.
2) Extract the file name.
3) Change the file name.
4) ?????????.
5) Close the file.

Does someone have any idea which steps I should follow at item 4 to achieve this.

Thanks in advance.

Johan
 
I'm not certain what step 4 should be. I see one problem with your scenario though. If you open the file, you cannot rename it until it is closed, so step 5 should go before step 3.

I would do it slightly differently...
[ol]
[li]Iterate through each file putting the file name in a list or vector or something similar[/li]
[li]Re-iterate through the list or vector and rename each file.[/li]
[/ol]

This would eliminate a couple of potential errors. First, it would eliminate the open file error I described above. Second, it would eliminate any problem with encountering the same file multiple times, before you rename it and again after you rename it. This second problem could put you into an endless loop.

It would also be faster since you don't have to open the file. Somewhere in forum101 are the steps on how to iterate through files. If you can't find the thread, post back and I'll put a FAQ in that thread.


James P. Cottingham
-----------------------------------------
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top