简体   繁体   English

在命令提示符下复制和重命名文件(Windows)

[英]Duplicating and renaming files from the command prompt (Windows)

I have a parent folder called "myfiles" which contains several children folders. 我有一个名为“ myfiles”的父文件夹,其中包含几个子文件夹。 Each of those children has its own file1.txt 每个孩子都有其自己的file1.txt

What I need to do is make a copy of file1.txt and rename each to file2.txt in each respective child folder (I need to do this from a command prompt in windows). 我需要做的是复制file1.txt,并将每个子文件夹中的每个文件重命名为file2.txt(我需要在Windows的命令提示符下执行此操作)。

The goal is to have every child folder contain a file1.txt and a file2.txt 目标是让每个子文件夹都包含一个file1.txt和一个file2.txt

I've nearly got it with this: 我几乎可以做到这一点:

for /r %G in (file1.txt) DO copy %G file2.txt

This command will loop through (when run from the myfiles parent location) and look for any instance of file1.txt and make a copy renamed to file2.txt 该命令将循环遍历(当从myfiles父位置运行时)并查找file1.txt的任何实例,并将副本重命名为file2.txt。

But the problem with this is, that instead of file2.txt being created inside each respective child directory, it is being created inside the parent directory (and consequently asking me to overwrite itself each time). 但这是一个问题,不是在每个子目录中都创建file2.txt,而是在父目录中创建了File2.txt(因此每次都要求我覆盖自己)。

I know I'm close, but any help would be great. 我知道我已经接近了,但是任何帮助都会很棒。

This would be a quick and dirty way: 这将是一种快速而肮脏的方式:

for /r %%a in (file1.txt) do (
    copy "%%a" "%%a.TMP"
    ren "%%a.TMP" "file2.txt"
)

The snippet above will create a copy into the correct directory with .TMP appended and then rename the copy to file2.txt . 上面的代码段将创建一个副本到正确的目录中,并附加.TMP ,然后将该副本重命名为file2.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM