使用批处理将文件和文件夹复制到另一个路径

时间:2011-07-21 03:30:08

标签: python windows vbscript batch-file

我有一个目录c:/ go,里面有很多文件夹,子文件夹和文件。

我需要找到内部go,以net * .inf和oem * .inf开头的文件,并将文件夹,子文件夹和所有文件复制到c:/

的另一个地方

它必须是自动使用Windows的东西...像批处理脚本,c ++,python ... vbs pleasee !!提前谢谢

2 个答案:

答案 0 :(得分:4)

从命令行,一种方法是将xcopyfor loop合并:

for /D %i in (net oem) do xcopy /s c:\go\%i*.inf c:\go2\

在批处理文件中,只需将%i替换为%%i

答案 1 :(得分: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)

可能有可能进行改进,但如果你想这样做,这应该可以让你开始。