This page was last modified 12:03, 12 December 2007.
File Operations
From Forum Nokia Wiki
This article contains different functions that can be operated on File in Symbian C++. Functions can be categorized as Copy(), Delete(), Rename(), Move() etc...
Header File LFileAll.h
#include <f32file.h> #include <bautils.h> #include <aknnotewrappers.h> #include <e32base.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"); class CLFileAll : public CBase { public: void CallDelete(); void CallRename(); void CallMove(); void CallCopy(); void CallFileExists(); //CFileMan TBool Delete(const TDesC &aName, TUint aSwitch=0); //Rfs: Renames a Single File Or Directory TBool Rename(const TDesC &anOldName, const TDesC &aNewName); //CFileName TBool Move(const TDesC &anOld, const TDesC &aNew); //CFileMan TBool Copy(const TDesC &anOld, const TDesC &aNew);//, TUint aSwitch=EOverWrite); //BAFLUtils TBool TestFileExists(const RFs &aFs, const TDesC &aFileName); };
Source File LFileAll.cpp
#include "LFileAll.h" void CLFileAll::CallDelete() { CAknInformationNote* iInfo = new (ELeave) CAknInformationNote; TBuf16<30> iFilePath; iFilePath.Copy(KFilePathDelete); TBool result=Delete(iFilePath); if(result) iInfo->ExecuteLD(_L("File Deleted")); else iInfo->ExecuteLD(_L("File Not Deleted")); } void CLFileAll::CallRename() { CAknInformationNote* iInfo = new (ELeave) CAknInformationNote; TBuf16<30> iFilePathOld; iFilePathOld.Copy(KFilePathOldRename); TBuf16<30> iFilePathNew; iFilePathNew.Copy(KFilePathNewRename); TBool result=Rename(iFilePathOld,iFilePathNew); if(result) iInfo->ExecuteLD(_L("File Renamed")); else iInfo->ExecuteLD(_L("File Not Renamed")); } void CLFileAll::CallMove() { CAknInformationNote* iInfo = new (ELeave) CAknInformationNote; TBuf16<30> iFilePathOld; iFilePathOld.Copy(KFilePathOldMove); TBuf16<30> iFilePathNew; iFilePathNew.Copy(KFilePathNewMove); TBool result=Move(iFilePathOld,iFilePathNew); if(result) iInfo->ExecuteLD(_L("File Moved")); else iInfo->ExecuteLD(_L("File Not Moved")); } void CLFileAll::CallCopy() { CAknInformationNote* iInfo=new (ELeave) CAknInformationNote; TBuf16<30> iFilePathOld; iFilePathOld.Copy(KFilePathOldCopy); TBuf16<30> iFilePathNew; iFilePathNew.Copy(KFilePathNewCopy); TBool result=Copy(iFilePathOld,iFilePathNew); if(result) iInfo->ExecuteLD(_L("File Copied")); else iInfo->ExecuteLD(_L("File Not Copied")); } void CLFileAll::CallFileExists() { CAknInformationNote* iInfo=new (ELeave) CAknInformationNote; TBuf16<30> iFileName; iFileName.Copy(KFileName); RFs iFs; iFs.Connect(); TBool result=TestFileExists(iFs,iFileName); iFs.Close(); if(result) iInfo->ExecuteLD(_L("File Exists")); else iInfo->ExecuteLD(_L("File Doesn't Exist")); } TBool CLFileAll::Delete(const TDesC &aName,TUint aSwitch) { RFs iFs; iFs.Connect(); CFileMan* iFileMan=CFileMan::NewL(iFs); CleanupStack::PushL(iFileMan); TInt iErr=iFileMan->Delete(aName,aSwitch); CleanupStack::PopAndDestroy(iFileMan); iFs.Close(); if(iErr==KErrNone) return ETrue; else return EFalse; } TBool CLFileAll::Rename(const TDesC &anOldName,const TDesC &aNewName) { RFs iFs; iFs.Connect(); TInt iErr=iFs.Rename(anOldName,aNewName); iFs.Close(); if(iErr==KErrNone) return ETrue; else return EFalse; } TBool CLFileAll::Move(const TDesC &anOld, const TDesC &aNew) { RFs iFs; iFs.Connect(); CFileMan* iFileMan=CFileMan::NewL(iFs); CleanupStack::PushL(iFileMan); TInt iErr=iFileMan->Move(anOld,aNew); CleanupStack::PopAndDestroy(iFileMan); iFs.Close(); if(iErr==KErrNone) return ETrue; else return EFalse; } TBool CLFileAll::Copy(const TDesC &anOld,const TDesC &aNew) { RFs iFs; iFs.Connect(); CFileMan* iFileMan=CFileMan::NewL(iFs); CleanupStack::PushL(iFileMan); TInt iErr=iFileMan->Copy(anOld,aNew); CleanupStack::PopAndDestroy(iFileMan); iFs.Close(); if(iErr==KErrNone) return ETrue; else return EFalse; } TBool CLFileAll::TestFileExists(const RFs &aFs,const TDesC &aFileName) { TBool result=BaflUtils::FileExists(aFs,aFileName); if(result) return ETrue; else return EFalse; }
- Paste LFileAll.h header file in your inc folder. - Paste LFileAll.cppsource file in your src folder. - Include this LFileAll.h your CYrAppUi.h file.
Like:#include <LFileAll.h>
- Declare an object in your CYrAppUi.h file.Like:
.... .....private: // Data CLFileAll* iFileAll; ....
- 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: { iFileAll->CallDelete(); } break; case ELFileOperationsRenameFile: { iFileAll->CallRename(); } break; case ELFileOperationsMoveFile: { iFileAll->CallMove(); } break; case ELFileOperationsCopyFile: { iFileAll->CallCopy(); } break; case ELFileOperationsFileExists: { iFileAll->CallFileExists(); } break; default: break; } }
NOTE: You need to add efsrv.lib, bafl.lib in your .mmp file.
| Related Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How I can make simple mathematical operations on the contents of a buffer of type Tbu | abrahim | General Symbian C++ | 5 | 2003-04-16 10:49 |
| Open C++: Cost of using standard file streams | rshaag | Open C | 2 | 2008-06-22 07:55 |
| Float (FPU) in N93? | alcabo | General Symbian C++ | 16 | 2007-11-01 06:49 |
| Problem with persisting data into the rms | ionutdavid_eu | Mobile Java General | 2 | 2008-01-11 08:03 |
| nextElement() vs elementAt(), which is better ? | akokchai | Mobile Java General | 2 | 2007-06-29 14:43 |
