Are you able to find an API to rename a file or directory? Have you checked for it?
So how we could rename a file in Windows? Have you ever noticed MoveFile API in Windows? It can be used for the same purpose.
Here’s the code snippet. Hope this is a new information for some beginners.
BOOL RenameFile(const char* pszCurrFullPath,const char* pszNewFullPath)
{
ASSERT(pszCurrFullPath);
ASSERT(pszNewFullPath);
return MoveFile(pszCurrFullPath,pszNewFullPath);
}
int main()
{
// Renaming (Moving) a file in current directory
if(!RenameFile(“test.txt”,”new test.txt”))
cout<<”Failed to Rename the specified file”<<endl;
// Renaming a file at the specified path
if(!RenameFile(“c:\\test.txt”,”c:\\new test.txt”))
{
cout<<”Failed to Rename the specified file”<<endl;
}
// Rename directory
if(!RenameFile(“c:\\test”,”c:\\new test”))
cout<<”Failed to Rename the directory”<<endl;
// Error it is not possible to move a directory from another volume using MoveFile
if(!RenameFile(“c:\\test”,”d:\\new test”))
cout<<”Failed to Rename the directory”<<endl;
return 0;
}