AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-30315641

marion's questions

Martin Hope
marion
Asked: 2025-04-19 20:32:55 +0800 CST

如何修复 Powershell 按钮选择问题?

  • 7

我使用 ChatGPT 生成了代码,以便了解如何自行操作,而且基本上可以正常工作。该脚本用于检查我的电视和电影文件夹中是否存在缺失的文件(folder.jpg、theme.mp3 等),以便 Plex 更好地工作,并显示哪些文件缺失以及缺失的数量。

它在电影文件夹上运行得很好,但我(AI 也不能)想出如何修复它来单击电视或电视结束按钮并让它搜索这些文件夹。

我最好的猜测是第 167 行的 $pathMap 变量是关键,并且它没有正确更新此部分 - $global:fixedPath = $pathMap

有人可以纠正它并向我解释如何解决这个问题吗?

Windows 10 22H2 和 Powershell PSVersion 5.1.19041.5737

    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing
    
    # Create form
    $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"
    
    # Fixed folder path
    $fixedPath = "D:\ServerFolders\Videos\02 - Movies"
    
    # Path label
    $label = New-Object System.Windows.Forms.Label
    $label.Text = "Checking subfolders in: $fixedPath"
    $label.Location = New-Object System.Drawing.Point(120, 20)
    $label.Size = New-Object System.Drawing.Size(550, 20)
    $form.Controls.Add($label)
    
    # Summary 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)
    
    # Output box
    $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)
    
    # Search file buttons (on the left)
    $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
    
            # Reset styles
            foreach ($b in $fileButtons.Values) {
                $b.UseVisualStyleBackColor = $true
            }
    
            # Highlight selected
            # Reset styles
            foreach ($b in $fileButtons.Values) {
                $b.UseVisualStyleBackColor = $true
                $b.BackColor = [System.Drawing.SystemColors]::Control
            }
    
            # Highlight selected
            $this.UseVisualStyleBackColor = $false
            $this.BackColor = 'LightBlue'
    
            $summaryLabel.Text = "Selected file: $global:selectedFile"
        })
    }
    
    # Scan button
    $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)
    
    # Create a new button for Movies, positioned to the right of the Scan button
    $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)
    
    # Create a new button for TV, positioned next to the Movies button
    $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)
    
    # Create a new button for TV Ended, positioned next to the TV button
    $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)  # Right of the TV button
    $tvEndedButton.Add_Click({
        Start-Process "D:\ServerFolders\Videos\TV (Ended)"  # Link to the TV Ended path
    })
    $form.Controls.Add($tvEndedButton)
    
    # Path variables for Movies, TV, and TV Ended
    $pathMap = @{
        "Movies"     = "D:\ServerFolders\Videos\02 - Movies"
        "TV Current" = "D:\ServerFolders\Videos\01 - TV"
        "TV Ended"   = "D:\ServerFolders\Videos\TV (Ended)"
    }
    
    # Default path for Movies
    $global:fixedPath = $pathMap["Movies"]
    $pathLabel.Text = "Scanning path: $global:fixedPath"
    
    # Add click event for Movies button
    $moviesButton.Add_Click({
        $global:fixedPath = $pathMap["Movies"]
        $pathLabel.Text = "Scanning path: $global:fixedPath"
        # Change button colors to indicate active path
        $moviesButton.BackColor = 'LightGreen'
        $tvButton.BackColor = [System.Drawing.SystemColors]::Control
        $tvEndedButton.BackColor = [System.Drawing.SystemColors]::Control
        # Start scanning the Movies path
        Write-Host "Scanning Movies path: $global:fixedPath"
        Scan-Folder -path $global:fixedPath
    })
    
    # Add click event for TV button
    $tvButton.Add_Click({
        $global:fixedPath = $pathMap["TV Current"]
        $pathLabel.Text = "Scanning path: $global:fixedPath"
        # Change button colors to indicate active path
        $tvButton.BackColor = 'LightGreen'
        $moviesButton.BackColor = [System.Drawing.SystemColors]::Control
        $tvEndedButton.BackColor = [System.Drawing.SystemColors]::Control
        # Start scanning the TV Current path
        Write-Host "Scanning TV path: $global:fixedPath"
        Scan-Folder -path $global:fixedPath
    })
    
    # Add click event for TV Ended button
    $tvEndedButton.Add_Click({
        $global:fixedPath = $pathMap["TV Ended"]
        $pathLabel.Text = "Scanning path: $global:fixedPath"
        # Change button colors to indicate active path
        $tvEndedButton.BackColor = 'LightGreen'
        $moviesButton.BackColor = [System.Drawing.SystemColors]::Control
        $tvButton.BackColor = [System.Drawing.SystemColors]::Control
        # Start scanning the TV Ended path
        Write-Host "Scanning TV Ended path: $global:fixedPath"
        Scan-Folder -path $global:fixedPath
    })
    
    # Function to scan the folder
    function Scan-Folder {
        param(
            [string]$path
        )
        Write-Host "Scanning folder: $path"
        # Insert the actual folder scanning logic here
    }
    
    
    # Path variables for Movies, TV, and TV Ended
    $pathMap = @{
        "Movies"     = "D:\ServerFolders\Videos\02 - Movies"
        "TV Current" = "D:\ServerFolders\Videos\01 - TV"
        "TV Ended"   = "D:\ServerFolders\Videos\TV (Ended)"
    }
    
    # Default path for Movies
    $global:fixedPath = $pathMap["Movies"]
    
    # Add click event for Movies button
    $moviesButton.Add_Click({
        $global:fixedPath = $pathMap["Movies"]
        $pathLabel.Text = "Scanning path: $global:fixedPath"
        # Change button colors to indicate active path
        $moviesButton.BackColor = 'LightGreen'
        $tvButton.BackColor = [System.Drawing.SystemColors]::Control
        $tvEndedButton.BackColor = [System.Drawing.SystemColors]::Control
        # Trigger the scanning logic
        Scan-Folder -path $global:fixedPath
    })
    
    # Add click event for TV button
    $tvButton.Add_Click({
        $global:fixedPath = $pathMap["TV Current"]
        $pathLabel.Text = "Scanning path: $global:fixedPath"
        # Change button colors to indicate active path
        $tvButton.BackColor = 'LightGreen'
        $moviesButton.BackColor = [System.Drawing.SystemColors]::Control
        $tvEndedButton.BackColor = [System.Drawing.SystemColors]::Control
        # Trigger the scanning logic
        Scan-Folder -path $global:fixedPath
    })
    
    # Add click event for TV Ended button
    $tvEndedButton.Add_Click({
        $global:fixedPath = $pathMap["TV Ended"]
        $pathLabel.Text = "Scanning path: $global:fixedPath"
        # Change button colors to indicate active path
        $tvEndedButton.BackColor = 'LightGreen'
        $moviesButton.BackColor = [System.Drawing.SystemColors]::Control
        $tvButton.BackColor = [System.Drawing.SystemColors]::Control
        # Trigger the scanning logic
        Scan-Folder -path $global:fixedPath
    })
    
    # Function to scan the folder
    function Scan-Folder {
        param(
            [string]$path
        )
        Write-Host "Scanning folder: $path"
        # Insert the actual scanning logic here to process the folder
    }
    
      # Right of the Movies button
    $tvButton.Add_Click({
        Start-Process "D:\ServerFolders\Videos\01 - TV"  # Link to the TV path
    })
    $form.Controls.Add($tvButton)
      # Right of the Scan button
    $moviesButton.Add_Click({
        Start-Process "D:\ServerFolders\Videos\02 - Movies"  # Link to the Movies path
    })
    $form.Controls.Add($moviesButton)
    
    
    # Globals
    $global:missingItems = @()
    
    function Run-Scan {
        $logBox.Clear()
        $summaryLabel.Text = ""
        $log = @()
        $logPath = Join-Path $fixedPath "missing_${global:selectedFile}_log.txt"
        $csvPath = Join-Path $fixedPath "missing_${global:selectedFile}_report.csv"
        $global:missingItems = @()
    
        if (-not (Test-Path $fixedPath)) {
            [System.Windows.Forms.MessageBox]::Show("Invalid folder path.", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
            return
        }
    
        $folders = Get-ChildItem -Path $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 = "All folders have $($global:selectedFile)."
        } else {
            $summaryLabel.Text = "Missing $($global:selectedFile) from [$($global:missingItems.Count)] folders."
        }
    
        $log | Out-File -FilePath $logPath -Encoding UTF8
    
        $logBox.AppendText("`r`nLog saved to: $logPath`r`n")
        $exportButton.Enabled = $global:missingItems.Count -gt 0
    }
    
    $scanButton.Add_Click({ Run-Scan })
    
    
    # Export to CSV button
    $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)
    
    $exportButton.Add_Click({
        $csvPath = Join-Path $fixedPath "missing_$($global:selectedFile)_report.csv"
    
        $csvPath = Join-Path $fixedPath "missing_$($global:selectedFile)_report.csv"
        $headerLines = @(
            "File type: $($global:selectedFile)",
            "Total missing: $($global:missingItems.Count)`r`n"
        )
        $headerLines | Out-File -FilePath $csvPath -Encoding UTF8
    
        $global:missingItems | ForEach-Object {
            [PSCustomObject]@{ FolderName = $_.Name; Path = $_.FullName }
        } | Export-Csv -Path $csvPath -Append -NoTypeInformation -Encoding UTF8
    
        $logBox.AppendText("CSV exported to: $csvPath`r`n")
    
    })
    
    
    $form.Topmost = $true
    $form.Add_Shown({ $form.Activate() })
    [void]$form.ShowDialog()

powershell
  • 1 个回答
  • 69 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve