-
Notifications
You must be signed in to change notification settings - Fork 0
/
ccd.ps1
97 lines (80 loc) · 1.96 KB
/
ccd.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<#PSScriptInfo
.VERSION 2024.06.09
.AUTHOR Levente Rog
.COPYRIGHT (c) 2024 Levente Rog
.LICENSEURI https://github.com/levid0s/useful/blob/master/LICENSE.md
.PROJECTURI https://github.com/levid0s/useful/
#>
<#
.SYNOPSIS
Open a folder browser dialog and changes the current directory to the selected folder.
.EXAMPLE
ccd
# do stuff...
popd
.EXAMPLE
# Specify starting directory
ccd d:\
"d:\" | ccd
# To return the selected path as a string instead of changing the directory:
$selectedPath = ccd -PassThru
$selectedPath = get-item c:\ | ccd -PassThru
#>
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline = $true)][string]$Path,
[switch]$PassThru,
[switch]$FileBrowser,
[switch]$Relative
)
if (!$Path) {
$Path = $pwd.Path
if ($FileBrowser) {
$IsDir = Test-Path -Path $path -PathType Container
if ($IsDir) {
$Path = $Path + "\."
}
}
}
[System.Reflection.Assembly]::LoadWithPartialName('System.windows.forms') | Out-Null
if (!$FileBrowser) {
$dialog = New-Object System.Windows.Forms.FolderBrowserDialog
$dialog.Description = 'Select a folder'
$dialog.rootfolder = 'MyComputer'
$dialog.SelectedPath = $Path
}
else {
$dialog = New-Object System.Windows.Forms.OpenFileDialog
$dialog.Title = 'Select a File or Folder'
$dialog.InitialDirectory = $Path
$dialog.Filter = 'All files (*.*)|*.*'
$dialog.CheckFileExists = $false
$dialog.CheckPathExists = $true
$dialog.DereferenceLinks = $true
$dialog.ValidateNames = $false
$dialog.ShowReadOnly = $true
$dialog.FileName = $Path
}
$response = $dialog.ShowDialog()
if ($response -ne 'OK') {
return
}
if (!$FileBrowser) {
$result = $dialog.SelectedPath
}
else {
$result = $dialog.FileName
}
if ($Relative) {
$result = Resolve-Path -Path $result -Relative
}
if ($PassThru) {
return $result
}
if (!$FileBrowser) {
Push-Location $result
}
else {
Write-Host "Opening: $result"
& $result
}