You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将文件夹树中CSS/JS/PHP等文件内容合并到单个TXT文件?

整合指定类型文件内容到单个TXT文件的方法

以下是不同平台下的实现方案,均能将目标文件夹中所有CSS、JS、PHP、JSON、TXT文件的内容按要求格式整合到一个TXT文件中:

Linux/macOS(Bash 命令)

直接在终端执行以下命令,会在当前目录生成output.txt文件:

find . -type f \( -name "*.css" -o -name "*.js" -o -name "*.php" -o -name "*.json" -o -name "*.txt" \) | while read -r file; do
    echo "---" >> output.txt
    formatted_path=$(echo "$file" | sed 's/\// > /g' | sed 's/^ > //')
    echo "$formatted_path" >> output.txt
    cat "$file" >> output.txt
done
sed -i '1s/^/---\n/' output.txt
  • 命令会自动遍历当前目录及所有子目录
  • 路径格式会将斜杠替换为>,比如./src/utils.js会转为src > utils.js

Windows(PowerShell 脚本)

打开PowerShell,执行以下脚本:

$extensions = @("*.css", "*.js", "*.php", "*.json", "*.txt")
$outputFile = "output.txt"

if (Test-Path $outputFile) { Remove-Item $outputFile }

Get-ChildItem -Recurse -File -Include $extensions | ForEach-Object {
    "---" | Out-File -FilePath $outputFile -Append -Encoding utf8
    $formattedPath = $_.FullName.Replace($pwd.Path + "\", "").Replace("\", " > ")
    $formattedPath | Out-File -FilePath $outputFile -Append -Encoding utf8
    Get-Content $_.FullName | Out-File -FilePath $outputFile -Append -Encoding utf8
}
  • 脚本会生成output.txt文件,包含所有符合条件的文件内容
  • 路径格式会将反斜杠替换为>,比如C:\project\style.css会转为project > style.css

跨平台(Python 脚本)

将以下代码保存为merge_files.py,然后运行python merge_files.py:

import os

TARGET_EXTENSIONS = ('.css', '.js', '.php', '.json', '.txt')
OUTPUT_FILE = 'output.txt'

def main():
    with open(OUTPUT_FILE, 'w', encoding='utf-8') as out_f:
        for root, _, files in os.walk('.'):
            for file in files:
                if file.lower().endswith(TARGET_EXTENSIONS):
                    rel_path = os.path.relpath(os.path.join(root, file), '.')
                    formatted_path = rel_path.replace(os.sep, ' > ')
                    out_f.write('---\n')
                    out_f.write(f'{formatted_path}\n')
                    with open(os.path.join(root, file), 'r', encoding='utf-8', errors='ignore') as in_f:
                        out_f.write(in_f.read())
                        out_f.write('\n')

if __name__ == '__main__':
    main()
  • 该脚本支持Windows、Linux、macOS所有平台
  • 自动处理不同系统的路径分隔符,统一转为>格式
  • 遇到编码问题时会忽略错误,避免脚本中断

内容的提问来源于stack exchange,提问作者cashrunner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 10:28:25