【问题标题】:Loop through each folder and concatenate files (output a combined file in each folder)循环遍历每个文件夹并连接文件(在每个文件夹中输出一个组合文件)
【发布时间】:2014-06-15 14:34:59
【问题描述】:

我希望遍历多个文件夹并连接所有 .html 文件,并通过批处理文件为每个文件夹输出一个 combine.html。

更好的是,我希望输出一个组合的[文件夹名].html。例如,对于 Folder1,该文件夹中的输出级联文件将是 combineFolder1.html。

我一直在使用命令提示符中的复制命令进入每个文件夹以连接文件:

copy *.html combined.html

如何遍历多个文件夹并运行复制命令进行连接并使用批处理文件为每个文件夹输出 combine.html(或 combine[foldername].html)?谢谢。

【问题讨论】:

    标签: windows batch-file command-prompt


    【解决方案1】:

    在主文件夹中启动它。它应该满足您的要求。

    @echo off
    for /d /r %%a in (*) do (
       pushd "%%a"
       echo "%%a"
          copy *.html tmp.tmp >nul
          move /y tmp.tmp "combined%%~nxa.html" >nul
       popd
    )
    pause
    

    【讨论】:

    • 你能解释一下它是做什么的,你是如何在上面运行的? /d/r/y是干什么用的?
    • @vadimbog /d 收集目录名称,/r 以递归方式进行。 /y 确保文件已存在时被覆盖。问题中描述了该功能。
    【解决方案2】:

    下面的一行命令应该做你需要的:

    find . -type d ! -path '.' | xargs -i_folder sh -c "cd _folder; cat *html > combined\$(echo _folder | sed 's#.*/##').html"

    我还要提到,上面假设您的目录名称中不包含空格。

    如果确实如此,那么这里的一些更改将解决问题:

    find . -type d ! -path '.' -print0 | xargs --null -i_folder sh -c "cd \"_folder\"; cat *html > \"combined\$(echo _folder | sed 's#.*/##').html\""

    【讨论】:

    • 这个问题似乎有一个Windows 标签,您的解决方案在通用Windows 中不起作用。
    • :) 我的错,没有注意到。这也解释了您的解决方案(不熟悉 Windows 中的脚本)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 2016-02-12
    • 2018-01-03
    • 2022-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多