Home » Questions » Computers [ Ask a new question ]

How to change dir quickly and effectively in Windows command line?

How to change dir quickly and effectively in Windows command line?

For example, I am in C:\test1, and I have another directory C:\test2\sub2

Asked by: Guest | Views: 372
Total answers/comments: 4
bert [Entry]

"You actually can't do that. It is kind of impossible, from a technical aspect.

Imagine you had a folder structure like this:

School Work/
English/
Assignments/
Class Work/
Documents/

Science/
Assignments/
Class Work/
Documents/

Math/
Assignments/
Class Work/
Documents/

Now, If you ran quickchange Documents from within the School Work folder, what folder would you expect to go to? There are three matches, and no way of knowing which one you actually want to get to. Sure, it might be able to prompt you as to which folder you are after, but that does not scale very well

Imagine this same folder structure for every student in a school, on a server, so it looks something like this:

Josh Hunt\
School Work\
[etc]

Cathy Wells\
School Work\
[etc]

Jack Thompson\
School Work\
[etc]

James Smith\
School Work\
[etc]

[repeat for all 1300 students]

Clearly, this does not scale well. There are now 3900 Documents folders.

The only possible way to make navigating folders easier is using Tab Completion. You would use it like this:

/Schoolwork/$ dir E<tab>[nglish/]A<tab>[ssignments/]

Just to clarify, <tab> indicates you hitting the tab button on the keyboard, whereas [nglish/] indicates what tab completion has filled in for you."
bert [Entry]

"Sorry for the late answer, it took me a couple years to find this question.

You can use a batch script that does this, then refer to that batch script either with a DOSKEY macro or put it in your path.

I wrote this script, it has the following use cases:

If the directory exists without looking in subdirectories, switch to it.
If one match is found, switch to it.
If multiple matches are found, list them and prompt for a choice.
If no match is found, display ""Directory not found"".
If no argument is given, list all subdirectories and prompt for a choice.

Here's my script:

@echo off

setlocal enabledelayedexpansion

set target_directory=%1
set /a prompt_index=0
if exist %target_directory% (
set chosen_path=%target_directory%
goto :path_set
)
for /f ""tokens=*"" %%A IN ('dir /s /b /ad %1 2^>nul') do (
SET /a prompt_index+=1
set dir_!prompt_index!=%%A
)
if %prompt_index%==0 (
echo Directory not found
) else if %prompt_index%==1 (
set chosen_index=1
) else if %prompt_index% GTR 1 (
for /l %%i in (1, 1, %prompt_index%) do (
echo %%i. !dir_%%i!
)
echo.
set /p chosen_index=""Choose a directory: ""
)
set chosen_path=!dir_%chosen_index%!
:path_set
endlocal & set chosen_path=%chosen_path%
if defined chosen_path (
pushd %chosen_path%
set chosen_path=
)"
"Sorry for the late answer, it took me a couple years to find this question.

You can use a batch script that does this, then refer to that batch script either with a DOSKEY macro or put it in your path.

I wrote this script, it has the following use cases:

If the directory exists without looking in subdirectories, switch to it.
If one match is found, switch to it.
If multiple matches are found, list them and prompt for a choice.
If no match is found, display ""Directory not found"".
If no argument is given, list all subdirectories and prompt for a choice.

Here's my script:

@echo off

setlocal enabledelayedexpansion

set target_directory=%1
set /a prompt_index=0
if exist %target_directory% (
set chosen_path=%target_directory%
goto :path_set
)
for /f ""tokens=*"" %%A IN ('dir /s /b /ad %1 2^>nul') do (
SET /a prompt_index+=1
set dir_!prompt_index!=%%A
)
if %prompt_index%==0 (
echo Directory not found
) else if %prompt_index%==1 (
set chosen_index=1
) else if %prompt_index% GTR 1 (
for /l %%i in (1, 1, %prompt_index%) do (
echo %%i. !dir_%%i!
)
echo.
set /p chosen_index=""Choose a directory: ""
)
set chosen_path=!dir_%chosen_index%!
:path_set
endlocal & set chosen_path=%chosen_path%
if defined chosen_path (
pushd %chosen_path%
set chosen_path=
)"
bert [Entry]

Tab autocompletion of the path after cd isn't fast enough? Usually it only takes me 5 or 6 keystrokes to get where i need to go that way.
bert [Entry]

"Use pushd and popd.

C:\Documents and Settings\~quack> pushd d:\foo

D:\foo> pushd e:\bar

E:\bar> popd

D:\foo> popd

C:\Documents and Settings\~quack>

pushd pushes your current directory onto a stack, then cd's to the argument directory. The stack can be as large as you want. popd removes the directory on the top of the stack and cd's to it."