• 13
• 16
• 27
• 9
• 9

# Moving file doesn't work

This topic is 1743 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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);
}


##### Share on other sites

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

##### Share on other sites

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 ;)

##### Share on other sites

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.

Edited by Endurion

##### Share on other sites

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

##### Share on other sites

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);

Edited by Infinity95

##### Share on other sites

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*

##### Share on other sites

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.

##### Share on other sites

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