File manipulation in Symbian C++
This article demonstrates how to perform common file operations using the Symbian C++ CFileMan class. This includes operations like file copy, delete, rename, move files etc.
Article Metadata
Libraries:
LIBRARY efsrv.lib, bafl.lib
Header File: FileOpers.h
#include <e32base.h>
class FileOpers
{
public:
static void CallDelete();
static void CallRename();
static void CallMove();
static void CallCopy();
static void CallFileExists();
//CFileMan
static TBool Delete(const TDesC &aName, TUint aSwitch=0);
//Rfs: Renames a Single File Or Directory
static TBool Rename(const TDesC &anOldName, const TDesC &aNewName);
//CFileName
static TBool Move(const TDesC &anOld, const TDesC &aNew);
//CFileMan
static TBool Copy(const TDesC &anOld,
const TDesC &aNew);//, TUint aSwitch=EOverWrite);
//BAFLUtils
static TBool TestFileExists(const TDesC &aFileName);
};
Source File: FileOpers.cpp
#include <bautils.h>
#include <aknnotewrappers.h>
#include "FileOpers.h"
//Change as per your requirement
_LIT(KFilePathDelete,"\\System\\Test.txt");
_LIT(KFilePathOldRename,"\\System\\Test.txt");
_LIT(KFilePathNewRename,"\\System\\Test123.txt");
_LIT(KFilePathOldMove,"\\System\\Test.txt");
_LIT(KFilePathNewMove,"\\System\\Data\\");
_LIT(KFilePathOldCopy,"\\System\\Test.txt");
_LIT(KFilePathNewCopy,"\\System\\Data\\");
//FileExists()- Change as per your requirement
_LIT(KFileName,"\\System\\Test.txt");
void FileOpers::CallDelete()
{
CAknInformationNote* info = new (ELeave) CAknInformationNote;
TBuf16<30filePath;
filePath.Copy(KFilePathDelete);
if(FileOpers::Delete(filePath))
info->ExecuteLD(_L("File Deleted"));
else
info->ExecuteLD(_L("File Not Deleted"));
}
void FileOpers::CallRename()
{
CAknInformationNote* info = new (ELeave) CAknInformationNote;
TBuf16<30filePathOld;
filePathOld.Copy(KFilePathOldRename);
TBuf16<30filePathNew;
filePathNew.Copy(KFilePathNewRename);
if(FileOpers::Rename(filePathOld,filePathNew))
info->ExecuteLD(_L("File Renamed"));
else
info->ExecuteLD(_L("File Not Renamed"));
}
void FileOpers::CallMove()
{
CAknInformationNote* info = new (ELeave) CAknInformationNote;
TBuf16<30filePathOld;
filePathOld.Copy(KFilePathOldMove);
TBuf16<30filePathNew;
filePathNew.Copy(KFilePathNewMove);
if(FileOpers::Move(filePathOld,filePathNew))
info->ExecuteLD(_L("File Moved"));
else
info->ExecuteLD(_L("File Not Moved"));
}
void FileOpers::CallCopy()
{
CAknInformationNote* info=new (ELeave) CAknInformationNote;
TBuf16<30filePathOld;
filePathOld.Copy(KFilePathOldCopy);
TBuf16<30filePathNew;
filePathNew.Copy(KFilePathNewCopy);
if(FileOpers::Copy(filePathOld,filePathNew))
info->ExecuteLD(_L("File Copied"));
else
info->ExecuteLD(_L("File Not Copied"));
}
void FileOpers::CallFileExists()
{
CAknInformationNote* info=new (ELeave) CAknInformationNote;
TBuf16<30fileName;
fileName.Copy(KFileName);
if(FileOpers::TestFileExists(fileName))
info->ExecuteLD(_L("File Exists"));
else
info->ExecuteLD(_L("File Doesn't Exist"));
}
TBool FileOpers::Delete(const TDesC &aName,TUint aSwitch)
{
RFs fs;
fs.Connect();
CFileMan* fileMan=CFileMan::NewL(fs);
CleanupStack::PushL(fileMan);
TInt result = fileMan->Delete(aName,aSwitch);
CleanupStack::PopAndDestroy(fileMan);
fs.Close();
return result == KErrNone;
}
TBool FileOpers::Rename(const TDesC &anOldName,const TDesC &aNewName)
{
RFs fs;
fs.Connect();
TInt result = fs.Rename(anOldName,aNewName);
fs.Close();
return result == KErrNone;
}
TBool FileOpers::Move(const TDesC &anOld, const TDesC &aNew)
{
RFs fs;
fs.Connect();
CFileMan* fileMan=CFileMan::NewL(fs);
CleanupStack::PushL(fileMan);
TInt result=fileMan->Move(anOld,aNew);
CleanupStack::PopAndDestroy(fileMan);
fs.Close();
return result == KErrNone;
}
TBool FileOpers::Copy(const TDesC &anOld,const TDesC &aNew)
{
RFs fs;
fs.Connect();
CFileMan* fileMan=CFileMan::NewL(fs);
CleanupStack::PushL(fileMan);
TInt result=fileMan->Copy(anOld,aNew);
CleanupStack::PopAndDestroy(fileMan);
fs.Close();
return result==KErrNone;
}
TBool FileOpers::TestFileExists(const TDesC &aFileName)
{
RFs fs;
fs.Connect();
TBool result = BaflUtils::FileExists(fs,aFileName);
fs.Close();
return result;
}
- Paste FileOpers.h header file in your inc folder.
- Paste FileOpers.cppsource file in your src folder.
- Include this FileOpers.h in your CYrAppUi.cpp file. Like:
#include "FileOpers.h"
- Finally call these fucntions from the HandleCommand() from CYrAppUi.Cpp file. Like:
void CYrAppUi::HandleCommandL( TInt aCommand )
{
switch( aCommand )
{
case EEikCmdExit:
case EAknSoftkeyExit:
Exit();
break;
case ELFileOperationsDeleteFile:
FileOpers::CallDelete();
break;
case ELFileOperationsRenameFile:
FileOpers::CallRename();
break;
case ELFileOperationsMoveFile:
FileOpers::CallMove();
break;
case ELFileOperationsCopyFile:
FileOpers::CallCopy();
break;
case ELFileOperationsFileExists:
FileOpers::CallFileExists();
break;
default:
break;
}
}


08 Sep
2009
APIs for working with files are very important. Many complex applications require such functionality. Symbian C++ provides several classes for working with files, all these classes use specialized application server (efile.exe, UID: 0x100039e3) for the implementation of certain tasks. Each connection to file server is based on the object of the class RFs.
This article demonstrates how to implement functions for the basic file operations in synchronous mode. The source code is very simple which simplifies understanding. The class contains special functions that show the information messages as a result of operation execution - such approach will allow beginners quickly understand how it works. This class can be used as a base for implementing of more advanced functionality for working with files.