C++ Builder: File OperationsFileExists(FileName) You may want to check first whether a file exists. If it does, then you may want to copy, move, or delete it. TFile::Copy(FileName, NewFileName, Overwrite) TFile::Move(FileName, NewFileName) TFile::Delete(FileName) Parameters of the functions:
Directory operationsTDirectory::Exists(Path, FollowLink) checks whether a given directory
exists. It returns true if the given
path exists and is a directory, and false otherwise. TDirectory::CreateDirectory(Path) creates a new directory (folder). If the directories given in Path do not yet exist, CreateDirectory attempts to create them. TDirectory::Delete(Path, Recursive) deletes a directory. If the parameter Recursive is false, nonempty directories will not be deleted. TDirectory::Copy(Source, Destination) copies a directory. Important notes
ExamplesCopy a file: void __fastcall TForm1::btnCopyClick(TObject *Sender) { String Source = "D:\\Test\\File1.txt"; String Dest = "D:\\Test\\File2.txt"; if (FileExists(Source)) TFile::Copy(Source, Dest, true); else ShowMessage("The source file does not exist"); } Move a file: void __fastcall TForm1::btnMoveClick(TObject *Sender) { String OldName = "D:\\Test\\File1.txt"; String NewName = "D:\\Test\\File2.txt"; if (!FileExists(OldName)) ShowMessage("The file does not exist"); else if (FileExists(NewName)) ShowMessage("The destination file already exists"); else TFile::Move(OldName, NewName); } Delete a file: void __fastcall TForm1::btnDeleteClick(TObject *Sender) { Create a directory: void __fastcall TForm1::btnCreateDirClick(TObject *Sender) { String Dir = "D:\\Test\\Test2"; TDirectory::CreateDirectory(Dir); } Delete a directory: void __fastcall TForm1::btnDeleteDirClick(TObject *Sender) { String Dir = "D:\\Test\\Test2"; if (TDirectory::Exists(Dir, false)) TDirectory::Delete(Dir, true); else ShowMessage("The directory does not exist"); } Copy a directory: void __fastcall TForm1::btnCopyDirClick(TObject *Sender) { String Source = "D:\\Test"; String Dest = "D:\\Test2"; if (TDirectory::Exists(Source, false)) TDirectory::Copy(Source, Dest); else ShowMessage("The directory does not exist"); } |