2

How would I go about adding a delete feature to my script that is similar to the rename one? Where it opens up the file browser and allows a user to select a file to delete? Is it possible or do I have to do it another way? I need help with option 3 please.

}
#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')

1 Answer 1

2

It's pretty straightforward (watch code below). Few points I'd like to note.

I've renamed a function to replicate the result of it.

If you click Cancel when selecting folder browser dialog in your script - it would go further. With the do {} while () loop it won't :)

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')
Sign up to request clarification or add additional context in comments.

2 Comments

@FearThainn do you mean you want to select path, input a name and create a folder in that selected path?
Edited answer to replicate what you're looking for

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.