If you have an existing file you don't need anymore, you can delete it. To perform this operation in the VCL, you can call the DeleteFileA() function. Its syntax is: bool __fastcall DeleteFileA(System::UnicodeString FileName); Here is an example: //--------------------------------------------------------------------------- void __fastcall TForm1::btnDeleteFileClick(TObject *Sender) { if( FileExists(L"C:\\Exercise\\soho.txt") ) { if( Application->MessageBox( L"Do you really want to delete that file?", L"File processing", MB_YESNO | MB_ICONQUESTION) == IDYES ) { DeleteFileA(L"C:\\Exercise\\soho.txt"); } } } //--------------------------------------------------------------------------- To perform this same operation using the Win32 library, you can call the DeleteFile() function Its syntax is: BOOL WINAPI DeleteFile(__in LPCTSTR lpFileName); When calling this function, pass the name of, or the path (relative or complete) to, the file. Here is an example: //--------------------------------------------------------------------------- void __fastcall TForm1::btnDeleteFileClick(TObject *Sender) { if( FileExists(L"C:\\Exercise\\sample.txt") ) { if( Application->MessageBox( L"Do you really want to delete that file?", L"File processing", MB_YESNO | MB_ICONQUESTION) == IDYES ) { DeleteFile(L"C:\\Exercise\\sample.txt"); } } else ShowMessage(L"There is no such a file."); } //---------------------------------------------------------------------------
If you have an existing file whose name you don't want anymore, you can rename it. To perform this operation, you can call the Object Pascal's RenameFile() function. Its C++ suntax appears as: bool RenameFile(const String OldName, const String NewName); Pass the first argument as the name of the file you want to rename. Pass the second argument as the name name. Here is an example: //--------------------------------------------------------------------------- void __fastcall TForm1::btnRenameFileClick(TObject *Sender) { RenameFile("C:\\Exercise1\\example.txt", "C:\\Exercise1\\sample.txt"); } //--------------------------------------------------------------------------- In reality, you can use this function as you would the Win32's MoveFile() function. For example, you can call the RenameFile() function to move a file from one directory keep the same name of creating a new name. Here is an example: //--------------------------------------------------------------------------- void __fastcall TForm1::btnRenameFileClick(TObject *Sender) { RenameFile("C:\\Exercise1\\example.txt", "C:\\Exercise2\\sample.txt"); } //--------------------------------------------------------------------------- |
|
|||||
|