none
How to use a BrowseForFolder box in my powershell script? RRS feed

  • Question

  • Hello,

    I have the following script where I want to ask the user to select the folder they want the script to process the instructions from.  I've added a function for Select-Folder as follows:

    function Select-Folder($message='Select a folder', $path = 0) {
    $object = New-Object -comObject Shell.Application
    $folder = $object.BrowseForFolder(0, $message, 0, $path)  
    if ($folder -ne $null) {    
    	$folder.self.Path
    	}
    }
    
    Select-Folder 'Select the folder where the move scripts reside'
    
    $files=Get-ChildItem $folder -Filter "*.txt"
    foreach ($file in $files){
    	$outFileName=[IO.Path]::GetFileNameWithoutExtension($file.Name) + "_StartVMs.txt"
    	Get-Content $file.FullName | foreach{
    		 Add-Content $folder\$outFileName "Start-ClusterResource ""$($_.Split('"')[1])"""
    	}
    }

    Issue #1:

    The .txt files I am creating live in the same location as where this script lives. What I've found is the path I choose in the browseforfolder box that pops up is ignored and the files are converted in the location where the script lives.

    Issue #2:

    How do I get the Cancel button to work so the script will exit if the user selects Cancel?

    Any help you can provide would be greatly appreciated.

    Thank you.


    • Edited by greavette Friday, December 20, 2013 2:00 PM updated title
    Friday, December 20, 2013 2:00 PM

Answers

  • Hi,

    You're not setting a variable with the output of your function, so you don't have anything to test on. Give this slight modification a try (you'll need to work this back into your script, this is demo stuff only):

    function Select-Folder($message='Select a folder', $path = 0) {
    $object = New-Object -comObject Shell.Application
    $folder = $object.BrowseForFolder(0, $message, 0, $path)  
    if ($folder -ne $null) {    
    	$folder.self.Path
    	}
    }
    
    $folderPath = Select-Folder 'Select the folder where the move scripts reside'
    
    Write-host "The selected folder path is $folderPath"
    
    If ($folderPath) { Write-Host 'I have a folder, so keep processing in here' }
    Else { Write-Host 'I do not have a folder path' }


    Don't retire TechNet! - (Don't give up yet - 12,420+ strong and growing)

    • Marked as answer by greavette Friday, December 20, 2013 6:12 PM
    Friday, December 20, 2013 2:19 PM

All replies

  • Hi,

    You're not setting a variable with the output of your function, so you don't have anything to test on. Give this slight modification a try (you'll need to work this back into your script, this is demo stuff only):

    function Select-Folder($message='Select a folder', $path = 0) {
    $object = New-Object -comObject Shell.Application
    $folder = $object.BrowseForFolder(0, $message, 0, $path)  
    if ($folder -ne $null) {    
    	$folder.self.Path
    	}
    }
    
    $folderPath = Select-Folder 'Select the folder where the move scripts reside'
    
    Write-host "The selected folder path is $folderPath"
    
    If ($folderPath) { Write-Host 'I have a folder, so keep processing in here' }
    Else { Write-Host 'I do not have a folder path' }


    Don't retire TechNet! - (Don't give up yet - 12,420+ strong and growing)

    • Marked as answer by greavette Friday, December 20, 2013 6:12 PM
    Friday, December 20, 2013 2:19 PM
  • Thanks for the reply Mike.  Works Well!

    Can the Make New Folder button be removed from this BrowseForFolder box?  Not a big problem but it really doesn't apply here since we only want a folder with files in it and not an empty folder.

    Here is my script with your changes in them:

    function Select-Folder($message='Select a folder', $path = 0) {
    $object = New-Object -comObject Shell.Application
    $folder = $object.BrowseForFolder(0, $message, 0, $path)  
    if ($folder -ne $null) {    
    	$folder.self.Path
    	}
    }
    
    $folderPath = Select-Folder 'Select the folder where the move scripts reside'
    
    If ($folderPath) {
        $files=Get-ChildItem $folderPath -Filter "*.txt"
        foreach ($file in $files){
        	$outFileName=[IO.Path]::GetFileNameWithoutExtension($file.Name) + "_StartVMs.txt"
        	Get-Content $file.FullName | foreach{
        		 Add-Content $folderPath\$outFileName "Start-ClusterResource ""$($_.Split('"')[1])"""
        	}
        }
    }
    Else { Write-Host 'I do not have a folder path' }
    Thank you.
    Friday, December 20, 2013 2:43 PM
  • Thanks for the reply Mike.  Works Well!

    Can the Make New Folder button be removed from this BrowseForFolder box?  Not a big problem but it really doesn't apply here since we only want a folder with files in it and not an empty folder.

    Here is my script with your changes in them:

    <snip>

    Thank you.
    That's great, I'm glad it's working. AFAIK, there isn't a way to remove that button from the dialog box though.

    Don't retire TechNet! - (Don't give up yet - 12,420+ strong and growing)

    Friday, December 20, 2013 3:02 PM
  • Thank you for confirming this Mike.  Very much appreciated!
    Friday, December 20, 2013 6:12 PM
  • The second zero in $folder = $object.BrowseForFolder(0, $message, 0, $path)  is documented as ulFlags. I have yet to get them all to work the way I would like. Would love to share the link, but keep getting an error (unconfirmed account). So try GOOGLE with keywords

    powershell msdn browseforfolder -vba -visualbasic -FolderBrowserDialog


    $folder = $object.BrowseForFolder(0, $message, 0x0270, $path) 

    270 will let the user resize the dialog box, provides an edit box to type a location, and eliminates the new folder option. 0x200 handles the new folder option. 0x0070 is a combination of 10, 20, and 40.

    Thursday, December 31, 2015 1:47 AM
  • Question is marked as answered three years ago.  You are posting to an old and closed thread.

    \_(ツ)_/

    Thursday, December 31, 2015 2:39 AM