Link to home
Start Free TrialLog in
Avatar of Galilea
Galilea

asked on

renaming a file

Would someone be so kind so as to tell me of a nice/tidy way of renaming a file? (i.e open an existing one and saving the data with another name) At the moment my code looks pretty disgusting and old-style.

Thank you.
ASKER CERTIFIED SOLUTION
Avatar of jhance
jhance

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
system("rename fred.dat fred.bin");
Avatar of jhance
jhance

>>>system("rename fred.dat fred.bin");

What would you do it this way?  

1) There is no error notification or recovery.

2) It will cause a MSDOS window to annoyingly flash on the screen.

3) It is slow and resource intensive since a new process must be started to do this simple task.

4) MFC provides the CFile::Rename() member function and the Windows API provides the DeleteFile() API, either of which are MUCH more appropriate for use from within a program.
jhance, it's just another option.
It it not the most efficient method.
In a console app, there are no windowing issues.
I would prefer the CFile::Rename() method.
It's just another option.
Avatar of Galilea

ASKER

The problem is that my file is not in the same directory as the program, in fact it is on another computer.

I have this oscilloscope that is acquiring data and which I program through a GPIB PCMCIA.

Any ideas on that?

Thank you
You can specifiy the ENTIRE pathname to the CFile::Rename() function.  Do:

CFile::Rename("C:\\Program Files\\Some Folder\\SomeFile.txt", "C:\\Windows\\Temp\\SomeNewFile.txt");

The GPIB has nothing to do with this.  Just call CFile::Rename()...

(I'm ASSUMING, of course, that you are using MFC since you asked this in the MFC topic area.)
Is the other computer treated like a file server (with a drive letter-n-such)?  If so, try the suggestions above.  If not (and you do have write access), use the UNC name in the command
Using some rename method
...("\\SOME_MACHINE_NAME\SomeFolder\File.dat ...

How are you accessing the file?
Hey, I forgot about this one....