Moving file doesn't work

Started by
15 comments, last by Servant of the Lord 10 years, 10 months ago

So i'm having trouble finding the Problem. It never moves the file to it's new destination. Maybe im just too tired but please give me some ideas why it's not working.

Here's the relevant code:


void Logger::RotateLogFiles() {
    ostringstream newpath;
    string oldpath;
    this->logtime = time(0);
    this->now = localtime(&this->logtime);

    newpath << this->archive << "/" << this->now->tm_mday << "-" << this->now->tm_mon + 1 << "-" << this->now->tm_year + 1900 << "/" << this->filepath.substr(this->filepath.find(".")+1, string::npos);
    if(rename(this->filepath.c_str(), newpath.str().c_str()) != 0) MessageBox(NULL, L"Error moving Logfile.", L"Error", MB_OK | MB_ICONERROR);
}
 

"Errare humanum est, sed in errare perseverare diabolicum."

Advertisement

It looks like your "newpath" contains some directories. Do these directories exist? Does the code work if you manually create these directories?

Ill try that tomorrow. Going to bed now cause it's kinda late here in germany. Thanks in advance for helping and maybe i'll even solve it myself tomorrow after a good night's rest ;)

"Errare humanum est, sed in errare perseverare diabolicum."

If the paths are different this may not work depending on the OS you're running.

AFAIK Windows only allows rename if the drive is the same.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

This strange behavior can be caused by excessive count of '/' symbols in `newpath` value.

OK i just came home from school and im gonna try and figure out whats wrong after lunch.

@Endurion: I'm writing to the same drive so i don't think there is a problem with that.

@GuardianX: There's only two '/' in 'newpath' so that shouldn't be a problem either.

EDIT:

Here's some debug values:

newpath is: "/Logs/5-6-2013/14_16_44-Default.log"

filepath is: "5-6-2013.Default.log"

I dont see what i did wrong. The folders in newpath all exist.

EDIT2: Ok, seems like i cant use rename to move a file into a folder... is there any other function to do that, thats easy to use?

EDIT3: I now tried using MoveFile from the windows library and it still doesn't seem to work. Do i have to use an absolute path maybe instead of a relative one?


if(!MoveFile((TCHAR*)this->filepath.c_str(), (TCHAR*)newpath.str().c_str())) MessageBox(NULL, L"Error moving Logfile.", L"Error", MB_OK | MB_ICONERROR);

"Errare humanum est, sed in errare perseverare diabolicum."

Any help on this? I still haven't found a solution to this and im kinda stuck on what i should do next to find a solution.

*Bump*

"Errare humanum est, sed in errare perseverare diabolicum."

Call GetLastError after it fails and look up the error message (you can use FormatMessage to get the error as a string).

http://msdn.microsoft.com/en-us/library/windows/desktop/ms679360%28v=vs.85%29.aspx

Then you should be able to find out why it failed.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Is the file still open when you attempt to move it? If so, the move will fail.

OK i got the error message now. It says it can't find the file. The file is definitely closed before moving it and it definitely exists. I checked the variables value in the debugger.

"Errare humanum est, sed in errare perseverare diabolicum."

This topic is closed to new replies.

Advertisement