从批处理文件中连接多个目录中的文件

时间:2013-02-11 01:09:50

标签: batch-file cmd xcopy

我正在尝试连接多个目录中的文件。从单个目录中,我知道您可以执行

copy /B *.blah all.blah

将扩展名为.blah的文件连接到名为all.blah的单个文件。我的结构是什么样的:

level 1/
    level 2_1/
        file_1.blah
        file_2.blah
    level 2_2/
        ...
    level 2_3/
        ...
    do_not_include_this_directory/
        ...

我要做的是在顶级目录中创建一个all.blah文件,该文件是.blah子目录中所有level*个文件的串联,没有包括do_not_include_this_directory目录中的任何文件。

我的目标是在批处理文件中执行此操作(此批处理文件中包含不同目录的其他文件串联逻辑),但我花了一个小时过去使用cmd for逻辑到没有有效(我的一些目录名称中有空格)。也许这是我应该使用python脚本做的事情?我认为这可以通过for使用copy循环来相对容易地完成,但我对这些东西的技能至少缺乏说明(大约2小时前才遇到cmd)

有没有人知道如何做到这一点,或者你是否只是建议我使用Python加入并写一些东西?任何帮助或建议将不胜感激。

2 个答案:

答案 0 :(得分:2)

可以在没有批处理脚本的情况下从命令行轻松完成:)

copy nul all.blah >nul&for /d %F in (level*) do @copy /b all.blah + "%F\*.blah" >nul

作为批处理脚本

@echo off
copy nul all.blah >nul
for /d %F in (level*) do copy /b all.blah + "%F\*.blah" >nul

我不确定/B开关是否完全正确。它具有不同的含义,具体取决于它出现的位置:在任何文件之前,在源之后或在目的地之后。

答案 1 :(得分:0)

对于Windows批处理文件,这似乎是非常可怕的。在Windows 7下测试; YMMV等

@rem get all pathnames, even in excluded directories
@rem EDIT THIS COMMAND to change wildcard to match
dir /b/s *.c >files.tmp
@rem get rid of things with prefix we want to exclude
@rem EDIT THIS COMMAND to change prefix
findstr /V "C:\temp\fee" files.tmp >files2.tmp
del copy.tmp
@rem append things one at a time the hard way!
For /f tokens^=*^ delims^=^ eol^=  %%a in (files2.tmp) do (
copy "%%a" + copy.tmp copy2.tmp
del copy.tmp
rename copy2.tmp copy.tmp
echo.%%a)
@rem clean up
del copy2.tmp
del files.tmp
del files2.tmp
相关问题