PowerShell Plex文件检查脚本按钮路径切换问题修复求助
Plex媒体文件检查脚本修复方案
核心问题分析
你的脚本存在多个冲突和变量错误,导致路径切换失效:
- 重复定义按钮点击事件:后面的
Start-Process事件覆盖了之前的路径切换逻辑,点击按钮只会打开文件夹而非切换扫描路径 - 标签变量名不一致:初始创建的路径标签是
$label,后续更新时误用了未定义的$pathLabel,导致路径切换后界面无反馈 - Run-Scan函数未使用全局路径:函数内直接引用本地
$fixedPath,而非全局的$global:fixedPath,切换路径后扫描仍沿用初始路径 - 代码冗余重复:重复定义
$pathMap、按钮点击事件和Scan-Folder函数,引发逻辑冲突
修复步骤
1. 清理冗余代码
删除重复的$pathMap定义、按钮点击事件和Scan-Folder函数,保留一套完整逻辑。
2. 修正标签变量名
将所有路径更新相关的标签操作统一使用初始创建的$label变量,确保界面能正确显示当前扫描路径。
3. 替换按钮的错误事件
删除按钮的Start-Process事件,保留路径切换+触发扫描的逻辑,确保点击按钮后切换全局路径并更新界面。
4. 修正Run-Scan函数的路径引用
将函数内的$fixedPath替换为$global:fixedPath,确保扫描使用最新切换的路径。
5. 统一全局变量使用
确保所有涉及路径的操作都引用全局的$global:fixedPath,避免本地变量覆盖。
修复后的完整脚本
Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing # 创建窗体 $form = New-Object System.Windows.Forms.Form $form.Text = "Marion's Plex checker" $form.Size = New-Object System.Drawing.Size(700, 700) $form.StartPosition = "CenterScreen" # 全局路径变量 $global:fixedPath = "D:\ServerFolders\Videos\02 - Movies" # 路径标签 $label = New-Object System.Windows.Forms.Label $label.Text = "Checking subfolders in: $global:fixedPath" $label.Location = New-Object System.Drawing.Point(120, 20) $label.Size = New-Object System.Drawing.Size(550, 20) $form.Controls.Add($label) # 汇总标签 $summaryLabel = New-Object System.Windows.Forms.Label $summaryLabel.Text = "" $summaryLabel.Location = New-Object System.Drawing.Point(120, 45) $summaryLabel.Size = New-Object System.Drawing.Size(550, 20) $form.Controls.Add($summaryLabel) # 输出日志框 $logBox = New-Object System.Windows.Forms.TextBox $logBox.Multiline = $true $logBox.ScrollBars = "Vertical" $logBox.Location = New-Object System.Drawing.Point(120, 75) $logBox.Size = New-Object System.Drawing.Size(550, 350) $logBox.Anchor = "Top,Bottom,Left,Right" $logBox.ReadOnly = $true $form.Controls.Add($logBox) # 文件类型选择按钮(左侧) $fileTypes = @("theme.mp3", "folder.jpg", "background.jpg", "poster.jpg") $fileButtons = @{} $yPos = 20 $global:selectedFile = "theme.mp3" foreach ($file in $fileTypes) { $btn = New-Object System.Windows.Forms.Button $btn.Text = $file $btn.Tag = $file $btn.Location = New-Object System.Drawing.Point(10, $yPos) $btn.Size = New-Object System.Drawing.Size(100, 30) $form.Controls.Add($btn) $fileButtons[$file] = $btn $yPos += 40 $btn.Add_Click({ $global:selectedFile = $this.Tag # 重置所有按钮样式 foreach ($b in $fileButtons.Values) { $b.UseVisualStyleBackColor = $true $b.BackColor = [System.Drawing.SystemColors]::Control } # 高亮选中按钮 $this.UseVisualStyleBackColor = $false $this.BackColor = 'LightBlue' $summaryLabel.Text = "Selected file: $global:selectedFile" }) } # 扫描按钮 $scanButton = New-Object System.Windows.Forms.Button $scanButton.Text = "Scan" $scanButton.Location = New-Object System.Drawing.Point(10, 440) $scanButton.Size = New-Object System.Drawing.Size(100, 30) $form.Controls.Add($scanButton) # 创建路径切换按钮 $moviesButton = New-Object System.Windows.Forms.Button $moviesButton.Text = "Movies" $moviesButton.Size = New-Object System.Drawing.Size(100, 30) $moviesButton.Location = New-Object System.Drawing.Point(120, 440) $form.Controls.Add($moviesButton) $tvButton = New-Object System.Windows.Forms.Button $tvButton.Text = "TV" $tvButton.Size = New-Object System.Drawing.Size(100, 30) $tvButton.Location = New-Object System.Drawing.Point(230, 440) $form.Controls.Add($tvButton) $tvEndedButton = New-Object System.Windows.Forms.Button $tvEndedButton.Text = "TV Ended" $tvEndedButton.Size = New-Object System.Drawing.Size(100, 30) $tvEndedButton.Location = New-Object System.Drawing.Point(340, 440) $form.Controls.Add($tvEndedButton) # 路径映射表 $pathMap = @{ "Movies" = "D:\ServerFolders\Videos\02 - Movies" "TV Current" = "D:\ServerFolders\Videos\01 - TV" "TV Ended" = "D:\ServerFolders\Videos\TV (Ended)" } # 路径切换按钮点击事件 $moviesButton.Add_Click({ $global:fixedPath = $pathMap["Movies"] $label.Text = "Checking subfolders in: $global:fixedPath" # 更新按钮样式 $moviesButton.BackColor = 'LightGreen' $tvButton.BackColor = [System.Drawing.SystemColors]::Control $tvEndedButton.BackColor = [System.Drawing.SystemColors]::Control # 触发扫描 Run-Scan }) $tvButton.Add_Click({ $global:fixedPath = $pathMap["TV Current"] $label.Text = "Checking subfolders in: $global:fixedPath" # 更新按钮样式 $tvButton.BackColor = 'LightGreen' $moviesButton.BackColor = [System.Drawing.SystemColors]::Control $tvEndedButton.BackColor = [System.Drawing.SystemColors]::Control # 触发扫描 Run-Scan }) $tvEndedButton.Add_Click({ $global:fixedPath = $pathMap["TV Ended"] $label.Text = "Checking subfolders in: $global:fixedPath" # 更新按钮样式 $tvEndedButton.BackColor = 'LightGreen' $moviesButton.BackColor = [System.Drawing.SystemColors]::Control $tvButton.BackColor = [System.Drawing.SystemColors]::Control # 触发扫描 Run-Scan }) # 导出CSV按钮 $exportButton = New-Object System.Windows.Forms.Button $exportButton.Text = "Export to CSV" $exportButton.Location = New-Object System.Drawing.Point(10, 400) $exportButton.Size = New-Object System.Drawing.Size(100, 30) $exportButton.Enabled = $false $form.Controls.Add($exportButton) # 全局变量:缺失文件的文件夹列表 $global:missingItems = @() # 扫描执行函数 function Run-Scan { $logBox.Clear() $summaryLabel.Text = "" $log = @() $logPath = Join-Path $global:fixedPath "missing_${global:selectedFile}_log.txt" $csvPath = Join-Path $global:fixedPath "missing_${global:selectedFile}_report.csv" $global:missingItems = @() if (-not (Test-Path $global:fixedPath)) { [System.Windows.Forms.MessageBox]::Show("无效的文件夹路径。", "错误", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error) return } $folders = Get-ChildItem -Path $global:fixedPath -Directory foreach ($folder in $folders) { $targetFile = Join-Path $folder.FullName $global:selectedFile if (-not (Test-Path -LiteralPath $targetFile)) { $global:missingItems += $folder $logLine = "$($folder.Name)" $logBox.AppendText($logLine + "`r`n") $log += $logLine } } if ($global:missingItems.Count -eq 0) { $summaryLabel.Text = "所有文件夹都包含$($global:selectedFile)。" } else { $summaryLabel.Text = "[$($global:missingItems.Count)]个文件夹缺失$($global:selectedFile)。" } $log | Out-File -FilePath $logPath -Encoding UTF8 $logBox.AppendText("`r`n日志已保存至: $logPath`r`n") $exportButton.Enabled = $global:missingItems.Count -gt 0 } # 绑定扫描按钮事件 $scanButton.Add_Click({ Run-Scan }) # 绑定导出CSV按钮事件 $exportButton.Add_Click({ $csvPath = Join-Path $global:fixedPath "missing_$($global:selectedFile)_report.csv" $headerLines = @( "文件类型: $($global:selectedFile)", "缺失总数: $($global:missingItems.Count)`r`n" ) $headerLines | Out-File -FilePath $csvPath -Encoding UTF8 $global:missingItems | ForEach-Object { [PSCustomObject]@{ 文件夹名称 = $_.Name; 路径 = $_.FullName } } | Export-Csv -Path $csvPath -Append -NoTypeInformation -Encoding UTF8 $logBox.AppendText("CSV已导出至: $csvPath`r`n") }) $form.Topmost = $true $form.Add_Shown({ $form.Activate() }) [void]$form.ShowDialog()
排查思路
- 检查事件绑定冲突:查看按钮是否被多次绑定点击事件,后绑定的事件会覆盖前一个,导致预期逻辑失效
- 验证变量作用域:确认全局变量是否被正确引用,避免本地变量覆盖全局值
- 界面元素匹配:检查界面控件的变量名是否一致,确保更新操作能正确作用到目标控件
- 调试输出验证:在关键逻辑处添加
Write-Host输出,确认路径切换后全局变量是否正确更新 - 代码冗余清理:删除重复定义的变量、函数和事件,避免逻辑冲突
内容的提问来源于stack exchange,提问作者marion
相关产品推荐
相关产品推荐

