Add GetTempDir function

This commit is contained in:
Markus Fleschutz 2021-12-09 16:36:44 +01:00
parent 2881994f5e
commit 41f8c9d5cd
4 changed files with 25 additions and 5 deletions

View File

@ -12,8 +12,13 @@
Author: Markus Fleschutz · License: CC0 Author: Markus Fleschutz · License: CC0
#> #>
function GetTempDir {
if ($IsLinux) { return "/tmp" }
return "$env:TEMP"
}
try { try {
$TargetDir = resolve-path "$env:TEMP" $TargetDir = resolve-path GetTempDir
if (-not(test-path "$TargetDir" -pathType container)) { if (-not(test-path "$TargetDir" -pathType container)) {
throw "Temporary folder at 📂$TargetDir doesn't exist (yet)" throw "Temporary folder at 📂$TargetDir doesn't exist (yet)"
} }

View File

@ -15,6 +15,11 @@
param([string]$text = "") param([string]$text = "")
function GetTempDir {
if ($IsLinux) { return "/tmp" }
return "$env:TEMP"
}
try { try {
# print reply on the console: # print reply on the console:
"$text" "$text"
@ -29,7 +34,7 @@ try {
} }
# remember last reply: # remember last reply:
"$text" > "$env:TEMP/last_reply.txt" "$text" > "$(GetTempDir)/last_reply_given.txt"
exit 0 # success exit 0 # success
} catch { } catch {
"⚠️ Error: $($Error[0]) ($($MyInvocation.MyCommand.Name):$($_.InvocationInfo.ScriptLineNumber))" "⚠️ Error: $($Error[0]) ($($MyInvocation.MyCommand.Name):$($_.InvocationInfo.ScriptLineNumber))"

View File

@ -11,8 +11,13 @@
https://github.com/fleschutz/PowerShell https://github.com/fleschutz/PowerShell
#> #>
function GetTempDir {
if ($IsLinux) { return "/tmp" }
return "$env:TEMP"
}
try { try {
$TargetDir = resolve-path "$env:TEMP" $TargetDir = resolve-path GetTempDir
if (-not(test-path "$TargetDir" -pathType container)) { if (-not(test-path "$TargetDir" -pathType container)) {
throw "Temporary folder at 📂$TargetDir doesn't exist (yet)" throw "Temporary folder at 📂$TargetDir doesn't exist (yet)"
} }

View File

@ -11,9 +11,14 @@
https://github.com/fleschutz/PowerShell https://github.com/fleschutz/PowerShell
#> #>
if (test-path "$env:TEMP/last_reply.txt" -pathType leaf) { function GetTempDir {
if ($IsLinux) { return "/tmp" }
return "$env:TEMP"
}
if (test-path "$(GetTempDir)/last_reply_given.txt" -pathType leaf) {
$Reply = "It was: " $Reply = "It was: "
$Reply += Get-Content "$env:TEMP/.last_reply.txt" $Reply += Get-Content "$(GetTempDir)/last_reply_given.txt"
} else { } else {
$Reply = "Sorry, I can't remember." $Reply = "Sorry, I can't remember."
} }