批处理文件有选择地将文件复制到相同的文件夹树
Batch File to Selectively Copy Files To an Identical Folder Tree
我有 2 个相同的文件夹树,我们称它们为 C:\First
和 C:\Second
,有许多子文件夹。 C:\First
在许多子文件夹和子文件夹中有许多 XML 文件。 C:\Second
刚刚创建了文件夹树。
我想遍历 C:\First\*
中的所有 XML 个文件,并将其放在 C:\Second
中的等效位置。
但首先我要检查 C:\Current
文件夹中是否存在同名文件(那里没有子文件夹),在这种情况下,我会将 C:\Current
中的文件复制到正确的位置C:\Second
.
中的子文件夹
换句话说,我想将整个结构和文件从 C:\First
复制到 C:\Second
,但我想使用 C:\Current
中可能存在也可能不存在的最新版本.而且,在 C:\Current
中有很多我不关心的文件。
示例:
C:\Current
有这些文件:
a.xml
b.xml
1.xml
c.xml
d.xml
e.xml
2.xml
f.xml
g.xml
3.xml
在 C:\First
中,我将 a.xml, b.xml, c.xml, d.xml, e.xml, f.xml, g.xml
分散在其子文件夹中。
我希望我不会太混乱...
当问题描述得当,同样的问题描述可以作为编写程序的规范。在你的情况下,你有这样的描述:
I want to go through all XML files in C:\First* and put it in it's
equivalent place in C:\Second.
But first I want to check if a file with the same name exists in the
C:\Current folder
"But first"不是用来写程序的。您只需要首先写下第一件事!例如:
I want to go through all XML files in C:\First*. I want to check if a
file with the same name exists in the C:\Current folder in which case
I will copy the one from C:\Current to the proper sub-sub-sub folder
in C:\Second. Otherwise put the file from C:\First in it's equivalent
place in C:\Second.
下面的伪代码是如何将您的原始问题描述转化为程序的示例:
rem I have 2 identical folder trees, let's call them C:\First and C:\Second, with many subfolders.
rem C:\First has many XML files in many sub and sub-sub folders. C:\Second just has the folder tree created.
rem I want to go through all XML files in C:\First\*
for /R inside C:\First with all *.XML files do (
rem But first I want to check if a file with the same name exists in the C:\Current folder (no subfolders there)
if exist "C:\Current folder\place here just the name of the file from the for command" (
rem in which case I will copy the one from C:\Current to the proper sub-sub-sub folder in C:\Second.
set properFolder=place here the sub-sub-sub folder from the for command
set properFolder=change "C:\First" by "C:\Second" in properFolder
copy "C:\Current folder\just the name" "!properFolder!"
) else (
rem ... and put it in it's equivalent place in C:\Second.
set properFolder=place here the sub-sub-sub folder from the for command
set properFolder=change "C:\First" by "C:\Second" in properFolder
copy "the file from for command" "!properFolder!"
)
)
如果分析这段代码,您会发现获取 properFolder 的行在两部分中是相同的,因此一种更简单的方法是在 if 命令之前仅获取一次 properFolder。
您可以使用此伪代码作为编写批处理文件的起点。
我有 2 个相同的文件夹树,我们称它们为 C:\First
和 C:\Second
,有许多子文件夹。 C:\First
在许多子文件夹和子文件夹中有许多 XML 文件。 C:\Second
刚刚创建了文件夹树。
我想遍历 C:\First\*
中的所有 XML 个文件,并将其放在 C:\Second
中的等效位置。
但首先我要检查 C:\Current
文件夹中是否存在同名文件(那里没有子文件夹),在这种情况下,我会将 C:\Current
中的文件复制到正确的位置C:\Second
.
换句话说,我想将整个结构和文件从 C:\First
复制到 C:\Second
,但我想使用 C:\Current
中可能存在也可能不存在的最新版本.而且,在 C:\Current
中有很多我不关心的文件。
示例:
C:\Current
有这些文件:
a.xml
b.xml
1.xml
c.xml
d.xml
e.xml
2.xml
f.xml
g.xml
3.xml
在 C:\First
中,我将 a.xml, b.xml, c.xml, d.xml, e.xml, f.xml, g.xml
分散在其子文件夹中。
我希望我不会太混乱...
当问题描述得当,同样的问题描述可以作为编写程序的规范。在你的情况下,你有这样的描述:
I want to go through all XML files in C:\First* and put it in it's equivalent place in C:\Second.
But first I want to check if a file with the same name exists in the C:\Current folder
"But first"不是用来写程序的。您只需要首先写下第一件事!例如:
I want to go through all XML files in C:\First*. I want to check if a file with the same name exists in the C:\Current folder in which case I will copy the one from C:\Current to the proper sub-sub-sub folder in C:\Second. Otherwise put the file from C:\First in it's equivalent place in C:\Second.
下面的伪代码是如何将您的原始问题描述转化为程序的示例:
rem I have 2 identical folder trees, let's call them C:\First and C:\Second, with many subfolders.
rem C:\First has many XML files in many sub and sub-sub folders. C:\Second just has the folder tree created.
rem I want to go through all XML files in C:\First\*
for /R inside C:\First with all *.XML files do (
rem But first I want to check if a file with the same name exists in the C:\Current folder (no subfolders there)
if exist "C:\Current folder\place here just the name of the file from the for command" (
rem in which case I will copy the one from C:\Current to the proper sub-sub-sub folder in C:\Second.
set properFolder=place here the sub-sub-sub folder from the for command
set properFolder=change "C:\First" by "C:\Second" in properFolder
copy "C:\Current folder\just the name" "!properFolder!"
) else (
rem ... and put it in it's equivalent place in C:\Second.
set properFolder=place here the sub-sub-sub folder from the for command
set properFolder=change "C:\First" by "C:\Second" in properFolder
copy "the file from for command" "!properFolder!"
)
)
如果分析这段代码,您会发现获取 properFolder 的行在两部分中是相同的,因此一种更简单的方法是在 if 命令之前仅获取一次 properFolder。
您可以使用此伪代码作为编写批处理文件的起点。