cbPlus 0 Posted June 17 I have written some code from a tutorial. Now I am stuck with the RenameFile() function from System.SysUtils.hpp The returnValue_ is always false or 0. I tried it as Administrator. I checked the file permissions security. Nothing worked. //--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "uTSearchRec.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm2* Form2; TSearchRec SearchRec; //--------------------------------------------------------------------------- __fastcall TForm2::TForm2(TComponent* Owner) : TForm(Owner) {} //--------------------------------------------------------------------------- void __fastcall TForm2::btnSearchClick(TObject* Sender) { String FindFirstPath_ = "C:\\Users\\NO_NO\\OneDrive\\TEMP\\*.*"; String Caption1_ = "Current Path:\n" + FindFirstPath_; String Caption2_ = SearchRec.Name + " is " + IntToStr(SearchRec.Size) + "bytes in size"; FindFirst(FindFirstPath_, faAnyFile, SearchRec); lbOutput->Caption = Caption1_ + "\n" + Caption2_; } //--------------------------------------------------------------------------- void __fastcall TForm2::btnAgainClick(TObject* Sender) { if (FindNext(SearchRec) == 0) { lbOutput->Caption = SearchRec.Name + " is " + IntToStr(SearchRec.Size) + " bytes in size"; } else FindClose(SearchRec); } //--------------------------------------------------------------------------- void __fastcall TForm2::btnRenameClick(TObject* Sender) { String const OldFileName_ = SearchRec.Name; String const NewFileName_ = OldFileName_ + "bak"; String const MessageFail_ = "Humpty Bumpty ! Njet Internjet !"; String const MessageOK_ = "The bether its not get !"; bool returnValue_ = true; try { returnValue_ = RenameFile(OldFileName_, NewFileName_); lbControlText->Caption = IntToStr(returnValue_); } catch (...){ lbOutput->Caption = "NewFileName_ : " + NewFileName_ + "\nOldFileName_ :" + OldFileName_ + "\nreturnValue : " + IntToStr(returnValue_); } lbOutput->Caption = MessageOK_; /* if ( (RenameFile(OldFileName_, NewFileName_) == false )) { lOutput->Caption = MessageFail_; } else lOutput->Caption = MessageOK_; */ } //--------------------------------------------------------------------------- Header //--------------------------------------------------------------------------- #ifndef uTSearchRecH #define uTSearchRecH //--------------------------------------------------------------------------- #include <System.Classes.hpp> #include <Vcl.Controls.hpp> #include <Vcl.StdCtrls.hpp> #include <Vcl.Forms.hpp> #include <System.SysUtils.hpp> //--------------------------------------------------------------------------- class TForm2 : public TForm { __published: // IDE-managed Components TLabel *lbOutput; TButton* btnSearch; TButton* btnAgain; TButton* btnRename; TLabel *lbControlText; void __fastcall btnSearchClick(TObject* Sender); void __fastcall btnAgainClick(TObject* Sender); void __fastcall btnRenameClick(TObject* Sender); private: // User declarations public: // User declarations __fastcall TForm2(TComponent* Owner); }; //--------------------------------------------------------------------------- extern PACKAGE TForm2* Form2; //--------------------------------------------------------------------------- #endif Share this post Link to post
Brian Evans 100 Posted June 17 Normal practice to use GetLastError to see what the problem is when some Windows APIs fail. GetLastError function (errhandlingapi.h) - Win32 apps | Microsoft Learn Share this post Link to post
Remy Lebeau 1330 Posted June 17 1 hour ago, cbPlus said: I have written some code from a tutorial. Now I am stuck with the RenameFile() function from System.SysUtils.hpp The returnValue_ is always false or 0. I tried it as Administrator. I checked the file permissions security. Nothing worked. RenameFile() does not raise an exception on failure, so your try..catch is useless. Are you sure FindFirst/FindNext() is even successful to begin with? You are not doing any error checking on FindFirst(). Did you debug to make sure OldFileName and NewFileName are what you are expecting? Also, you are using a file path into OneDrive, does OneDrive allow renaming files in this manner to begin with? Do you have the same problem with local files? 31 minutes ago, Brian Evans said: Normal practice to use GetLastError to see what the problem is when some Windows APIs fail. GetLastError function (errhandlingapi.h) - Win32 apps | Microsoft Learn Note that FindFirst() and FindNext() return the actual error code. Share this post Link to post
Brian Evans 100 Posted June 17 (edited) I read the question too fast, the code that should have GetLastError added to see any errors specific to the rename (like file open) is commented out. Delphi equivalent pattern is: if RenameFile(oldName, newName) then ShowMessage('File renamed OK') else ShowMessage('File rename failed with error : '+ IntToStr(GetLastError)); Edited June 17 by Brian Evans Share this post Link to post
Remy Lebeau 1330 Posted June 17 15 minutes ago, Brian Evans said: Delphi equivalent pattern is: if RenameFile(oldName, newName) then ShowMessage('File renamed OK') else ShowMessage('File rename failed with error : '+ IntToStr(GetLastError)); In C++: if (RenameFile(OldFileName_, NewFileName_)) lbControlText->Caption = _D("OK"); else lbOutput->Caption = _D("NewFileName_ : ") + NewFileName_ + _D("\nOldFileName_ : ") + OldFileName_ + _D("\nError : ") + IntToStr(GetLastError()); Alternatively use RaiseLastOSError() to throw an exception holding the error code: try { if (!RenameFile(OldFileName_, NewFileName_)) RaiseLastOSError(); lbControlText->Caption = _D("OK"); } catch (const EOSError& ex) { lbOutput->Caption = _D("NewFileName_ : ") + NewFileName_ + _D("\nOldFileName_ : ") + OldFileName_ + _D("\nError : ") + IntToStr(ex.ErrorCode); } Share this post Link to post
silvercoder79 23 Posted June 18 SearchRec.Name would only be the filename part and does not include the path. Unless something has changed. If so, make sure that you add path before the file name. 1 Share this post Link to post
cbPlus 0 Posted June 18 (edited) Thanks for all the answers. Very, very helpful. I like this forum. So. I took your advice and tampered my code. It works now. @silvercoder79 This was the final solution. I didn't recognize that the absolute path is needed. Thanks. I know the code's syntax is 'simple' coded. But I need this for now to better understand what's going on. It is actually only a learning code. Any further ideas are welcome. Anyway. Thank you. #include <vcl.h> #pragma hdrstop #include "uTSearchRec.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm2* Form2; TSearchRec SearchRec; String WorkingPath_ = "C:\\Users\\NONO\\OneDrive\\TEMP\\"; //--------------------------------------------------------------------------- __fastcall TForm2::TForm2(TComponent* Owner) : TForm(Owner) {} //--------------------------------------------------------------------------- void __fastcall TForm2::btnSearchClick(TObject* Sender) { String FindFirstPath_ = WorkingPath_ + "*.*"; String Caption1_ = "Current Path:\n" + FindFirstPath_; String Caption2_ = SearchRec.Name + " is " + IntToStr(SearchRec.Size) + "bytes in size"; FindFirst(FindFirstPath_, faAnyFile, SearchRec); lbOutput->Caption = Caption1_ + "\n" + Caption2_; } //--------------------------------------------------------------------------- void __fastcall TForm2::btnAgainClick(TObject* Sender) { if (FindNext(SearchRec) == 0) { lbOutput->Caption = SearchRec.Name + " is " + IntToStr(SearchRec.Size) + " bytes in size"; } else FindClose(SearchRec); } //--------------------------------------------------------------------------- void __fastcall TForm2::btnRenameClick(TObject* Sender) { String const OldFileName_ = WorkingPath_ + SearchRec.Name; String const NewFileName_ = OldFileName_ + ".bak"; String const MessageFail_ = "Humpty Bumpty ! Njet Internjet !"; String const MessageOK_ = "The bether its not get !"; bool returnValue_ = true; try { returnValue_ = RenameFile(OldFileName_, NewFileName_); if (returnValue_ == false) RaiseLastOSError(); lbControlText->Caption = IntToStr(returnValue_); } catch (const EOSError& ex){ lbOutput->Caption = "NewFileName_ : " + NewFileName_ + "\nOldFileName_ :" + OldFileName_ + "\nErrorCode : " + String(ex.ErrorCode); } lbOutput->Caption = MessageOK_; } //--------------------------------------------------------------------------- Edited June 18 by cbPlus The code view is somewhat wrong with the TABs Share this post Link to post
David Heffernan 2330 Posted June 18 What I don't understand is why you didn't use the debugger or other debugging techniques to check what values were being passed in to the function. That's my idea for you. Share this post Link to post
cbPlus 0 Posted June 18 (edited) I used and use the debugger. But as I said. I didn't recognize that I have to use the absolute path prior the filenames NewFileName_ and OldFileName_. So I did debug and thought it's ok. I added improtant variables to the watch list. Simply said, it was a logical thinking error. Recently I bought this book from Mohmad Yakub "C Programming Patterns - The Art of Coding Logic". 🙂 Edited June 18 by cbPlus Book add Share this post Link to post
David Heffernan 2330 Posted June 18 What directory would you have expected the files to be relative to? Share this post Link to post
cbPlus 0 Posted June 19 "C:\\Users\\NO_NO\\OneDrive\\TEMP\\*.*" Without the *.* because I thought this is temprorary only for the btnSearch within FindFirst() FindNext() Bad assumption I think. Share this post Link to post