Win平台统计一个文件夹中所有指定后续文件的行数,并合并所有文件内容到一个文件

2024-12-30 08:47:46 阅读 0评论 2
以下命令需要Shell执行:
 
统计行数:
 
Get-ChildItem -Recurse -Include *.js,*.wxss,*.json,*.wxml | Get-Content | Measure-Object -Line
 
合并:
 
# 定义输入和输出文件路径
$outputFile = "C:\path\to\output\paged_output.txt"
 
# 获取所有指定类型的文件
$inputFiles = Get-ChildItem -Recurse -Include *.js,*.wxss,*.json,*.wxml
 
# 准备输出文件内容
$outputContent = @()
 
# 读取所有文件内容并合并到一个数组中
foreach ($file in $inputFiles) {
    $content = [System.IO.File]::ReadAllLines($file.FullName, [System.Text.Encoding]::UTF8)
    $outputContent += $content
}
 
# 计算总行数和每页平均行数
$totalLines = $outputContent.Count
if ($totalLines -eq 0) {
    Write-Host "输入文件为空"
    exit
}
 
$totalPages = 60
$linesPerPage = [math]::Floor($totalLines / $totalPages)
$remainingLines = $totalLines % $totalPages
 
# 分页写入内容
$currentLine = 0
for ($page = 0; $page -lt $totalPages; $page++) {
    # 确定当前页的行数
    if ($page -lt $remainingLines) {
        $pageLines = $linesPerPage + 1
    } else {
        $pageLines = $linesPerPage
    }
 
    # 添加当前页的内容
    for ($i = 0; $i -lt $pageLines -and $currentLine -lt $totalLines; $i++, $currentLine++) {
        $outputContent[$currentLine] = $outputContent[$currentLine].Trim()
    }
 
    # 添加分页符
    if ($page -lt $totalPages - 1) {
        $outputContent += ""
        $outputContent += "-" * 80  # 使用80个连字符作为分页符
        $outputContent += ""
    }
}
 
# 写入输出文件,使用 UTF8 编码且不带 BOM
[System.IO.File]::WriteAllLines($outputFile, $outputContent, [System.Text.Encoding]::UTF8)
 
Write-Host "分页文本文件已生成: $outputFile"
文章版权声明:除非注明,否则均为林羽凡原创文章,转载或复制请以超链接形式并注明出处。
林羽凡

林羽凡V

林羽凡博客,是一个记录自己生活、互联网技术的原创独立博客 Linyufan.Com

4290 文章
18091 评论
20961972 浏览

了解一下?

  • 了解一下?

活跃朋友

最新留言

    厉害了。。。。。:heo-抬眼镜:
    老司机(FLYER) • 时间 2026年02月24日 10:26:37
    vue博客的网址是啥
    前端练习生 • 时间 2026年02月22日 19:32:03
    程序整好了分享一下呗*
    麻烦修改一下网站 https://www.penlife.cn/:heo-抬眼镜:
    前端练习生 • 时间 2026年02月22日 19:29:36
    新年快乐,祝马上发财,万事如意,大顺大财大吉祥
    爱娃子 • 时间 2026年02月22日 11:50:39
    大佬厉害啊
    爱娃子 • 时间 2026年02月22日 11:49:50
    折腾起来
    别无所求爱娃子 • 时间 2026年02月22日 11:49:05
    :heo-鼓掌::heo-鼓掌::heo-鼓掌:
    热衷于 • 时间 2026年02月21日 17:55:20
    :heo-耍酷:自己会开发还是比较爽的*
    对了你的SSL过期了
    灰常记忆 • 时间 2026年02月19日 23:50:46
    能不能叫你的AI们给你发点钞票?:heo-耍酷:
    wu先生 • 时间 2026年02月18日 09:38:57
    灵蛇辞岁,金马报春*
    新的一年*
    祝你电量满格,快乐超标*
    所有的美好,都奔你而来
    阿和 • 时间 2026年02月17日 14:12:47
    可以让AI成为你的黑奴为你做事情了哈哈哈
    李的日志 • 时间 2026年02月16日 02:08:11
    用自己开发的工具固然是好 但如果是自用 如果是兴趣之上 那还好 如果是开源使用 一个人开发一个人维护 如果没有相应的回报 可能有点上头哦
    刘郎 • 时间 2026年02月15日 08:10:14

再了解一下?

  • 林羽凡待办
取消
支付宝二维码
支付宝二维码
微信二维码