Copy in FOR loop works but keeps giving the error “The file cannot be copied onto itself”

核能气质少年 提交于 2019-12-13 17:54:15

问题


So when I copy using the dirs %userprofile%\Desktop\Out and %userprofile%\Desktop\Out\test The .txt are copied over however I get the error The file cannot be copied onto itself.

When I run the script with %userprofile%\Desktop\Out and %userprofile%\Desktop\New Folder It runs smoothly.

So my question is why does the script get confused when you copy from a common root folder to a different sub folder?

@ECHO ON
FOR /r "%userprofile%\Desktop\Out" %%a IN (*.txt) DO copy "%%a" "%userprofile%\Desktop\New Folder"
pause

回答1:


The destination directory %userprofile%\Desktop\Out\test is a subdirectory of the source, whereas %userprofile%\Desktop\New Folder is not.

Since you are using for /r, the copied files would be copied from the lower directory as the recursion takes place - FROM the destination directory INTO the destination directory. Hence, a file would be copied over itself.



来源:https://stackoverflow.com/questions/39571781/copy-in-for-loop-works-but-keeps-giving-the-error-the-file-cannot-be-copied-ont

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!