【问题标题】:Powershell script that prompts user提示用户的 Powershell 脚本
【发布时间】:2021-09-01 00:44:24
【问题描述】:

如何在脚本中添加类似于重命名的删除功能?它在哪里打开文件浏览器并允许用户选择要删除的文件?有可能还是我必须以另一种方式来做?我需要关于选项 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')

【问题讨论】:

    标签: powershell scripting file-manipulation


    【解决方案1】:

    这很简单(观看下面的代码)。我想指出几点。

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

    如果您在脚本中选择文件夹浏览器对话框时单击Cancel - 它会更进一步。使用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')
    

    【讨论】:

    • @FearThainn 您的意思是要选择路径,输入名称并在该选定路径中创建文件夹吗?
    • 编辑答案以复制您正在寻找的内容
    猜你喜欢
    • 1970-01-01
    • 2018-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 2020-09-03
    • 2010-10-01
    相关资源
    最近更新 更多