Skip to content

Commit 9d1bc2c

Browse files
committed
🎨 Convert many System.IO.Path features to Split-Path
1 parent 21282b8 commit 9d1bc2c

20 files changed

+80
-97
lines changed

‎Backup-File.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Backup-File.ps1 logfile.log
3333
Copies logfile.log to logfile-201612311159.log (on that date & time).
3434
#>
3535

36+
#Requires -Version 7
3637
[CmdletBinding()][OutputType([void])] Param(
3738
<#
3839
Specifies a path to the items being removed. Wildcards are permitted.
@@ -43,7 +44,6 @@ The parameter name ("-Path") is optional.
4344
)
4445
Process
4546
{
46-
$name = Split-Path $Path -Leaf
47-
$name = "$([IO.Path]::GetFileNameWithoutExtension($name))-$(Get-Date -Format yyyyMMddHHmmss)$([IO.Path]::GetExtension($name))"
47+
$name = "$(Split-Path $Path -LeafBase)-$(Get-Date -Format yyyyMMddHHmmss)$(Split-Path $Path -Extension)"
4848
Copy-Item $Path (Resolve-Path $Path |Split-Path |Join-Path -ChildPath $name)
4949
}

‎Backup-SchTasks.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Saves scheduled tasks to tasks-backup.xml using tasks.css as a display styleshee
3939
'<?xml version="1.0"?>' |Out-File $Path -Encoding utf8
4040
if($Stylesheet)
4141
{
42-
"<?xml-stylesheet href=`"$Stylesheet`" type=`"text/$([io.path]::GetExtension($Stylesheet).Trim('.'))`"?>" |
42+
"<?xml-stylesheet href=`"$Stylesheet`" type=`"text/$((Split-Path $Stylesheet -Extension).Trim('.'))`"?>" |
4343
Out-File $Path -Encoding utf8 -Append
4444
}
4545
schtasks /query /xml |

‎Convert-Xml.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Process
7777
}
7878
else
7979
{
80-
$absOutFile = [IO.Path]::Combine($PWD,$OutFile)
80+
$absOutFile = Join-Path "$PWD" $OutFile
8181
if((Test-Path $absOutFile) -and
8282
!$PSCmdlet.ShouldContinue("$(Get-Item $absOutFile |Select-Object FullName,LastWriteTime,Length)","Overwrite File?"))
8383
{Write-Warning "Skipping transform from $absPath to $OutFile"; return}

‎ConvertTo-SafeEntities.ps1

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,4 @@ the 7-bit ASCII range.
3939
#>
4040
[Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true)][string] $InputObject
4141
)
42-
Process
43-
{
44-
[char[]] $chars =
45-
for ($i = 0; $i -lt $InputObject.Length; $i++)
46-
{
47-
[int] $c = [char]$InputObject[$i]
48-
Write-Verbose "$i : $c"
49-
if([char]::IsSurrogatePair($InputObject,$i))
50-
{ ('&#x{0:X};' -f [char]::ConvertToUtf32($InputObject,$i++)).GetEnumerator() }
51-
elseif(0x7F -lt $c)
52-
{ ('&#x{0:X};' -f $c).GetEnumerator() }
53-
else
54-
{ [char]$c }
55-
}
56-
New-Object String $chars,0,$chars.Length
57-
}
42+
Process {return [Text.Encodings.Web.HtmlEncoder]::Default.Encode($InputObject)}

‎Export-Readme.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Generate README.md file for the scripts repo.
66
https://www.microsoft.com/download/details.aspx?id=40760
77
#>
88

9-
#Requires -Version 3
9+
#Requires -Version 7
1010
#Requires -Modules SqlServer
1111
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns','',
1212
Justification='This script deals with lists, and this is a pretty questionable rule.')]
@@ -60,7 +60,7 @@ function Export-Dependencies($image)
6060
Write-Progress 'Creating dependency image'
6161
$gv = Join-Path $PSScriptRoot ([IO.Path]::ChangeExtension($image,'gv'))
6262
Format-Dependencies |Out-File $gv -Encoding ascii -Width ([int]::MaxValue)
63-
$ext = [IO.Path]::GetExtension($image).Trim('.')
63+
$ext = (Split-Path $image -Extension).Trim('.')
6464
dot "-T$ext" -o $PSScriptRoot\$image $gv
6565
Remove-Item $gv
6666
Write-Progress 'Creating dependency image' -Completed
@@ -448,7 +448,7 @@ function Export-PSScriptPages
448448
Import-Module platyPS
449449
Write-Progress 'Export PowerShell script help pages' 'Removing docs for old scripts'
450450
Get-Item $PSScriptRoot\docs\*.ps1.md |
451-
Where-Object {!(Test-Path "$PSScriptRoot\$([IO.Path]::GetFileNameWithoutExtension($_.Name))" -Type Leaf)} |
451+
Where-Object {!(Test-Path "$PSScriptRoot\$(Split-Path $_.Name -LeafBase)" -Type Leaf)} |
452452
Remove-Item
453453
Write-Progress 'Export PowerShell script help pages' 'Updating script docs'
454454
Update-MarkdownHelp $PSScriptRoot\docs\*.ps1.md -ErrorAction Ignore |Write-Verbose

‎Find-Lines.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function ConvertTo-DateTimeOffset([Parameter(Position=0)][int] $UnixTime,[Parame
6969
$culturetextinfo = (Get-Culture).TextInfo
7070
function Get-LineBlameInfo([Parameter(Position=0)][string] $Path,[Parameter(Position=1)][int] $LineNumber)
7171
{
72-
Push-Location "$([IO.Path]::GetDirectoryName($Path))"
72+
Split-Path $Path |Push-Location
7373
$lineinfo = [Collections.Generic.List[string]]@(git blame -l -p -L "$LineNumber,$LineNumber" -- $Path)
7474
Pop-Location
7575
($sha1,$linetext) = ($lineinfo[0],$lineinfo[$lineinfo.Count -1])

‎Get-CommandPath.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Process
4646
if($FindAllInPath)
4747
{
4848
$files =
49-
if([io.path]::GetExtension($ApplicationName)) {$ApplicationName}
49+
if((Split-Path $ApplicationName -Extension)) {$ApplicationName}
5050
else {$env:PATHEXT.ToLower() -split ';' |ForEach-Object {[io.path]::ChangeExtension($ApplicationName,$_)}}
5151
Write-Verbose "Searching Path for $($files -join ', ')"
5252
foreach($p in $env:Path -split ';')

‎Get-NuGetConfigs.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ C:\ProgramData\NuGet\NuGetDefaults.config
2828

2929
function Get-Parent([Parameter(Position=0)][string] $Directory)
3030
{
31-
if($Directory -eq [io.path]::GetPathRoot($Directory)) {$Directory}
31+
if($Directory -eq "$(Join-Path (Split-Path $Directory -Qualifier) '')") {$Directory}
3232
else {$Directory; Get-Parent (Split-Path $Directory)}
3333
}
3434

‎Get-RepoName.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Process
3131
if(!$?) {Stop-ThrowError.ps1 "The path $Path is not a git repo."-Argument Path}
3232
$remote = git remote |Select-Object -First 1
3333
if($remote) {return ([uri](git remote get-url $remote)).Segments[-1] -replace '\.git\z',''}
34-
else {return [io.path]::GetFileName($Path)}
34+
else {return Split-Path $Path -Leaf}
3535
}
3636
finally {Pop-Location}
3737
}

‎Import-VsCodeDatabaseConnections.ps1

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ Add-VsCodeDatabaseConnection.ps1
2020
.LINK
2121
Get-ConfigConnectionStringBuilders.ps1
2222
23-
.LINK
24-
Split-FileName.ps1
25-
2623
.LINK
2724
Import-Variables.ps1
2825
@@ -45,7 +42,7 @@ function Get-ConfigConnections
4542
{
4643
foreach($cs in (Get-ConfigConnectionStringBuilders.ps1 $config))
4744
{
48-
$name = $cs.Name + '.' + ($config |Split-FileName.ps1 |Split-FileName.ps1 -Extension).Trim('.')
45+
$name = $cs.Name + '.' + ($config |Split-Path -LeafBase |Split-Path -Extension).Trim('.')
4946
if($connections -and $name -in $connections.profileName){Write-Verbose "A '$name' connection already exists."; continue}
5047
Import-Variables.ps1 $cs.ConnectionString
5148
$vsconn = @{

‎Join-FileName.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ Process
4242
if($AppendText -like '.*') { [IO.Path]::ChangeExtension($Path,$AppendText) }
4343
else
4444
{
45-
$name = [IO.Path]::GetFileNameWithoutExtension($Path) + $AppendText;
46-
if($AppendText -notlike '*.*'){$name+=[IO.Path]::GetExtension($Path)}
45+
$name = (Split-Path $Path -LeafBase) + $AppendText
46+
if($AppendText -notlike '*.*'){ $name += Split-Path $Path -Extension }
4747
if(Split-Path $Path) { Join-Path (Split-Path $Path) $name }
4848
else { $name }
4949
}

‎New-PesterTests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ $('"@')
9292
[Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)][string] $Synopsis,
9393
[Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)][psobject[]] $Examples
9494
)
95-
$shortname = [io.path]::GetFileNameWithoutExtension($Name)
95+
$shortname = Split-Path $Name -LeafBase
9696
$Local:OFS = $NL
9797
return @"
9898
<#

‎New-Shortcut.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ $sh = New-Object -ComObject WScript.Shell
4747
$folder = Split-Path ([IO.Path]::GetFullPath($Path))
4848
# the old WshShortcut COM component doesn't support Unicode
4949
$file = [Net.WebUtility]::UrlEncode((Split-Path $Path -Leaf))
50-
if(![IO.Path]::HasExtension($file)) {$file += if([uri]::IsWellFormedUriString($TargetPath,'Absolute')){'.url'}else{'.lnk'}}
50+
if(!(Split-Path $file -Extension)) {$file += if([uri]::IsWellFormedUriString($TargetPath,'Absolute')){'.url'}else{'.lnk'}}
5151
$fullname = Join-Path $folder $file
5252
$lnk = $sh.CreateShortcut($fullname)
5353
$lnk.TargetPath = $TargetPath

‎PS5/Convert-ScheduledTasksToJobs.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,8 @@ $CommandParam = @'
250250
$workdir,$filepath = $a.WorkingDirectory,$Matches.FilePath.Trim('"')
251251
Write-Verbose "Converting '$($Task.TaskName)' as a PowerShell file task ($filePath; $workdir)."
252252
$file =
253-
if([io.path]::IsPathRooted($filepath)) {$filepath}
254-
else { [io.path]::Combine($workdir,$filepath) }
253+
if([io.path]::IsPathRooted($filepath)) { $filepath }
254+
else { Join-Path $workdir $filepath }
255255
Convert-ScheduledTaskToJob $Task $file $Matches.Params
256256
}
257257
elseif($a.Arguments -match $CommandParam)
Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,55 @@
1-
<#
2-
.SYNOPSIS
3-
Returns the specified part of the filename.
4-
5-
.DESCRIPTION
6-
Split-FileName returns only the specified part of a filename:
7-
either the filename without extension (default) or extension.
8-
It can also tell whether the filename has an extension.
9-
10-
.INPUTS
11-
System.String file path to parse.
12-
13-
.OUTPUTS
14-
System.String containing the base file name (or extension),
15-
or System.Boolean if the -HasAttribute switch is present.
16-
17-
.FUNCTIONALITY
18-
Files
19-
20-
.EXAMPLE
21-
Split-FileName.ps1 readme.txt
22-
23-
readme
24-
25-
.EXAMPLE
26-
Split-FileName.ps1 readme.txt -Extension
27-
28-
․txt
29-
(the leading . is included, but can't be entered as such in this example)
30-
31-
.EXAMPLE
32-
Split-FileName.ps1 readme.txt -HasExtension
33-
34-
True
35-
#>
36-
37-
[CmdletBinding(DefaultParameterSetName='__AllParameterSets')]
38-
[OutputType([string],ParameterSetName='__AllParameterSets')]
39-
[OutputType([bool],ParameterSetName='HasExtension')] Param(
40-
# A file path to extract a part of; the base name without extension by default.
41-
[Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true)][string] $Path,
42-
<#
43-
Indicates that the path should be checked for the presence of an extension,
44-
returning a boolean value.
45-
#>
46-
[Parameter(ParameterSetName='HasExtension')][switch] $HasExtension,
47-
# Indicates the path's extension should be returned.
48-
[Parameter(ParameterSetName='Extension')][switch] $Extension
49-
)
50-
Process
51-
{
52-
if($HasExtension) { [IO.Path]::HasExtension($Path) }
53-
elseif($Extension) { [IO.Path]::GetExtension($Path) }
54-
else { [IO.Path]::GetFileNameWithoutExtension($Path) }
55-
}
1+
<#
2+
.SYNOPSIS
3+
Returns the specified part of the filename.
4+
5+
.DESCRIPTION
6+
Split-FileName returns only the specified part of a filename:
7+
either the filename without extension (default) or extension.
8+
It can also tell whether the filename has an extension.
9+
10+
.INPUTS
11+
System.String file path to parse.
12+
13+
.OUTPUTS
14+
System.String containing the base file name (or extension),
15+
or System.Boolean if the -HasAttribute switch is present.
16+
17+
.FUNCTIONALITY
18+
Files
19+
20+
.EXAMPLE
21+
Split-FileName.ps1 readme.txt
22+
23+
readme
24+
25+
.EXAMPLE
26+
Split-FileName.ps1 readme.txt -Extension
27+
28+
․txt
29+
(the leading . is included, but can't be entered as such in this example)
30+
31+
.EXAMPLE
32+
Split-FileName.ps1 readme.txt -HasExtension
33+
34+
True
35+
#>
36+
37+
[CmdletBinding(DefaultParameterSetName='__AllParameterSets')]
38+
[OutputType([string],ParameterSetName='__AllParameterSets')]
39+
[OutputType([bool],ParameterSetName='HasExtension')] Param(
40+
# A file path to extract a part of; the base name without extension by default.
41+
[Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true)][string] $Path,
42+
<#
43+
Indicates that the path should be checked for the presence of an extension,
44+
returning a boolean value.
45+
#>
46+
[Parameter(ParameterSetName='HasExtension')][switch] $HasExtension,
47+
# Indicates the path's extension should be returned.
48+
[Parameter(ParameterSetName='Extension')][switch] $Extension
49+
)
50+
Process
51+
{
52+
if($HasExtension) { [IO.Path]::HasExtension($Path) }
53+
elseif($Extension) { [IO.Path]::GetExtension($Path) }
54+
else { [IO.Path]::GetFileNameWithoutExtension($Path) }
55+
}

‎Rename-Script.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Write-Progress "Renaming $OldName to $NewName" -Completed
4242
Write-Progress "Updating uses of $OldName to $NewName" 'Finding uses'
4343
$scope = $ScriptDirectory |ForEach-Object {"$_\*.ps1"}
4444
[Microsoft.PowerShell.Commands.MatchInfo[]] $uses =
45-
@(Select-String "\b$([regex]::Escape([io.path]::GetFileNameWithoutExtension($OldName)))(?:\.ps1)?\b" $scope -List)
45+
@(Select-String "\b$([regex]::Escape((Split-Path $OldName -LeafBase)))(?:\.ps1)?\b" $scope -List)
4646
if($uses.Length -eq 0)
4747
{
4848
Write-Warning "No uses of $OldName found!"

‎Save-PodcastEpisodes.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Process
5454
{
5555
$filename = if($episode.PSObject.Properties.Match('episode')) {$episode.episode + ' '} else {''}
5656
$filename += $title -replace $invalidchars,'_'
57-
$filename += [io.path]::GetExtension($episode.enclosure.url)
57+
$filename += Split-Uri.ps1 $episode.enclosure.url -Extension
5858
Invoke-WebRequest $episode.enclosure.url -OutFile $filename
5959
(Get-Item $filename).CreationTime = $episode.published
6060
}

‎Save-WebRequest.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ Saves f1040.pdf (or else a filename specified in the Content-Disposition header)
3737

3838
[CmdletBinding()][OutputType([void])] Param(
3939
# The URL to download.
40-
[Parameter(Position=0,Mandatory=$true,ValueFromPipelineByPropertyName=$true)][Alias('Url')][uri] $Uri,
40+
[Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
41+
[Alias('Url','Href','Src')][uri] $Uri,
4142
# Sets the creation time on the file to the given value.
4243
[datetime] $CreationTime,
4344
# Sets the creation time on the file to the given value.

‎Update-Files.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Process
2828
{
2929
foreach($file in Resolve-Path $Path)
3030
{
31-
$destfile = Join-Path $Destination ([IO.Path]::GetFileName($file))
31+
$destfile = Join-Path $Destination (Split-Path $file -Leaf)
3232
if(!(Test-Path $destfile -PathType Leaf)) {continue}
3333
if((!$NewerOnly -or (Test-NewerFile.ps1 "$destfile" "$file")) -and
3434
$PSCmdlet.ShouldProcess("'$file' over '$destfile'",'copy'))

‎Use-Command.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ switch($PSCmdlet.ParameterSetName)
259259
$dir = Split-Path $Path
260260
if($PSCmdlet.ShouldProcess("$DownloadZip to $dir",'download/unzip'))
261261
{
262-
$filename = [IO.Path]::GetFileName($DownloadZip.LocalPath)
262+
$filename = Split-Uri.ps1 $DownloadZip.LocalPath -Leaf
263263
if (!(Test-Path $dir -PathType Container)) { mkdir $dir |Out-Null }
264264
$zippath = Join-Path $env:TEMP $filename
265265
Write-Verbose "Downloading $DownloadZip to $path"

0 commit comments

Comments
 (0)