Skip to content

Commit

Permalink
Enhance Get-FileName function to support custom file names and improv…
Browse files Browse the repository at this point in the history
…e documentation
  • Loading branch information
PrzemyslawKlys committed Dec 2, 2024
1 parent 310ee1a commit 52b44d6
Showing 1 changed file with 44 additions and 10 deletions.
54 changes: 44 additions & 10 deletions Public/FilesFolders/Get-FileName.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ function Get-FileName {
Generates a temporary file name with the specified extension.
.DESCRIPTION
This function generates a temporary file name based on the provided extension. It can generate a temporary file name in the system's temporary folder or just the file name itself.
This function generates a temporary file name based on the provided extension.
It can generate a temporary file name in the system's temporary folder or just the file name itself.
.PARAMETER Name
Specifies the name of the temporary file. If not specified, the function generates a random file name.
.PARAMETER Extension
Specifies the extension for the temporary file name. Default is 'tmp'.
Expand All @@ -16,33 +20,63 @@ function Get-FileName {
Indicates whether to generate only the temporary file name without the path.
.EXAMPLE
Get-FileName -Temporary
Generates a temporary file name in the system's temporary folder. Example output: 3ymsxvav.tmp
Get-FileName
Generates a temporary file name in the system's temporary folder.
Example output: C:\Users\przemyslaw.klys\AppData\Local\Temp\3ymsxvav.tmp
.EXAMPLE
Get-FileName -Temporary
Generates a temporary file name without the path. Example output: tmpD74C.tmp
Generates a temporary file name in the system's temporary folder.
Example output: C:\Users\przemyslaw.klys\AppData\Local\Temp\3ymsxvav.tmp
.EXAMPLE
Get-FileName -Temporary -Extension 'xlsx'
Generates a temporary file name with the specified extension in the system's temporary folder. Example output: tmp45B6.xlsx
Generates a temporary file name with the specified extension in the system's temporary folder.
Example output: C:\Users\przemyslaw.klys\AppData\Local\Temp\3ymsxvav.xlsx
.EXAMPLE
Get-FileName -Name 'MyFile' -Temporary
Generates a temporary file name with the specified name in the system's temporary folder.
Example output: C:\Users\przemyslaw.klys\AppData\Local\Temp\MyFile_3ymsxvav.xlsx
.EXAMPLE
Get-FileName -Extension 'xlsx' -TemporaryFileOnly
Generates a temporary file name with the specified extension without the path.
Example output: 3ymsxvav.xlsx
.NOTES
These examples demonstrate how to use the Get-FileName function to generate temporary file names.
.EXAMPLE
Get-FileName -Name 'MyFile' -TemporaryFileOnly
Generates a temporary file name with the specified name without the path.
Example output: MyFile_3ymsxvav.xlsx
.EXAMPLE
Get-FileName -Name 'MyFile' -Extension 'xlsx'
Generates a temporary file name with the specified name and extension.
Example output: C:\Users\przemyslaw.klys\AppData\Local\Temp\MyFile.xlsx
#>
[CmdletBinding()]
param(
[string] $Name,
[string] $Extension = 'tmp',
[switch] $Temporary,
[switch] $TemporaryFileOnly
)

if ($Temporary) {
if ($Name -and $Temporary) {
$NewName = "$($Name)_$($([System.IO.Path]::GetRandomFileName()).Split('.')[0]).$Extension"
return [io.path]::Combine([System.IO.Path]::GetTempPath(), $NewName)
} elseif ($Name -and $TemporaryFileOnly) {
return "$($Name)_$($([System.IO.Path]::GetRandomFileName()).Split('.')[0]).$Extension"
} elseif ($Name) {
$NewName = "$Name.$Extension"
return [io.path]::Combine([System.IO.Path]::GetTempPath(), $NewName)
} elseif ($Temporary) {
# C:\Users\przemyslaw.klys\AppData\Local\Temp\p0v4bbif.xlsx
return [io.path]::Combine([System.IO.Path]::GetTempPath(), "$($([System.IO.Path]::GetRandomFileName()).Split('.')[0]).$Extension")
}
if ($TemporaryFileOnly) {
} elseif ($TemporaryFileOnly) {
# Generates 3ymsxvav.tmp
return "$($([System.IO.Path]::GetRandomFileName()).Split('.')[0]).$Extension"
} else {
return [io.path]::Combine([System.IO.Path]::GetTempPath(), "$($([System.IO.Path]::GetRandomFileName()).Split('.')[0]).$Extension")
}
}

0 comments on commit 52b44d6

Please sign in to comment.