简体   繁体   English

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

[英]copy files and folders to another path using batch

I have a directory c:/go, inside go there is tons of folders, subfolders and files.我有一个目录 c:/go,在 go 里面有大量的文件夹、子文件夹和文件。

I need to find inside go, files that start with net*.inf and oem*.inf, and copy the folder, subfolders and all files where they are to another palce at c:/我需要在 go 中找到以 net*.inf 和 oem*.inf 开头的文件,并将它们所在的文件夹、子文件夹和所有文件复制到 c 的另一个位置:/

It must be something automatic using windows... like batch script, c++, python...vbs pleasee!!它必须是自动使用 windows... 像批处理脚本,c++,python...vbs 请! thanks in advance提前致谢

From the command line, one way is to combine xcopy with a for loop :从命令行,一种方法是将xcopyfor 循环结合使用:

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

In a batch file just replace %i with %%i .在批处理文件中,只需将%i替换为%%i

The xcopy technique in @ars' answer is obviously simpler for your situation if it is appropriate for you. @ars 答案中的 xcopy 技术显然更适合您的情况,如果它适合您的话。 However, below is a Python implementation.但是,下面是 Python 实现。 It will make sure the target directory is there and create it if it isn't:它将确保目标目录存在,如果不存在则创建它:

#!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)

Improvements are probably possible, but this should get you started if you want to go this way.改进可能是可能的,但如果您想以这种方式 go,这应该可以帮助您入门。

暂无
暂无

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

相关问题 如何使用python检查路径是否存在并复制文件夹和文件 - how to check if path exist and copy folders and files using python 将文件和文件夹复制到另一个位置 - Clearcase copy files and folders to another location 使用Python将文件从目录复制到文件夹 - Copy Files from Directory to Folders using Python 如何使用 python boto3 将文件和文件夹从一个 S3 存储桶复制到另一个 S3 - how to copy files and folders from one S3 bucket to another S3 using python boto3 Python - 将文件夹从一个目录复制到另一个目录(仅复制我包含在 txt 文件中的文件夹) - Python - Copy folders from one directory to another directory (Copy only that folders that i have included in a txt files) 如何使用 Python 将子文件夹和文件复制到新文件夹中 - How to Copy Sub Folders and files into New folder using Python Python-在不使用disutils的情况下将文件夹复制到另一个位置 - Python- Copy folders to another location without using disutils 如何使用 Python 复制多个文件夹中的多个文件 - How to copy multiple files in multiple folders using Python 使用python脚本将文件复制到网络路径 - Copy files to a network path using python script Python代码返回“ ======重新启动: <path> ======””,以前可以正常使用时,使用查找和替换批处理重命名文件夹和文件 - Python code returns “====== RESTART: <path> ======” when previously working properly, batch renaming folders and files with find and replace
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM