new-item path failure

Welcome Forums General PowerShell Q&A new-item path failure

This topic contains 11 replies, has 4 voices, and was last updated by

 
Participant
2 weeks, 1 day ago.

  • Author
    Posts
  • #132828

    Participant
    Points: 57
    Rank: Member

    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.

  • #132831

    Participant
    Points: 201
    Helping Hand
    Rank: Participant

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

    File or folder is only an attribute of those objects.

  • #132833

    Participant
    Points: 1,124
    Helping Hand
    Rank: Community Hero

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

  • #132834

    Participant
    Points: 57
    Rank: Member

    Thank you.

  • #132921

    Participant
    Points: 201
    Helping Hand
    Rank: Participant

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

    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.

  • #132930

    Participant
    Points: 1,124
    Helping Hand
    Rank: Community Hero

    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
    
  • #132951

    Participant
    Points: 201
    Helping Hand
    Rank: Participant

    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

    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...

  • #132960

    Participant
    Points: 1,124
    Helping Hand
    Rank: Community Hero

    My executions contained a slash, hence empty extension

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

  • #133058

    Participant
    Points: 201
    Helping Hand
    Rank: Participant

    My executions contained a slash, hence empty extension
    Get-Item .\d.ps1\ | fl *

    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...

  • #133068

    Participant
    Points: 201
    Helping Hand
    Rank: Participant

    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>
  • #133098

    Participant
    Points: 389
    Helping Hand
    Rank: Contributor

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

  • #133163

    Participant
    Points: 1,124
    Helping Hand
    Rank: Community Hero

    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 😜 .

You must be logged in to reply to this topic.