从一个目录移动文件 [英] Moving A File From One Directory

查看:93
本文介绍了从一个目录移动文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文件从一个目录移动到另一个目录.我有这段代码可以复制整个目录(有效),但是当我尝试移动文件时,它失败了.如果我直接输入文件名,它将起作用.所以我认为它是string上的语法.说了这么多,我仍然有从旧目录中删除文件的问题.在C ++中,是否有一种更简单的方法将文件从一个目录移动到另一个目录?谢谢.

我现在在用什么......

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....

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.

推荐答案

不要复制-它可能花费太多时间,甚至由于磁盘空间不足而失败.而是使用int rename ( const char * oldname, const char * newname )重命名整个内容.参见 http://www.cplusplus.com/reference/clibrary/cstdio/rename/ [ ^ ].

在Windows 7上,您可能需要以管理员身份以提升的特权运行程序;如果您仅以管理员身份登录,这还不够(甚至不需要).使用"c:\ mapper \"之类的目录很糟糕,在Windows 7中不是合法的.您需要使用用户帐户下的特殊"目录.

这是您可以使用的选项的完整说明: http://www.sevenforums.com/tutorials/11841 -run-administrator.html [ ^ ].

—SA
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


您(和SA)正在艰难地做事.使用Windows提供的MoveFile()或MoveFileEx()API.

MoveFile使用最有效的方法,具体取决于文件是在同一驱动器(重命名)还是在不同驱动器(副本)上.
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).


这篇关于从一个目录移动文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆