Click here to Skip to main content
15,867,835 members
Articles / Command line

Easier Navigation Within the Windows Command Prompt

Rate me:
Please Sign up or sign in to vote.
2.71/5 (3 votes)
6 Mar 2016CPOL7 min read 5.9K   33   1  
How to create batch files that make navigating the filesystem in the command prompt a breeze.

Introduction

The creation of these files started from the desire to be able to more easily navigate the filesystem while using the command prompt. It was frustrating to have to type long paths, repeatedly switch between viewing the list of folders in the current directory to find an unknown folder name and then using a different command to navigate into it just to start the process over. Using these commands, you will be able to change directories more easily as well as quickly save important and often used paths to a list, with each saved path accessible by index number. There's even more to gain from these commands, so let's get started.

Using the code

To use these files, either place them in your "C:\Windows" folder or add the path to the directory you put them in to your PATH environment variable. This way you will be able to use them as commands every time you open you command line. Make sure to run the command line with the /V option for the commands to function properly.

There are three files explained in this article. You may notice if examining the downloaded files that they all start with this line:

@echo off

This keeps the prompt from displaying every action taken when running a command. Although this line is present in all of the files I will only be explaining it this once.

Now let's get down to it, starting with the simplest of the batch files.

"UP" Command

This file is used in a command prompt to easily navigate—you guessed it—up.

First we check for an argument. If there is none we simply navigate up a folder and then jump to the end of the file.

if [%1]==[] cd.. & goto :eof

If there is an argument specified that is a number we set a variable to the argument's value. If the argument is not a number it defaults to 0, which means no action will be performed.

set /a num=%1

Next we check that the number is larger than zero, and if it is we use a "for" loop with the number of iterations as specified in the variable we just set. Each iteration navigates up a directory.

if /i %num% geq 0 (
    for /l %%i in (1,1,%num%) do cd..
)

"INITVARS" Command

The initvars command is what reads the list of saved paths from file and sets indexed variables for the command prompt session with the paths as values along with other necessary variables. It shouldn't have to be called manually because using the nav command runs this script automatically when needed but I will still explain how the script works.

First off, a variable needs to be set with the value being the path to the text file containing the paths you save using the nav command. You can change the default path of "savedpaths.txt" to whatever path you want the file to be stored at. The script then makes sure the file exists and if it doesn't it creates it at the specified location.

Next, we declare a variable called navindex to be used later in the script and set its value to 1. 

set list=savedpaths.txt
if not exist %list% echo.>%list%
set /a navindex=1

Lastly for this file is a "for" loop that iterates through the paths in the saved file and creates a session variable for each of them. They are kept track of by incrementing the navindex variable's value and appending the number to each path variable's name in order.

for /f "tokens=*" %%a in (%list%) do  (
    set nav!navindex!=%%a
    set /a navindex+=1
)
set /a navindex-=1

"NAV" Command

The first step in creating the navigation file is setting all the required variables that the file uses so we'll call the initvars script before doing anything else.

call initvars

Next we need to check for parameter usage with a series of "if" statements. These parameters control which function of the tool you are using by jumping to labels that are put at a specific location in the script.

if [%1]==[] goto :help
if [%1]==[/?] goto :help
if [%1]==[/v] goto :view
if [%1]==[/s] goto :save
if [%1]==[/x] goto :delete

goto :jump

Now that we have the calls for navigating to the named labels, we now need to add the label destinations and the code for each of the five major functions of the command.

We'll start with the "help" label. This section simply prints out usage information to the prompt and then jumps to the end of the script file.

:help
echo.
echo  Options
echo  ----------------------------
echo  /v : views all shortcuts
echo  /s : saves the current path to a shortcut
echo  /x : deletes a shortcut at the specified number

goto :eof

The "view" label's script is a bit more complicated, as it uses a "for" loop and also variable expansion. This loop uses the /L option which is used to iterate from a minimum number to a maximum number using the specified increment. The value of the number for each iteration is held by the %%i variable and can be accessed in the code ran for the iteration. The minimum we set here to 1 and the increment also to 1, but the maximum we set to the navindex variable which should already be set to the number of paths stored in your shortcut list.

In the iteration's code we print out the current value of %%i along with the expanded value of the variable named by the current iteration value with the prefix "nav." It then has done its job and jumps to the end of the file.

:view
for /l %%i in (1,1,!navindex!) do echo %%i: !nav%%i!

goto :eof

We now will examine the "save" option's code. This code is used to append the path of the current directory to the file at the path we set in the initvars file. It then calls the file to clear out and redeclare all necessary variables, taking into account the newly added path value. Like before, job's done so it skips to the file end.

:save
echo.%cd%>>%list%

call initvars

goto :eof

The next command option we will look at is located under the "delete" label. This option can accept a parameter which will specify the index of the path to delete. How it functions is (with saved paths stored as variables) it clears the file where the paths are stored and then iterate through the paths stored in memory and checks if its index matches the one specified as a parameter. If it does not match it proceeds to add the path back to the save file. If it does match it skips the addition of that path to the file. Once all indeces have been checked it clears out the paths stored in memory and reinitializes them then jumps to the end of the file.

:delete
echo.>%list%
for /l %%i in (1,1,%navindex%) do (
    if not [%%i]==[%2] (
        if defined nav%2 ( echo !nav%%i!>>!list! )
    )
)
call initvars

goto :eof

To jump to a specific saved path we need to write the code under the "jump" label. This code runs a "for" loop that iterates through 50 numbers (can be increased if you have a need for more paths than that.) With each number it appends the standard prefix and checks if the variable is defined. If so, it navigates to the path stored with that variable name and jumps to the end of the file. This code only performs an action if the argument specified is a number. If the argument doesn't match any of the numbers it falls through to the next label to perform further checks on the specified arguments.

:jump
for /l %%i in (1,1,50) do (
    if [%%i]==[%1] (
        if defined nav%1 cd /d !nav%1!
        goto :eof
    )
)

The last option for the nav command is the "follow" label. This is the action taken when none of the other options are specified but the command still has arguments. It is possibly the most confusing script of all of them in appearance but the functionality is quite simple. It allows you to specify one or more folders to navigate into separated by spaces instead of slashes. Some may not find this feature helpful but personally I find it an easier way to specify directories.

The code for this option first unsets a variable called "loc" to ensure it is working with a clean slate. This variable is then appended with each of the folder names you specified and adds the slashes for you where appropriate. This happens inside of yet another "for" loop that uses the %* variable which contains all arguments passed to the command. If you need to specify a directory name that contains spaces, wrap the name with quotes and the code will remove them when necessary.

Once the path is assembled the script attempts to navigate to the now full path. It also automatically specifies the option when executing that makes navigating to other drives possible so that's one less thing to bother with when moving around the filesystem.

:follow
set loc=
for %%a in (%*) do (
    set tmp=%%a
    set loc=!loc!!tmp:"=!/
)

cd /d "%loc%"

That's it for the internals of the commands, now here are some examples of how to use them to simplify navigating in a command line. Make sure to download the full collection of batch files.

Image 1

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer JBHworks
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --