new-item path failure

Why does this fail:

cd \temp 
new-item d 
new-item -ItemType Directory d

This appears to have the same behavior but no error:

$file = New-Object System.IO.FileInfo("c:\temp\d")

$file.Directory.create()

In both cases using powershell and then the c# methods I can’t seem to get a folder to be created with the same name as a file. If the condition is that the file exists already called d and I try to create the folder the new-item -itemtype directory complains that the file already exists.

It’s because each object must have a unique name inside a folder.

File or folder is only an attribute of those objects.

Files need extension. Provide extension when creating file which will address this problem.

Thank you.

[quote quote=132833]Files need extension. Provide extension when creating file which will address this problem.

[/quote]

While this is the most common practice to create files with extensions and folders without extension, technically you can create files without an extension and folders with an extension.

It’s only the attribute which makes the difference.

Look at the Mode property of the following examples.

Creating file and folder WITHOUT an extension

PS C:\Temp> New-Item d -ItemType File

    Directory: C:\Temp

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        05-Jan-19     10:33              0 d

PS C:\Temp> Remove-Item d
PS C:\Temp> New-Item d -ItemType Directory

    Directory: C:\Temp

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        05-Jan-19     10:33                d

PS C:\Temp>

Creating file and folder WITH an extension

PS C:\Temp> New-Item d.ps1 -ItemType File

    Directory: C:\Temp

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        05-Jan-19     10:34              0 d.ps1

PS C:\Temp> Remove-Item d.ps1
PS C:\Temp> New-Item d.ps1 -ItemType Directory

    Directory: C:\Temp

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        05-Jan-19     10:34                d.ps1

PS C:\Temp>

That’s why a name (including the possible extension) must be unique among all files and folders inside a same folder.

Only files can have extensions. .txt, .log etc in a directory name are not extensions. Extensions are property of System.Io.Info objects. you can try

$r = Get-Item -Path .\d.ps1 #file
$r.Extension

#or

$r = Get-Item -Path .\d.ps1 #directory
$r.Extension

I tried and here is what I get, either with PS 5.1 or PS 6.1.

(The Get-Childitem in the middle is just to show that there is nothing else inside this folder than d.ps1)

PS C:\Temp> New-Item d.ps1 -ItemType Directory

Directory: C:\Temp

Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 05-Jan-19 17:07 d.ps1

PS C:\Temp> Get-ChildItem

Directory: C:\Temp

Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 05-Jan-19 17:07 d.ps1

PS C:\Temp> (Get-Item d.ps1).Extension
.ps1
PS C:\Temp>

My understanding of that is that folder objects can have extensions too…

My executions contained a slash, hence empty extension

Get-Item .\d.ps1\ | fl *

[quote quote=132960]My executions contained a slash, hence empty extension
Get-Item .\d.ps1\ | fl *
[/quote]

Woao! That’s weird and interesting at the same time!

If you get a filesystem object with or without the final slash it doesn’t return the same property values.

Good to know, and I hope I will remember it the day I need this information…

Even more weird: you can get a file with a final backslash and it has the same behaviour as for the folder…

PS C:\Temp> New-Item d.ps1 -ItemType File


    Directory: C:\Temp


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        06-Jan-19     10:02              0 d.ps1


PS C:\Temp> (Get-Item .\d.ps1).Extension
.ps1
PS C:\Temp> (Get-Item .\d.ps1\).Extension

PS C:\Temp>

It seems like it totally fails to fill both Name and Extension properties entirely when you retrieve it with a final slash:

https://gist.github.com/vexx32/935aa7fa277505b04c095ee290bb311b

Probably that would be a wrong usage on files with having a slash at the end. I think it tries assumes it as a directory and tries gets the items inside it which is not available, which works for a directory even without any child items. But it should ideally trim the slash and get the desired result.

If we do tab completion, it shows the slash at the end only for directories and not for the files :stuck_out_tongue_winking_eye: .