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

Windows Server 2022自动提取子文件夹中CBZ文件的可行方案

在Windows Server 2022批量处理CBZ文件:提取、合并目录并清理

我有一个包含多层子文件夹的目录,每个子文件夹里都有一个CBZ文件。需要完成三个操作:

  • 提取每个CBZ文件的内容
  • 如果提取后生成了子文件夹,把里面的内容移到CBZ所在的原文件夹
  • 删除原CBZ文件和提取出来的临时文件夹

之前用ChatGPT生成的PowerShell脚本运行时直接闪退,脚本内容如下:

$sevenZipPath = "C:\Program Files\7-Zip\7z.exe"  # Change this if 7z.exe is not in your PATH

Get-ChildItem -Recurse -Filter *.cbz | ForEach-Object {
    $cbzFile = $_.FullName
    $baseFolder = $_.DirectoryName
    $tempFolder = Join-Path $baseFolder ([System.IO.Path]::GetFileNameWithoutExtension($_.Name))

    # Extract to a subfolder
    & $sevenZipPath x $cbzFile "-o$tempFolder" -y

    # Move everything up one level
    Get-ChildItem -Path $tempFolder -Recurse | ForEach-Object {
        $targetPath = Join-Path $baseFolder $_.Name

        if (-not (Test-Path $targetPath)) {
            Move-Item $_.FullName -Destination $targetPath
        } else {
            Write-Host "Skipping '$($_.Name)' — already exists in $baseFolder"
        }
    }

    # Clean up
    Remove-Item $cbzFile -Force
    Remove-Item $tempFolder -Recurse -Force
}

闪退原因分析

脚本闪退主要是两个问题:

  1. 没有错误捕获机制,遇到异常直接终止;如果双击运行脚本,窗口会在崩溃后直接关闭,看不到错误信息。
  2. 原移动逻辑有缺陷——递归移动所有文件会把嵌套目录里的文件直接丢到父文件夹,打乱原有结构,也容易引发文件冲突。

修复后的脚本

下面是优化后的脚本,解决了闪退问题,完善了移动逻辑,还增加了错误提示和进度反馈:

# 指定7-Zip路径,根据实际安装位置修改
$sevenZipPath = "C:\Program Files\7-Zip\7z.exe"

# 先检查7-Zip是否存在
if (-not (Test-Path $sevenZipPath)) {
    Write-Error "7-Zip未找到,请检查路径是否正确:$sevenZipPath"
    pause
    exit 1
}

# 递归查找所有CBZ文件
Get-ChildItem -Recurse -Filter *.cbz | ForEach-Object {
    $cbzFile = $_.FullName
    $baseFolder = $_.DirectoryName
    $tempFolder = Join-Path $baseFolder $_.BaseName  # 简化文件名获取方式

    Write-Host "正在处理:$cbzFile"

    try {
        # 提取CBZ到临时文件夹,-y自动覆盖已存在文件
        & $sevenZipPath x $cbzFile "-o$tempFolder" -y | Out-Null

        # 获取临时文件夹内的所有内容
        $tempContents = Get-ChildItem -Path $tempFolder -Force

        if ($tempContents.Count -eq 1 -and $tempContents[0].PSIsContainer) {
            # 如果临时文件夹里只有一个子文件夹,就把这个子文件夹里的内容移到父目录
            $innerFolder = $tempContents[0]
            Get-ChildItem -Path $innerFolder.FullName -Recurse -Force | ForEach-Object {
                $targetPath = Join-Path $baseFolder $_.Name
                if (-not (Test-Path $targetPath)) {
                    Move-Item $_.FullName -Destination $targetPath -Force
                } else {
                    Write-Warning "跳过:$($_.Name) 已存在于 $baseFolder"
                }
            }
        } else {
            # 否则直接把临时文件夹里的所有内容移到父目录
            $tempContents | ForEach-Object {
                $targetPath = Join-Path $baseFolder $_.Name
                if (-not (Test-Path $targetPath)) {
                    Move-Item $_.FullName -Destination $targetPath -Force
                } else {
                    Write-Warning "跳过:$($_.Name) 已存在于 $baseFolder"
                }
            }
        }

        # 清理原CBZ和临时文件夹
        Remove-Item $cbzFile -Force
        Remove-Item $tempFolder -Recurse -Force
        Write-Host "处理完成:$cbzFile`n"
    } catch {
        Write-Error "处理 $cbzFile 时出错:$_"
        # 出错后暂停,方便查看错误
        pause
    }
}

Write-Host "所有CBZ文件处理完毕!"
pause

使用方法

  1. 确保已安装7-Zip,并根据实际安装路径修改脚本里的$sevenZipPath参数。
  2. 不要双击运行脚本,而是打开PowerShell终端操作:
    • 按下Win+X,选择「Windows PowerShell(管理员)」或「终端(管理员)」
    • 使用cd命令切换到脚本所在目录,比如cd C:\Your\Script\Path
    • 运行脚本:.\YourScriptName.ps1
  3. 如果遇到执行权限问题,先执行:Set-ExecutionPolicy RemoteSigned(按提示输入Y确认)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 08:48:16