Skip to content

Instantly share code, notes, and snippets.

@sheepla
Created February 11, 2021 09:47
Show Gist options
  • Save sheepla/459ebf6895399ce1089426db6ce16207 to your computer and use it in GitHub Desktop.
Save sheepla/459ebf6895399ce1089426db6ce16207 to your computer and use it in GitHub Desktop.
PowerShellでdirname/basename
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