-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathChange-Directory.ps1
More file actions
114 lines (94 loc) · 2.8 KB
/
Change-Directory.ps1
File metadata and controls
114 lines (94 loc) · 2.8 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
function Internal-Change-Directory($cmd, $ShowCount){
if(!$ShowCount) {
$ShowCount = 10;
}
if(!$global:CD_COMMAND_HISTORY_LIST) {
$global:CD_COMMAND_HISTORY_LIST = New-Object 'System.Collections.Generic.List[string]'
}
function Set-CDLocation($newLocation) {
$newLocation = get-item $newLocation;
$global:CD_COMMAND_HISTORY_LIST.Add($newLocation)
Push-Location $newLocation
}
function Get-CommandList() {
$global:CD_COMMAND_HISTORY_LIST | sort -Unique
}
function Print-Extended-CD-Menu() {
$index = 1;
foreach($location in Get-CommandList){
Write-Host ("{0,6}) {1}" -f $index, $location)
$index++
if($index -gt $ShowCount){
break;
}
}
}
switch($cmd) {
"" { Print-Extended-CD-Menu }
"?" { Print-Extended-CD-Menu }
"--help" { Print-Extended-CD-Menu }
"-" {
if($global:CD_COMMAND_HISTORY_LIST.Count -ge 2) {
Set-CDLocation ($global:CD_COMMAND_HISTORY_LIST[$global:CD_COMMAND_HISTORY_LIST.Count-2])
}
}
default {
$newLocation = $cmd;
# Turn "cd ...." into "cd ../../../"
if ($newLocation -match "^\.*$" -and $newLocation.length -gt 2) {
$numberToChange = $newLocation.length - 1
$newLocation = ""
for($i = 0; $i -lt $numberToChange; $i++) {
$newLocation += "../"
}
}
# check to see if we're using a number command and get the correct directory.
[int]$cdIndex = 0;
if([system.int32]::TryParse($cmd, [ref]$cdIndex)) {
# Don't pull from the history if the index value is the same as a folder name
if( !(test-path $cdIndex) ) {
$results = (Get-CommandList);
if( ($results | measure).Count -eq 1 ){
$newLocation = $results
}
else {
$newLocation = $results[$cdIndex-1]
}
}
}
#If we are actually changing the dir.
if($pwd.Path -ne $newLocation){
# if the path exists
if( test-path $newLocation ){
# if it's a file - get the file's directory.
if( !(Get-Item $newLocation -Force).PSIsContainer ) {
$newLocation = (split-path $newLocation)
}
Set-CDLocation $newLocation
}
else {
if($force) {
$prompt = 'y'
}
else {
$prompt = Read-Host "Folder not found. Create it? [y/n]"
}
if($prompt -eq 'y' -or $prompt -eq 'yes') {
mkdir $newLocation
Set-CDLocation $newLocation
}
}
}
}
}
}
Internal-Change-Directory -cmd $cmd -ShowCount $ShowCount
# allow typing `..` to go up 1 dir or `....` to go up 3 dirs
function ..() { cd .. }
function ...() { cd ... }
function ....() { cd .... }
function .....() { cd ..... }
function ......() { cd ...... }
function .......() { cd ....... }
function ........() { cd ........ }
Set-Alias -Name cd -Value Internal-Change-Directory -Option AllScope