使用批处理文件移动不包括 *.log 的文件
我想将文件从一个目录移动到另一个目录(不包括 *.log 文件)。 我尝试过 XCOPY,因为它有一个 EXCLUDE 开关,但它只是复制而不是移动,文件仍然保留为源文件夹。
I want to move files from one directory to another excluding *.log file.
I have tried XCOPY as it has a EXCLUDE switch but it does COPY not MOVE, files still remain as source folder.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您可以使用 ROBOCOPY,请尝试以下操作:
If you can use ROBOCOPY, try this:
首先,请尝试使用此 bat
经过仔细测试后,删除
ECHO
命令。有关详细信息,请阅读
HELP FOR
、HELP IF
,有关用于从文件名中提取扩展名的语法,请阅读HELP CALL
。To get you started, try this bat
After careful testing, remove the
ECHO
command.For more information, read
HELP FOR
,HELP IF
and for the syntax used to extract the extension from the filename, readHELP CALL
.无需RoboCopy,您就可以批量接近。这将采用传递的通配符,对其运行 dir,然后删除以 .log 结尾的目录列表中的条目。然后它对每一个调用 move。由于我们要单独移动每个文件,因此需要确保目标存在并且是一个文件夹 - 否则移动可能会将其解释为移动并重命名。
请注意,如果您专门向 dir 传递单个文件夹名称,则返回的内容可能不是您所期望的。不过,所有通配符都应该可以正常工作。
Without RoboCopy, you can get close in batch. This will take a passed wildcard, run dir on it, then remove entries in the directory list ending in .log. It then calls move on each one. Since we're moving each file individually, we need to make sure the destination exists and is a folder - otherwise move might interpret it as a move and rename, instead.
Note that what dir returns may not be what you expect if you pass it a single folder name specifically. All wildcards should work fine, though.
您可能会这样做:
要测试您是否要移动正确的文件,您只需执行“ls”并将其通过管道传输到“grep”,忽略“mv”
You could probably do something like:
To test if you're going to be moving the right files you can just do an "ls" and pipe that to the "grep" missing out the "mv"
http://www.pcreview.co.uk/forums /use-exclude-option-xcopy-t1469487.html
在前面的超链接中,我可以阅读以下问题及相关答案:
问题:
您好,
我使用下面的 xcopy 命令,但它没有执行我想要的操作。相反,它
抱怨“无法读取文件:.obj”,然后停止。
xcopy c:\t1 c:\t2 /EXCLUDE:.obj
我希望在复制过程中跳过所有 .obj 文件。有人可以帮助我吗?
谢谢。托尼
回答:
/EXCLUDE 指令指定包含字符串的文件列表
要排除的文件名。例如,要做你想做的事,你会
需要创建一个文件,假设我们将其命名为MyExcludes.txt。在这个文件中,
只有一个文件规范 - “.obj”(不带引号)。
然后将命令更改为:
xcopy c:\t1 c:\t2 /EXCLUDE:MyExcludes.txt
===>这是一个部分解决方案,因为您复制除 .obj 文件之外的所有文件,但不会从源中删除它们。您可以尝试将 XCOPY 的输出重定向到文件,然后手动将 DELETE 添加到 XCOPY 命令写入的文件的每一行。最后,执行该文件以删除源文件。
http://www.pcreview.co.uk/forums/use-exclude-option-xcopy-t1469487.html
At the previous hyperlink, I can read the following QUESTION with the related ANSWER:
QUESTION:
Hi,
I use an xcopy command below but it doesn't do what I want. Instead, it
complains "cannot read file: .obj" and then stops.
xcopy c:\t1 c:\t2 /EXCLUDE:.obj
I want all .obj files are skipped during copying. Can someone help me?
Thanks. Tony
ANSWER:
The /EXCLUDE directive specifies a list of files which contain strings of
file names to exclude. For example, to do what you're trying to do, you'll
need to create a file, suppose we call it MyExcludes.txt. In this file,
there is just one filespec - ".obj" (w/o the quotes).
Then change the command to this:
xcopy c:\t1 c:\t2 /EXCLUDE:MyExcludes.txt
===> THIS IS A PARTIAL SOLUTION, because you copy all but .obj files, but you don't delete them from the SOURCE. You can try to redirect the output of XCOPY to file, and then to manually add DELETE to every row of the file written by the XCOPY command. At last, you execute the file in order to delete the source files.