0

I have a script with a GUI that pops up the Windows Explorer and allows a user to choose a file to export the data too. However, I would like to manipulate some files inside the directory that they choose but when I try to use Split-Path on the filename I am given this error:

Set-Location : Cannot find drive. A drive with the name 'System.Windows.Forms.TextBox, Text' does not exist.

Is there a way to remove the beginning part of that returned text and only get the path text inside?

//users selected path from GUI Note* this is the path but it is selected by the gui button through file explorer
$UsersPath = "C:\username\desktop\MyFolder\myFile.csv"

$newPath = Split-Path -Path "$UsersPath"

Set-Location "$newPath.Text"

//In this case, $newPath = System.Windows.Forms.TextBox, Text: C:\Users\username\Desktop\myFolder

I have tried using .Text and .ToString to no avail.

Below is my code that lets me open a Dialog box to ask the user to choose a file.

function open_CSV_File{

    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $openFileDialog.InitialDirectory = "C:\";
    $OpenFileDialog.Filter = "csv files (*.csv)|*.csv"
    "
    if ($OpenFileDialog.ShowDialog() -eq "OK"){
        $textbox_BrowseForCSV.Text = $OpenFileDialog.FileName 
    }
}
binjamin
  • 123
  • 1
  • 13
  • 5
    The code you posted would not throw that error. Please create a [mcve] showing where "users selected path" comes from and update your question with that. – Ansgar Wiechers Aug 22 '19 at 21:32
  • I'm assuming `$newPath` is in reality a `TextBox` instance, which would explain the symptom; Rich's answer and the linked post show how to access an object property inside an expandable string (`"..."`), but note that you don't need an expandable string at all in your scenario; `Set-Location $newPath.Text` should do. – mklement0 Aug 22 '19 at 22:44
  • Thanks @mklement0. Yes you are correct in saying that ```Set-Location $newPath.Text``` should work. My problem was the initial splitting of the $userPath. Appreciate the help. – binjamin Aug 23 '19 at 13:42
  • @AnsgarWiechers it was throwing the error because I was trying to set the location to ``System.Windows.Forms.TextBox, Text: C:\Users\username\Desktop\myFolder`` which it did not recognize. – binjamin Aug 23 '19 at 13:44
  • Again, the sample code you posted would not have thrown that error, because `"$newPath.Text"` would have evaluated to `"C:\username\desktop\MyFolder.Text"` (and thus probably have caused a "cannot find path" error). Which would be apparent to you had you actually test-run the code before posting. That is the exact reason why I've asked you to create a [mcve]. – Ansgar Wiechers Aug 23 '19 at 14:30
  • I wouldn't be asking the question if it was evaluating to the correct path....Not sure what other code you expect to be posted since I posted mostly everything I wrote. Also, How would I have gotten that error If I had not already "test-run the code"? The problem was solved by a helpful user below @Rich Moss – binjamin Aug 23 '19 at 14:48

1 Answers1

2

Change this:

Set-Location "$newPath.Text"

To this:

Set-Location "$($newPath.Text)"
Rich Moss
  • 2,195
  • 1
  • 13
  • 18
  • 2
    Good catch, though note that in this simple case there is no need to use an expandable string at all - `Set-Location $newPath.Text` will do. – mklement0 Aug 22 '19 at 22:45
  • Rich thanks for the suggestion! This syntax actually solved my problem but only when I used it when splitting the users path initially. For example: ```Split-Path -Path "$($userInput.Text)"``` got me "C:\users\desktop\myFolder". Thanks! – binjamin Aug 23 '19 at 13:36