Monthly Archive

Categories

Scripting Games Comments VII: File Names

In a number of the events it has been necessary to play around with file names.  I’ve seen a number of ways to get the actual file name ie. without the extension.  The easiest way I know is to use the basename property.

Get-ChildItem | where {!$_.PSisContainer}

returns a list of files – it drops the subfolders.  The file names contain the extension. This will show the relationship between the different parts of the name

Get-ChildItem | where {!$_.PSisContainer} | Format-table FullName, Name, BaseName, Extension –AutoSize

FullName                       Name                BaseName        Extension
--------                       ----                --------        ---------
C:\scripts\auto.csv            auto.csv            auto            .csv
C:\scripts\computers.txt       computers.txt       computers       .txt
C:\scripts\emptyfolders.txt    emptyfolders.txt    emptyfolders    .txt

so if you only need the name

Get-ChildItem | where {!$_.PSisContainer} | select basename

Leave a Reply