开发者

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.

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 p开发者_StackOverflow社区alce at c:/

It must be something automatic using windows... like batch script, c++, python...vbs pleasee!! thanks in advance


From the command line, one way is to combine xcopy with a for loop:

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

In a batch file just replace %i with %%i.


The xcopy technique in @ars' answer is obviously simpler for your situation if it is appropriate for you. However, below is a Python implementation. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜