Click here to Skip to main content
15,903,854 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I'm trying to move a file from one directory to another. I have this code that copies the entire directory (works) but when I try to move a file over, it fails. If I directly put the files name in, it works. So I think that its the syntax on the string. Having said all that, I still have the problem of removing the file from the old directory. Is there an easier way in C++ to just move the file from one directory to another? Thank you.

What I am using now....

C++
SHFILEOPSTRUCT fileop;
fileop.hwnd = NULL;
fileop.wFunc = FO_MOVE;
fileop.pFrom = (string("c:\\mapper\\").append(string(iter->cFileName)).c_str());
fileop.pTo = "c:\\FAIL\\";
fileop.fFlags = FOF_ALLOWUNDO|FOF_NOCONFIRMATION|FOF_NOCONFIRMMKDIR;


Andre, why would you delete this? I didn't even have time to get your answer!
I thought you had a good answer! Please repost it! I got the move part but I didn't get the string part. Thank you.
Posted
Updated 7-Oct-11 15:43pm
v8

Don't copy — it can take too much time and even failed due to insufficient space of the disk. Instead, rename whole thing using int rename ( const char * oldname, const char * newname ). See http://www.cplusplus.com/reference/clibrary/cstdio/rename/[^].

On Windows 7, you may need to run your program with elevated privileges as administrator; if you just log in as administrator, this is not enough (and not even required). Using a directory like "c:\mapper\" is bad, it won't be legal at Windows 7; you need to work with "special" directories under user accounts.

This is a good description of options you can use: http://www.sevenforums.com/tutorials/11841-run-administrator.html[^].

—SA
 
Share this answer
 
v2
Comments
CPallini 7-Oct-11 16:39pm    
5.
Sergey Alexandrovich Kryukov 7-Oct-11 16:45pm    
Thank you.
--SA
Member 7766180 7-Oct-11 16:42pm    
Holy Cow! That is a wonderful answer! My 100. If I could do it, so I guess I'll have settle on a 5. Thank you.
Sergey Alexandrovich Kryukov 7-Oct-11 16:44pm    
Your understanding and good words is more important than a vote of 9000 :-)
--SA
Chuck O'Toole 7-Oct-11 20:43pm    
I don't know why directories in the root (e.g. C:\Mapper\ or C:\Fail\) would be illegal under Windows 7, I use them all the time (running Windows 7 Enterprise).
You (and SA) are doing things the hard way. Use the MoveFile() or MoveFileEx() APIs provided by windows.

MoveFile uses the most efficient method, depending on if the files are on the same drive (rename) or different drives (copy).
 
Share this answer
 
Comments
Member 7766180 7-Oct-11 21:32pm    
Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900