在FOR循环中复制可正常工作,但始终显示错误“无法将文件复制到自身上". [英] Copy in FOR loop works but keeps giving the error "The file cannot be copied onto itself"

查看:140
本文介绍了在FOR循环中复制可正常工作,但始终显示错误“无法将文件复制到自身上".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,当我使用目录%userprofile%\Desktop\Out%userprofile%\Desktop\Out\test复制时,.txt被复制了,但是出现错误The file cannot be copied onto itself.

当我使用%userprofile%\Desktop\Out%userprofile%\Desktop\New Folder运行脚本时,它运行平稳.

所以我的问题是,当您从公用根文件夹复制到另一个子文件夹时,为什么脚本会感到困惑?

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

解决方案

目标目录%userprofile%\Desktop\Out\test是源的子目录,而%userprofile%\Desktop\New Folder不是.

由于使用的是for /r,因此在递归发生时,复制的文件将从较低的目录复制-从目标目录到目标目录.因此,文件将被复制到其自身上.

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

解决方案

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.

这篇关于在FOR循环中复制可正常工作,但始终显示错误“无法将文件复制到自身上".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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