Improved some cd-*.ps1 scripts

This commit is contained in:
Markus Fleschutz 2025-04-23 13:22:05 +02:00
parent 257c2c3d82
commit c35fcd8ea0
8 changed files with 39 additions and 25 deletions

View File

@ -13,13 +13,16 @@
#>
try {
if ($IsLinux) {
if ($IsLinux -or $IsMacOS) {
if (-not(Test-Path "~/Desktop" -pathType container)) {
throw "No 📂Desktop folder in your home directory yet"
}
$path = Resolve-Path "~/Desktop"
} else {
$path = [Environment]::GetFolderPath('DesktopDirectory')
}
if (-not(Test-Path "$path" -pathType container)) {
throw "No desktop folder at 📂$path"
if (-not(Test-Path "$path" -pathType container)) {
throw "No desktop folder at 📂$path yet"
}
}
Set-Location "$path"
"📂$path"

View File

@ -13,13 +13,16 @@
#>
try {
if ($IsLinux) {
if ($IsLinux -or $IsMacOS) {
if (-not(Test-Path "~/Documents" -pathType container)) {
throw "No 📂Documents folder in your home directory yet"
}
$path = Resolve-Path "~/Documents"
} else {
$path = [Environment]::GetFolderPath('MyDocuments')
}
if (-not(Test-Path "$path" -pathType container)) {
throw "No documents folder at 📂$path"
if (-not(Test-Path "$path" -pathType container)) {
throw "No documents folder at 📂$path yet"
}
}
Set-Location "$path"
$files = Get-ChildItem $path -attributes !Directory

View File

@ -15,7 +15,7 @@
try {
if ($IsLinux -or $IsMacOS) {
if (-not(Test-Path "~/Downloads" -pathType container)) {
throw "No downloads folder at ~/Downloads yet"
throw "No 📂Downloads folder in your home directory yet"
}
$path = Resolve-Path "~/Downloads"
} else {

View File

@ -5,7 +5,7 @@
This PowerShell script changes the working directory to the user's Dropbox folder.
.EXAMPLE
PS> ./cd-dropbox
📂C:\Users\Markus\Dropbox
📂C:\Users\Markus\Dropbox (has 2 files and 4 subfolders)
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
@ -13,12 +13,14 @@
#>
try {
$path = Resolve-Path "~/Dropbox"
if (-not(Test-Path "$path" -pathType container)) {
throw "No Dropbox folder at 📂$path - is Dropbox installed?"
if (-not(Test-Path "~/Dropbox" -pathType container)) {
throw "No 📂Dropbox folder in your home directory - is Dropbox installed?"
}
$path = Resolve-Path "~/Dropbox"
Set-Location "$path"
"📂$path"
$files = Get-ChildItem $path -attributes !Directory
$folders = Get-ChildItem $path -attributes Directory
"📂$path entered (has $($files.Count) files and $($folders.Count) subfolders)"
exit 0 # success
} catch {
"⚠️ Error: $($Error[0])"

View File

@ -5,7 +5,7 @@
This PowerShell script changes the working directory to the /etc directory.
.EXAMPLE
PS> ./cd-etc
📂C:\Windows\System32\drivers\etc
📂C:\Windows\System32\drivers\etc (has 2 files and 3 subfolders)
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
@ -13,7 +13,7 @@
#>
try {
if ($IsLinx) {
if ($IsLinux -or $IsMacOS) {
$path = "/etc"
} else {
$path = Resolve-Path "$env:WINDIR\System32\drivers\etc"
@ -22,7 +22,9 @@ try {
throw "No /etc directory at 📂$path"
}
Set-Location "$path"
"📂$path"
$files = Get-ChildItem $path -attributes !Directory
$folders = Get-ChildItem $path -attributes Directory
"📂$path entered (has $($files.Count) files and $($folders.Count) subfolders)"
exit 0 # success
} catch {
"⚠️ Error: $($Error[0])"

View File

@ -5,7 +5,7 @@
This PowerShell script changes the working directory to the fonts folder.
.EXAMPLE
PS> ./cd-fonts
📂C:\Windows\Fonts
📂C:\Windows\Fonts (has 2 file and 3 subfolders)
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
@ -18,7 +18,9 @@ try {
throw "No fonts folder at 📂$path"
}
Set-Location "$path"
"📂$path"
$files = Get-ChildItem $path -attributes !Directory
$folders = Get-ChildItem $path -attributes Directory
"📂$path entered (has $($files.Count) files and $($folders.Count) subfolders)"
exit 0 # success
} catch {
"⚠️ Error: $($Error[0])"

View File

@ -5,7 +5,7 @@
This PowerShell script changes the working directory to the user's home directory.
.EXAMPLE
PS> ./cd-home.ps1
📂C:\Users\Markus entered (has 4 files and 7 folders)
📂C:\Users\Markus entered (has 4 files and 7 subfolders)
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
@ -13,12 +13,12 @@
#>
try {
if (-not(Test-Path "~" -pathType container)) { throw "No home directory at $path" }
$path = Resolve-Path "~"
if (-not(Test-Path "$path" -pathType container)) { throw "No home directory at $path" }
Set-Location "$path"
$files = Get-ChildItem $path -attributes !Directory
$folders = Get-ChildItem $path -attributes Directory
"📂$path entered (has $($files.Count) files and $($folders.Count) folders)"
"📂$path entered (has $($files.Count) files and $($folders.Count) subfolders)"
exit 0 # success
} catch {
"⚠️ Error: $($Error[0])"

View File

@ -5,7 +5,7 @@
This PowerShell script changes the current working directory to the logs directory.
.EXAMPLE
PS> ./cd-logs
📂/var/logs
📂/var/logs entered (has 3 files and 2 subfolders)
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
@ -13,7 +13,7 @@
#>
function GetLogsDir {
if ($IsLinux) { return "/var/logs" }
if ($IsLinux -or $IsMacOS) { return "/var/logs" }
$WinDir = [System.Environment]::GetFolderPath('Windows')
return "$WinDir\Logs"
}
@ -21,7 +21,9 @@ function GetLogsDir {
try {
$path = GetLogsDir
Set-Location "$path"
"📂$path"
$files = Get-ChildItem $path -attributes !Directory
$folders = Get-ChildItem $path -attributes Directory
"📂$path entered (has $($files.Count) files and $($folders.Count) subfolders)"
exit 0 # success
} catch {
"⚠️ Error: $($Error[0])"