Dirname of something that does not exist

Why… Why does it work this way:

On Unix, this is easy:

    tmp=`dirname /some/directory/that/does/not/exist/foobar.txt`
    mkdir -p $tmp

Returns: /some/directory/that/does/not/exist

Same thing in Python:

   (dirname, basename) = os.path.split("/some/directory/that/does/not/exist/foobar.txt")

It just works.

Same thing in tcl

    set F "/some/directory/that/does/not/exist/foobar.txt"
    puts [file $F dirname]
    puts [file $F tail]

I don’t have PERL but I’m sure it works too.

What about power shell?

    $tmp=Get-item -path /some/directory/that/does/not/exist/foobar.txt
    # Raises ERROR because it does not exit yet.
    New-item -Path $tmp > $null

Question:

  • Why does the thing need to exist?
  • Other script languages do not require this

What do I want:

  • I don’t want an answer here in line.
  • I want a link to documentation that shows how to do this.

Why? Because When I try to do what I think are simple things in PowerShell I end up frustrated as hell. I cannot find basic examples. At times I think nobody writing or architecting PowerShell have actually use it in a way that somebody familiar with other script languages think it should work.

You are comparing different things. In the first and third example, you appear to be setting a variable and then acting upon that variable. In the powershell example you are querying the nonexistent path and then attempting to make a new item on an empty variable.

This seems like a more equivalent comparison

$tmp= '/some/directory/that/does/not/exist/foobar.txt'
New-item -Path $tmp -Force > $null

For the python example, You are simply splitting a path string into two variables. This would be a better equivalent.

$tmp = '/some/directory/that/does/not/exist/foobar.txt'
$dirname = [System.IO.Path]::GetDirectoryName($tmp)
$basename = [System.IO.Path]::GetFileName($tmp)

Or a more powershell idiomatic version

$tmp = '/some/directory/that/does/not/exist/foobar.txt'
$dirname = $tmp | Split-Path -Parent
$basename = $tmp | Split-Path -Leaf

In all these examples, the thing did not need to exist. If you actually tried to query the directory in the unix example before creating it, you would also get an error.

tmp=`dirname /some/directory/that/does/not/exist/foobar.txt`
dir $tmp
dir: cannot access '/some/directory/that/does/not/exist': No such file or directory

It makes plenty of sense if you compare apples to apples

1 Like

I sure understand that switching syntax between languages, especially coming from a Linux background going to PowerShell, can be confusing. My whole career is based on Windows and PowerShell. Give me a bash prompt or a python script and I’m lost.

Another way to explain this. Get-Item is trying to get an item, just like the command says. If the item does not exist, you cannot get it. Thus error.

Like @krzydoug said, the other examples you gave are not doing the equivalent of getting an item before creating it, so no error.

One of the best things about PowerShell commands is that you can usually tell what they are trying to do by the command itself. New-Item creates a new item. Get-Item gets an item. Since you are working with the file system, the item is a file or folder. Also, when something in PowerShell doesn’t seem to make sense, reading the help is usually helpful.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.