Database Powershell文件数据库与驱动器号变量输入的比较
我正在建立一个工具,将扫描我的文件和朋友的文件。我们将使用它来确保我们的数据库中有相同的文件。到目前为止,我的脚本存在一个变量输入问题。由于某些原因,PowerShell脚本在我的驱动器号输入中失败。有人有什么想法吗 这是我的剧本:Database Powershell文件数据库与驱动器号变量输入的比较,database,file,powershell,variables,scanning,Database,File,Powershell,Variables,Scanning,我正在建立一个工具,将扫描我的文件和朋友的文件。我们将使用它来确保我们的数据库中有相同的文件。到目前为止,我的脚本存在一个变量输入问题。由于某些原因,PowerShell脚本在我的驱动器号输入中失败。有人有什么想法吗 这是我的剧本: { function Show-Menu { param ( [string] $Title = "Andy's Manual Database Tool" ) Clear-Host
{
function Show-Menu {
param (
[string] $Title = "Andy's Manual Database Tool"
)
Clear-Host
Write-Host ""
Write-Host "================ $Title ================"
Write-Host ""
Write-Host -f Green "1. Andys Files listing"
Write-Host -f green "2. Reids files listing"
Write-Host -f Red "3. Dark Matter Testing"
Write-Host "4. Convert .txt to .csv"
Write-Host "5. Convert Blank File to .csv"
Write-Host "6. Convert .csv to .txt"
}
Function Body {
Show-Menu
Write-Host ""
$Input = Read-Host "Please make a selection"
if ($Input -eq "1") {
Clear-Host
$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
}
if ($Input -eq "2") {
Clear-Host
$root = Read-Host -Prompt 'Specify the location of Database? Example format: C:\*'
$y = read-host -Prompt 'Input file types. File Format Examples: " .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' From Reid.csv'
}
if ($Input -eq "3") { Get-Process | Stop-Process }
if ($Input -eq "4") {
Clear-Host
Get-ChildItem *.txt | rename-item -newname { $_.name -replace ".txt",".csv" }
}
if ($Input -eq "5") {
Clear-Host
Get-ChildItem * -Exclude *.ps1,*.CSV,*.TXT | rename-item -newname { "$($_.name).CSV" }
}
if ($Input -eq "6") {
Clear-Host
Get-ChildItem *.csv | rename-item -newname { $_.name -replace ".csv",".txt" }
}
Write-Host 'Complete! ^_^'
Start-Sleep -seconds 5
Body
}
Body
}
根据上面的脚本,本期内容如下:
$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
我正在使用
$root
作为问题所在的驱动器号或位置路径的输入 有几个小问题
$z Andy.csv
需要更改为“$z Andy.csv”
。您将注意到,当您运行此操作时,您将收到以下消息:Get ChildItem-Path$root-File-Recurse*$y
需要更改为Get ChildItem-Path“$root”-Recurse-Include“*$y”
-提示用户将文件扩展名设置为
(分配给$y
),然后尝试使用$y
进行筛选
例如:$y=读取主机-提示“输入文件类型”。格式示例:“.jpg、.mp4、.mp3、.pdf…”Do*for all“
问题:“*.y”
这将等于“.extension”。您的结果将是0(除非您有一个.jpg
或类似文件)if ($Input -eq "1") {
Clear-Host
$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" -Recurse -Include "*$y" | Select Name | Export-Csv -NoTypeInformation "$z Andy.csv"
}
您应该能够按照“更正的”示例更正脚本的其余部分
编辑:
贷记不可抗力1
除预期用途外,不得使用任何其他用途。在您的情况下,$Input
:
包含枚举器,该枚举器枚举传递给
功能。$input变量仅适用于函数和
脚本块(未命名函数)。在
函数中,$input变量枚举当前
在筹备中当进程块完成时,没有
对象留在管道中,因此$input变量枚举
空集合。如果函数没有进程块,则
在End块中,$input变量枚举所有
函数的输入
然而,在这种情况下,它不应该影响你的结果。但是,这是一个非常坏的习惯,最好避免 您输入的是什么?如果格式正确,会立即注意到您的问题。永远不要使用自动变量作为变量名。在这种情况下:
$Input
嗨,雅各布·科尔文,我正在输入驱动器号。所以它可能是c:\或c:\temp\或d、g或f:\@rogerwilcolta您所说的“问题”代码对我来说运行良好?您没有解决最突出的问题,即变量名$Input
@的使用。在本例中,$Input始终为0,对吗?由于函数未参数化。@不可纠正错误1我将使用$Input
作为变量来解决此问题。但是,这不会影响脚本的当前功能。它绝对会影响脚本$Input
对他正在链接的函数具有非常特殊的功能together@TheIncorrigible1我意识到了这个错误,但我不认为在这种情况下它会破坏任何东西<代码>功能显示菜单{$Input};1..3 |显示菜单;函数显示菜单{$Input=2;$Input;$Input=3;$Input};1..3|显示菜单