Solved

renaming a file

Posted on 2001-09-11
8
1,179 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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

778 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