提示用户的 Powershell 脚本

标签 powershell scripting file-manipulation

我将如何向我的脚本添加类似于重命名脚本的删除功能?它在哪里打开文件浏览器并允许用户选择要删除的文件?有可能还是我必须以其他方式进行?我需要有关选项 3 的帮助。

}
#now start the loop calling the function named Show-Menu
do {
    Show-Menu
    #ask the user to make a selection and read this in as a variable
    $options = Read-Host "Enter your choice"
    switc
        #Renames a folder
        '2' {
            $
            # Se = ($renamefolder -split "\\")[-1]
            # Change our path so the directories can be referenced more easily via
until ($options -eq 'q')

最佳答案

它非常简单(请看下面的代码)。我想指出几点。

我重命名了一个函数以复制它的结果。

如果在脚本中选择文件夹浏览器对话框时单击取消 - 它会走得更远。使用 do {} while () 循环它不会:)

function Get-FolderPath() {  
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
 
    $OpenFileDialog = New-Object System.Windows.Forms.FolderBrowserDialog
    do {
        $OpenFileDialog.ShowDialog() | Out-Null
        $OpenFileDialog.SelectedPath
    }
    while (!($OpenFileDialog.SelectedPath))
}

#Creates a function called Show-Menu
function Show-Menu {
    param (
        [string]$Title = 'My Folder Menu'
    )
    # Clear-Host
    Write-Host "==========Choose your option============="
    Write-Host "1. Press '1' to Create a Folder"
    Write-Host "2. Press '2' to Rename a Folder"
    Write-Host "3. Press '3' to Delete a Folder"
    Write-Host "Q. Press 'Q' to exit the script"
    Write-Host "========================================="
}
#now start the loop calling the function named Show-Menu
do {
    Show-Menu
    #ask the user to make a selection and read this in as a variable
    $options = Read-Host "Enter your choice"
    switch ($options) {
        #Creates a new directory
        '1' {
            #Get parent folder path
            $parentPath = Get-FolderPath
            #Get folder name
            $folderName = Read-Host -Prompt 'Input new directory name'
            #Create folder in parent path that has selected name 
            New-Item (Join-Path $parentPath $folderName) -ItemType Directory | Out-Null
        }
        #Renames a folder
        '2' {
            $renamefolder = Get-FolderPath
            $newFolderName = Read-Host 'Please enter the new name for the folder'
            # Split the path and get the last item in the array
            $oldFolderName = ($renamefolder -split "\\")[-1]
            #The same can be made using:
            #$oldFolderName = (Split-Path $renamefolder -Leaf)
    
            # Change our path so the directories can be referenced more easily via ./
            Push-Location (Join-Path $renamefolder -ChildPath ..)
            Move-Item -Path (Join-Path -Path ./ -ChildPath $oldFolderName) -Destination (Join-Path ./ -ChildPath $newFolderName)
            # Return process back to the original path
        }
        #Deletes a folder
        '3' {
            #Get folder path
            $folderToDelete = Get-FolderPath
            #Remove the item :)
            Remove-Item $folderToDelete -Verbose #-WhatIf #for testing
        }
    }
}
until ($options -eq 'q')

关于提示用户的 Powershell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67998560/

相关文章:

regex - PowerShell使用正则表达式来分割字符串

powershell - 如何在 powershell 函数中自定义示例的标题

Powershell:在 32 位模式下运行时缺少 DLL 文件

bash - 如何使用 bash 在两个已知行 block 之间的文件中插入一行(如果之前尚未插入)?

python - 检索文件路径的尾端

powershell - 如何在 PowerShell 二进制模块中捕获外部 DLL 的控制台输出?

scripting - 带有 unc 路径的 xcopy 添加虚拟网络驱动器

c# - 多个文件,同名

git - 用 git 或 hub 命令替换 GitHub UI 序列

linux - 在linux中使用tac命令查找和反转多个文件中的文本