Search

Split-Path gets error if drive doesn't exist locally by Stephen Mills

Closed
as Fixed Help for as Fixed

8
0
Sign in
to vote
Type: Bug
ID: 499194
Opened: 10/21/2009 11:50:26 AM
Access Restriction: Public
0
Workaround(s)
6
User(s) can reproduce this bug
If you have a path that you want to split that was generated on a remote system, Split-Path will generate an error unless you have the same drive letter on your local system. If you leave out the drive letter and ":", then it works as expected, except you no longer have a reference to the drive letter. Since it doesn't validate any other part of the path, it shouldn't validate the drive letter existing either.
Details (expand)
How often does this happen?
Always Happens

Have you seen this problem before in this product?

I don't know if this issue existed previously
Reproduction Steps
Use PowerShell V2 RC on Windows XP
Execute:
Split-Path -Path 'T:\Backups\2009\10\DBBackup.BAK' -Leaf
Results:
Split-Path : Cannot find drive. A drive with the name 'T' does not exist.
At line:1 char:11
+ Split-Path <<<< -Path 'T:\Backups\2009\DBBackup.BAK'
    + CategoryInfo         : ObjectNotFound: (T:String) [Split-Path], DriveNotFoundException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.SplitPathCommand
Expected Results
As long as the path has a valid format, it should split it as if the drive letter did exist.

Execute:
Split-Path -Path 'T:\Backups\2009\10\DBBackup.BAK' -Leaf
Results:
DBBackup.BAK

or
Execute:
Split-Path -Path 'T:\Backups\2009\10\DBBackup.BAK' -Parent
Results:
DBBackup.BAK
T:\Backups\2009\10

File Attachments
0 attachments
Sign in to post a comment.
Posted by SadMacc on 2/17/2013 at 12:32 PM
I can reproduce the problem in Powershell 2.0:

Split-Path : Cannot find drive. A drive with the name 'D' does not exist.
    + CategoryInfo         : ObjectNotFound: (D:String) [Split-Path], DriveNotFoundException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.SplitPathCommand

This is strange, I assumed Split-Path didn't hit the file system, and got the error above.

An ugly but robust work-a-round is to use [System.IO.Path]::GetDirectoryName, or [System.IO.Path]::GetFileName if you're using the -Leaf option. Those functions don't hit the local file system, and I assumed Split-Path used these functions internally.

This is a dangerous bug which can surprise everyone when you least expect it. Even when using a single computer the drive configuration can change.

Lesson: Don't use Split-Path in ANY powershell code, use the [System.IO.Path] functions instead. Can we have some feedback if this will be fixed in Powershell 2.0 at all?
Sign in to post a workaround.