Solved

renaming a file

Posted on 2001-09-11
8
1,207 Views
Last Modified: 2013-11-20
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.
0
Comment
Question by:Galilea
[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
  • 4
  • 3
8 Comments
 
LVL 32

Accepted Solution

by:
jhance earned 100 total points
ID: 6474012
From the MFC documentation on class CFile:

//example for CFile::Rename
extern char* pOldName;
extern char* pNewName;
TRY
{
    CFile::Rename( pOldName, pNewName );
}
CATCH( CFileException, e )
{
    #ifdef _DEBUG
        afxDump << "File " << pOldName << " not found, cause = "
            << e->m_cause << "\n";
    #endif
}
END_CATCH
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6475622
system("rename fred.dat fred.bin");
0
 
LVL 32

Expert Comment

by:jhance
ID: 6476336
>>>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.
0
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 6

Expert Comment

by:Triskelion
ID: 6476472
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.
0
 

Author Comment

by:Galilea
ID: 6476672
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
0
 
LVL 32

Expert Comment

by:jhance
ID: 6476746
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.)
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6477312
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?
0
 
LVL 32

Expert Comment

by:jhance
ID: 6491888
Hey, I forgot about this one....
0

Featured Post

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

622 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