使用批处理将文件和文件夹复制到另一路径
我有一个目录 c:/go,里面有很多文件夹、子文件夹和文件。
我需要在 go 目录里找到那些以 net*.inf 和 oem*.inf 开头的文件,然后把它们所在的文件夹、子文件夹和所有文件一起复制到 c:/ 的另一个地方。
这必须是自动化的,可以使用 Windows 的一些工具,比如批处理脚本、C++、Python 或者 VBS,求助啊!!提前谢谢!
2 个回答
2
在@ars的回答中提到的xcopy方法,对于你的情况来说显然更简单,如果适合你的话。不过,下面是一个用Python写的实现。这个代码会确保目标文件夹存在,如果没有的话会帮你创建一个:
#!python
import os
import re
import shutil
def parse_dir(src_top, dest_top):
re1 = re.compile("net.*\.inf")
re2 = re.compile("oem.*\.inf")
for dir_path, dir_names, file_names in os.walk(src_top):
for file_name in file_names:
if re.match(re1, file_name) or re.match(re2, file_name):
target_dir = dir_path.replace(src_top, dest_top, 1)
if not os.path.exists(target_dir):
os.mkdir(target_dir)
src_file = os.path.join(dir_path, file_name)
dest_file = os.path.join(target_dir, file_name)
shutil.copyfile(src_file, dest_file)
src_top = "\\go"
dest_top = "\\dest"
parse_dir(src_top, dest_top)
可能还有改进的空间,但如果你想走这条路,这段代码应该能帮你入门。