Reputation: 65
I have a function that returns a folder name with full path, like C:\Folder\Subfolder.
I then extract the name of the subfolder using Split-Path.
It works like expected when its folders or subfolders like C:\Folder or C:\Folder\Subfolder, but not when a drive is selected
Here's my code. The "Get-folder" function is just a file dialogue where the user can select a folder:
$FolderNavn = Get-Folder
$FolderNavnKort = Split-Path -Path $Foldernavn -Leaf
Here's what I'm expecting:
if the folder is 'C:\Folder\Subfolder' it should return "Subfolder" (works)
If the folder is just 'C:\Folder' it should return "Folder" (works)
If C:\ is selected (and thus not a folder) is should return "", but instead I get:
Out-File : The given path's format is not supported.
When I'm testing, $FolderNavn returns "C:\".
Any suggestions?
Edit, content of Get-Folder:
function Get-Folder {
[CmdletBinding()]
param (
[Parameter(Mandatory=$false, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Position=0)]
[string]$Message = "Please select a directory.",
[System.Environment+SpecialFolder]$InitialDirectory = [System.Environment+SpecialFolder]::MyComputer,
[switch]$ShowNewFolderButton
)
Add-Type -AssemblyName System.Windows.Forms
$dialog = New-Object System.Windows.Forms.FolderBrowserDialog
$dialog.Description = 'Select a directory.'
$dialog.RootFolder = $InitialDirectory
$dialog.ShowNewFolderButton = if ($ShowNewFolderButton) { $true } else { $false }
$selected = $null
# force the dialog TopMost
# Since the owning window will not be used after the dialog has been
# closed we can just create a new form on the fly within the method call
$result = $dialog.ShowDialog((New-Object System.Windows.Forms.Form -Property @{TopMost = $true }))
if ($result -eq [Windows.Forms.DialogResult]::OK){
$selected = $dialog.SelectedPath
}
# clear the FolderBrowserDialog from memory
$dialog.Dispose()
# return the selected folder
$selected
}
Upvotes: 0
Views: 1784
Reputation: 472
Based on some research, it seems this is likely due to an extra colon in the file path. Without the code relevant to building the path / file ($KanalerStiNavn
), my guess is that it's attempting to create a folder with the leaf subfolder you're retrieving earlier in this line.
$FolderNavnKort = Split-Path -Path $Foldernavn -Leaf
When you have a subfolder (such as "Subfolder", "Folder" as in your example) the returned value can be created as a folder in your desired directory in the Out-File
command. Unfortunately, when (in the above line) your $foldernavn
is the root, it returns "C:\" as the folder, and you try to create a folder in another directory including that
# Assuming the root export folder is D:\newfolder
# When $foldernavn is C:\folder (and therefore $FolderNavnKort is "folder")
$KanalerStiNavn could be "D:\newfolder\folder"
# When $foldernavn is C:\ (and therefore $FolderNavnKort is "C:\")
$KanalerStiNavn could be "D:\newfolder\C:\"
The second example is an invalid filename, hence the error.
To correct this you need to handle the case that the returned value of the Split-Path
function contains a colon. Could be as simple as:
if ($FolderNavnKort -like "*:*")
{
$FolderNavnKort = ""
}
Not overly beautiful, but should do the job.
Upvotes: 1