简体   繁体   English

有没有办法让 powershell 根据用户是否单击打开文件对话框中的取消按钮来结束循环?

[英]Is there a way to get powershell to end a loop based upon if the user clicks the cancel button in an open file dialog?

Is there a way to get powershell to end a loop based upon if the user clicks the cancel button in an open file dialog?有没有办法让 powershell 根据用户是否单击打开文件对话框中的取消按钮来结束循环? I cannot figure it out, the only thing I have gotten to work so far is to create a file called cancel.txt and to end the loop when that one is selected in the open file dialog.我无法弄清楚,到目前为止,我唯一要做的就是创建一个名为 cancel.txt 的文件,并在打开文件对话框中选择该文件时结束循环。

do {
cls
clear-variable filearray -ErrorAction Ignore

$ask = read-host "Would you like to Hash? (y/n)"

if ($ask -eq "y" -or $ask -eq "Y") {
cls
Function Get-FileName($initialDirectory)
{   
 [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
 Out-Null

 $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
 $OpenFileDialog.initialDirectory = $initialDirectory
 $OpenFileDialog.filter = "All files (*.*)| *.*"
 $OpenFileDialog.ShowDialog() | Out-Null
 $OpenFileDialog.filename
} 

$filearray = @()

do {
$file = Get-FileName

foreach ($files in $file) {
$filearray += $files
}

} until ($file -match "cancel.txt")

foreach ($filearrays in $filearray) {
$c = Get-FileHash -algorithm SHA256 -path $filearrays | foreach-object {"Algorithm: $($_.Algorithm)", "Hash: $($_.Hash)"}

"File: $filearrays"
""
$c
""
}

if ($save -eq "n" -or $save -eq "N") {
}
}

if ($ask -eq "n" -or $ask -eq "N") {
exit
}
} until ($ask -eq "n" -or $ask -eq "N")

The System.Windows.Forms.OpenFileDialog object also has a property with which you can set it so the user can select multiple files. System.Windows.Forms.OpenFileDialog object 还具有一个属性,您可以使用它设置它,以便用户可以设置多个文件 Z99EF949282F04016

If you use that, you can save yourself a lot of repeating the dialog, one file at a time.如果你使用它,你可以节省很多重复对话框,一次一个文件。 Another helpful property is CheckFileExists with which you prevent users to type in just about anything.另一个有用的属性是CheckFileExists ,您可以使用它阻止用户输入任何内容。

Anyway, I would change your code to this:无论如何,我会将您的代码更改为:

# set this to a path and filename of a CSV file of your choice.
$outputFile = 'D:\Test\FileHashes.csv'

function Get-FileName ([string]$initialDirectory) { 
    Add-Type -AssemblyName System.Windows.Forms

    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $OpenFileDialog.InitialDirectory = $initialDirectory
    $OpenFileDialog.CheckFileExists = $true   # so the user does not type in gibberish
    $OpenFileDialog.Multiselect = $true       # allows the user to select more than one file
    $OpenFileDialog.Filter = "All files (*.*)| *.*"

    # if the user clicked OK, capture and output the selected file names
    # otherwise return nothing
    $result = $OpenFileDialog.ShowDialog()
    if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
        $selected = $OpenFileDialog.FileNames
    }
    $OpenFileDialog.Dispose()
    if ($selected) { return ,$selected }  # force an array return by using the unary comma
}

# start an endless loop
while ($true) {
    Clear-Host
    $ask = Read-Host "Would you like to Hash? (y/n)"

    if ($ask -notlike "Y*") { break }  # exit the loop

    $selectedFiles = Get-FileName -initialDirectory ([Environment]::GetFolderPath("MyDocuments"))

    # if nothing is returned, exit the loop
    if (!$selectedFiles -or $selectedFiles.Count -eq 0) { break }

    # calculate the hash values for each selected file
    $hashes = foreach ($file in $selectedFiles) {
        Write-Host "Calculating Hash for '$file'"
        Get-FileHash -Algorithm SHA256 -Path $file
    }
    # here you can save the hash(es)
    $hashes | Export-Csv -Path $outputFile -UseCulture -NoTypeInformation -Append
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM