Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


January 29, 2002

JSI Tip 4757. CMD prompt folder navigation tool?

RSS
Subscribe to Windows IT Pro | See More Tips Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!


If you often use the CMD prompt and have to constantly navigate to complex folder paths, CCD.cmd will make your life easier.

Dean Wells, MSEtechnology, has scripted the ccd.cmd, which has the following help (CCD /?) syntax:

ERROR - Nothing to do, help switch supplied ... syntax as follows -

        CCD [any number or sequence of directory path components] [/R]
        ... /R causes the drive index to be rebuilt

        e.g. -
             CCD istrator applic data
             ... C:\Documents and Settings\Administrator\Application Data
             CCD D: /r
             ... Causes the index for drive D to be rebuilt

        NOTE - Drive specifiers must be the first argument supplied
On first use, I built an index for each of my drives:
CCD C: /r
CCD D: /r
Now, after opening a CMD prompt, I can navigate to the TIP4700 folder by typing:
C:\>ccd tip4700

STATUS - Multiple hits found as follows, using primary match ...

C:\InetPub\WWWRoot\SUBJ\TIP4700
C:\InetPub\WWWRoot\SUBJ\TIP4700\_vti_cnf

Type POPD to return to previous working directory
C:\InetPub\WWWRoot\SUBJ\TIP4700>
To return to the previous folder, type popd.

To navigate to D:\WebCompiler\JSITTARH\SUBJ\TIP4700, I type ccd d: tip4700:

Type POPD to return to previous working directory
D:\WebCompiler\JSITTARH\SUBJ\TIP4700>
CCD.cmd contains:
:: Cute CD - Dean Wells, MSEtechnology - January 2002

@echo off

:: Check and redefine arguments
set SYNTAXERR=yes
set TMPSW=%*
if not defined TMPSW (
        echo.
        set ERROR=No arguments supplied, syntax as follows -
        call :ERROR
        goto :END
)
set SW=%TMPSW:"=%
if "%SW%"=="/?" (
        echo.
        set ERROR=Nothing to do, help switch supplied ... syntax as follows -
        call :ERROR
        goto :END
)
if not defined SW (
        echo.
        set ERROR=No arguments supplied, syntax as follows -
        call :ERROR
        goto :END
)
if not defined TMPSW (
        echo.
        set ERROR=No arguments supplied, syntax as follows -
        call :ERROR
        goto :END
)
set TMPDIR=%SW: /R=%
set DIR=%TMPDIR: /r=%

:: Prepare remaining environment, temporary files and the index
if "%DIR:~1,1%"==":" (
        set DRIVE=%DIR:~0,1%
) else (
        set DRIVE=%CD:~0,1%
)

:: Set the index file location
set INDEX=%TEMP%\%DRIVE%colon.ccd.index

:: Check for forced rebuild switch only
if /i "%SW%"=="/R" (
        set REBUILD=yes
        set REBUILDONLY=yes
)

:: Check for forced rebuild switch amongst other arguments
if not "%DIR%"=="%SW%" (
        set REBUILD=yes
)

:: Check for absence of existing drive index
if not exist %INDEX% (
        set REBUILD=yes
)

:: Convert case of drive letter (display purposes only)
for %%c in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do if /i %DRIVE%==%%c set DRIVE=%%c

:: Check for drive validity
if not exist %DRIVE%: (
        echo.
        set ERROR=Drive specifier was invalid - %DRIVE%
        call :ERROR
        goto :END
)

:: Check for root argument only
if "%DIR%"=="\" (
        set SPECIALCASE=yes
)

:: Check for drive letter only
if /i "%DIR:~0,3%"=="%DRIVE%:" (
        if "%REBUILD%"=="yes" (
                set REBUILDONLY=yes
        ) else (
                echo.
                set ERROR=Drive only specified, root of drive %DRIVE% assumed
                set SYNTAXERR=no
                call :ERROR
                echo.
                set SPECIALCASE=yes
        )
)

:: Check for drive letter plus root only
if /i "%DIR:~0,4%"=="%DRIVE%:\" (
        set SPECIALCASE=yes
)

:: Rebuild index if necessary or requested
if "%REBUILD%"=="yes" (
        del %INDEX% 2>nul
        echo.
        echo STATUS - Building new directory index for drive %DRIVE% - Wait ...
        dir %DRIVE%:\ /s /ad /b >%INDEX%
        echo.
        echo Completed!
)

:: Terminate if index rebuild was only argument supplied
if "%REBUILDONLY%"=="yes" goto :END

:: Check for special case target directory
if "%SPECIALCASE%"=="yes" (
        set DIR=%DRIVE%:        goto :SPECIALCASE
)

:: Check validity of existing index
if not exist %INDEX% (
        echo.
        set ERROR=The directory index for drive %DRIVE% could not be located
        set SYNTAXERR=no
        call :ERROR
        goto :END
)

:: Create temporary index files
copy %INDEX% %INDEX%.src /y 1>nul 2>&1
if not exist %INDEX%.src (
        echo.
        set ERROR=The temporary index for drive %DRIVE% could not be created
        set SYNTAXERR=no
        call :ERROR
        goto :END
)

:: Begin search for directory path strings supplied
for %%s in (%DIR%) do (
        copy %INDEX%.src %INDEX%.tgt /y 1>nul 2>&1
        type %INDEX%.tgt | find /i "%%s" >%INDEX%.src
)

:: Check for existence of temporary index files
if not exist %INDEX%.src (
        echo.
        set ERROR=Unable to read necessary temporary files
        set SYNTAXERR=no
        call :ERROR
        goto :END
)

:: Set the target directory
set /p DIR=<%INDEX%.src

:: Count the number of hits and react accordingly
for /f %%c in ('type %INDEX%.src ^|find /c /v "#$VeRyUnLiKeLyToOcCuR$#"') do set COUNT=%%c
if %COUNT%==0 (
        echo.
        set ERROR=No directories found on drive %DRIVE% matching %DIR%, try using /R switch
        call :ERROR
        goto :END
)
if %COUNT% GTR 1 IF %COUNT% LSS 21 (
        echo.
        echo STATUS - Multiple hits found as follows, using primary match ...
        echo.
        type %INDEX%.src
) else (
        echo.
        set ERROR=Too many hits to display, using primary match ...
        set SYNTAXERR=no
        call :ERROR
        echo.
)

:: Ready to change to new target working directory
:SPECIALCASE
pushd "%DIR%"
if not defined ERROR echo.
echo Type POPD to return to previous working directory
goto :END

:ERROR
echo ERROR - %ERROR%
if not "%SYNTAXERR%"=="yes" goto :EOF
echo.
echo         CCD [any number or sequence of directory path components] [/R]
echo         ... /R causes the drive index to be rebuilt
echo.
echo         e.g. -
echo              CCD istrator applic data
echo              ... C:\Documents and Settings\Administrator\Application Data
echo              CCD D: /r
echo              ... Causes the index for drive D to be rebuilt
echo.
echo         NOTE - Drive specifiers must be the first argument supplied
goto :EOF

:END
:: Clean up (setlocal is not being used due to variable handling issues)
del %INDEX%.tgt 2>nul
del %INDEX%.src 2>nul
set DRIVE=
set DIR=
set REBUILD=
set COUNT=
set INDEX=
set TMPDIR=
set TMPSW=
set ERROR=
set SYNTAXERR=
set REBUILDONLY=
set SW=
set SPECIALCASE=


End of Article



Reader Comments

You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
Battery Life Issues Almost Certainly Not Windows 7's Fault

While Microsoft is still investigating a notebook battery life issue that was supposedly caused by Windows 7, some interesting trends have emerged. ...

Confirmed: Battery Life Issues Not Windows 7's Fault

Microsoft on Monday issued a lengthy statement about the recent Windows 7 battery controversy, echoing my assessment from earlier in the day, but backing it up with hard, cold evidence. Put simply, Windows 7 is not responsible for any battery life issues ...

Getting your iPhone to Sync with Exchange 2003

Follow these steps to use an iPhone with Exchange. ...


Related Events 7 Ways To Get More From Your SharePoint Deployment Now

Check out our list of Free Email Newsletters!

Related Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2010 Penton Media, Inc. Terms of Use | Privacy Statement