1

我正在尝试连接来自多个目录的文件。从一个目录,我知道你可以执行

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 编写一些东西吗?任何帮助或建议将不胜感激。

4

2 回答 2

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开关是否完全正确。它具有不同的含义,具体取决于它出现的位置:在任何文件之前、在源之后或在目标之后。

于 2013-02-11T04:48:18.260 回答
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
于 2013-02-11T02:08:43.727 回答