Solved

renaming a file

Posted on 2001-09-11
8
1,188 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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 create frequencies of a variable from SAS dataset? 10 137
ffmpeg - "rtsp://...... Operation not permitted" 4 87
zeroFront challenge 7 121
x-bar in Google Sheets 2 91
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

679 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