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

PowerShell文件对比脚本盘符输入变量故障求助

Fixing Your PowerShell Script's Variable Input & Path Issues

Hey there! Let's work through the problems in your script that are causing failures when you input drive paths like C:\*. I'll break down the key issues and show you how to fix them step by step.

Key Problems in the Troublesome Code Snippet

First, let's look at the specific lines you flagged and why they're breaking:

$root = Read-Host -Prompt 'Specify the location of Database? Example format: C:\*'
$y = read-host -Prompt 'Input file types. Format Example: " .jpg,.mp4,.mp3,.pdf .... " Do * for all'
$z = Read-Host -Prompt 'Your Save file name will be? Examples: Movies database , Music database , audiobooks.'
Get-ChildItem -Path $root -File -Recurse *.$y | Select-Object -Property Name | Export-Csv -NoTypeInformation $z Andy.csv

1. Conflicting Wildcards in Get-ChildItem

When you input C:\* for $root, you're already adding a wildcard to the Path parameter. Appending *.$y afterward creates invalid syntax—PowerShell doesn't expect a free-standing wildcard string after the cmdlet parameters. We need to use the dedicated -Include parameter for file type filtering instead.

2. Broken Multiple File Type Handling

Your prompt tells users they can input comma-separated types like .jpg,.mp4, but *.$y won't parse that correctly. Get-ChildItem's -Include parameter accepts an array of patterns, so we need to split the user's input into a proper array.

3. Misformatted Export-Csv Filename

Export-Csv -NoTypeInformation $z Andy.csv passes two separate arguments ($z and Andy.csv) to the cmdlet, which makes PowerShell misinterpret the output path. We need to combine these into a single string to get a valid filename.

4. Using a Reserved Automatic Variable

$Input is a built-in PowerShell automatic variable—using it to store user input will cause unexpected behavior. Rename it to something like $userSelection to avoid conflicts.

Fixed Code for Menu Options 1 & 2

Here's the corrected version of the problematic sections, with comments explaining each change:

Fixed Option 1:

if ($userSelection -eq "1") {
    Clear-Host
    $root = Read-Host -Prompt 'Specify the location of Database? Example format: C:\'
    $fileTypes = Read-Host -Prompt 'Input file types. Format Example: ".jpg,.mp4,.mp3,.pdf" Do * for all'
    $saveName = Read-Host -Prompt 'Your Save file name will be? Examples: Movies database, Music database, audiobooks.'

    # Clean up input: remove extra spaces and split into an array of file types
    $cleanFileTypes = $fileTypes -replace '\s+', '' -split ','
    # Build valid filter patterns (add * prefix unless user entered *)
    $filterPatterns = $cleanFileTypes | ForEach-Object {
        if ($_ -eq "*") { "*" } else { "*$_" }
    }

    # Use Get-ChildItem correctly with Path and Include parameters
    Get-ChildItem -Path $root -File -Recurse -Include $filterPatterns |
        Select-Object -Property Name |
        Export-Csv -NoTypeInformation -Path "$saveName Andy.csv"
}

Fixed Option 2:

if ($userSelection -eq "2") {
    Clear-Host
    $root = Read-Host -Prompt 'Specify the location of Database? Example format: C:\'
    $fileTypes = Read-Host -Prompt 'Input file types. File Format Examples: ".jpg,.mp4,.mp3,.pdf" Do * for all'
    $saveName = Read-Host -Prompt 'Your Save file name will be? Examples: Movies database, Music database, audiobooks.'

    $cleanFileTypes = $fileTypes -replace '\s+', '' -split ','
    $filterPatterns = $cleanFileTypes | ForEach-Object {
        if ($_ -eq "*") { "*" } else { "*$_" }
    }

    Get-ChildItem -Path $root -File -Recurse -Include $filterPatterns |
        Select-Object -Property Name |
        Export-Csv -NoTypeInformation -Path "$saveName From Reid.csv"
}

Critical Fix for the Full Script

Don't forget to update the main menu input variable to avoid using the reserved $Input:

# Replace $Input with $userSelection
$userSelection = Read-Host "Please make a selection"

How to Test the Fixed Script

  1. When prompted for the database location, input C:\ (no trailing *—the -Include parameter handles filtering now).
  2. For file types, try inputting .jpg,.mp4 (no spaces) or just * to include all files.
  3. The save filename will correctly combine your input with the suffix (e.g., "Movies database Andy.csv").

This should resolve the path input failures and make your file listing functionality work as expected!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 07:24:58