Created
February 11, 2021 09:47
-
-
Save sheepla/459ebf6895399ce1089426db6ce16207 to your computer and use it in GitHub Desktop.
PowerShellでdirname/basename
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function dirname | |
{ | |
Param | |
( | |
[Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Position=1, | |
HelpMessage="Files or directories for which you want to get the parent directory name")] | |
[String[]] $Path | |
) | |
foreach ($item in (Get-Item $Path)) { | |
if ($item.PSIsContainer) { | |
$item.FullName | |
} else { | |
[IO.Path]::GetDirectoryName($item.FullName) | |
} | |
} | |
} | |
function basename | |
{ | |
Param | |
( | |
[Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Position=1, | |
HelpMessage="Files or directories for which you want to get basename")] | |
[String[]] $Path, | |
[Parameter(Mandatory=$false, ValueFromPipeline=$false, ValueFromPipelineByPropertyName=$false)] | |
[Switch] $WithinExtension | |
) | |
foreach ($item in (Get-Item $Path)) { | |
if ($item.PSIsContainer) { | |
Write-Output "" | |
} else { | |
if ($WithinExtension) { | |
[IO.Path]::GetFileName($item.FullName) | |
} else { | |
[IO.Path]::GetFileNameWithoutExtension($item.FullName) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment