upcnomis This blog is deprecated. Please check out my new blog at http://blog.simoncpu.com.
Entries "With Logic and Chaos":

Friday, February 4, 2005

jumping back to the original directory

In the previous post, a method was proposed for easily jumping back to any directory up in the hierarchy.  However, if you need to jump back to your original directory, you may need to navigate back to it again.

A solution to this problem is to use the pushd command.  As its name suggests, pushd stores your current directory in a stack (a structure that stores data in a last-in-first-out manner), then changes to the directory you specify.  Use popd to change back to the directory last stored by pushd.

To apply this concept to our batch script, observe the following:


C:\>cd WINDOWS\SYSTEM32\DirectX

C:\WINDOWS\SYSTEM32\DirectX>cd Dinput

C:\WINDOWS\SYSTEM32\DirectX\Dinput>pushd .

C:\WINDOWS\SYSTEM32\DirectX\Dinput>back 2

Traversing 2 level(s) up...

C:\WINDOWS\SYSTEM32>popd

C:\WINDOWS\SYSTEM32\DirectX\Dinput>


In line 3, we entered "pushd ." to store the current directory to the stack.  Note that "." refers to the current directory, and ".." refers to the parent.  For more information in using the pushd and popd commands, type "pushd /?" and "popd /?" in the Command Prompt.

»Feb 4, 2005, 10:36:01 AM    »No comments     »Send entry    

Posted by: simoncpu
Modified on February 4, 2005 at 10:42 AM
Wednesday, February 2, 2005

traversing back to the nth level in the directory hierarchy

Problem: Using Command Prompt, you often need to navigate deep down to the lowest nodes such as C:\a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\z\aa.  However, you need to traverse back to the nth level without further ado.

Solution: Create a batch script named "back.cmd" containing the code below.  The script works by repeatedly invoking the "cd .." command in the number of times you have specified.

For instance, in order to navigate to directory i, just type "back 10."  It is recommended that you save your scripts in one folder, and append its path to the PATH environmental variable.

@echo off

:: by simon cornelius p. umacob <http://blog.simoncpu.com>
:: Feb 02, 2005

if "%1" == "" goto noargs
if "%1" == "/?" goto noargs

echo.
echo Traversing %1 level(s) up...
for /l %%i in (1, 1, %1) do cd ..
goto end

:noargs
echo Traverses back to the nth level in the directory hierarchy.
echo.
echo To use the %0 comand, specify the number of levels
echo that you want to traverse back in the directory.
echo.
echo e.g.:
echo    Suppose you are at directory C:\a\b\c\d\e\f\g.
echo    If you want to jump to directory b, just type:
echo            %0 5
echo.

:end

»Feb 2, 2005, 11:46:14 AM    »No comments     »Send entry    

Posted by: simoncpu
Modified on February 4, 2005 at 10:33 AM