Solved

renaming a file

Posted on 2001-09-11
8
1,166 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Detect CR LF to each line 12 153
Shell script to login to remote m/c and execute commands 10 78
wordcount challenge 11 108
Sed question 2 68
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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 Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now