diff --git a/Public/FilesFolders/Get-FileName.ps1 b/Public/FilesFolders/Get-FileName.ps1 index 08bd752..3ae0d00 100644 --- a/Public/FilesFolders/Get-FileName.ps1 +++ b/Public/FilesFolders/Get-FileName.ps1 @@ -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'. @@ -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") } } \ No newline at end of file