CDX.BAT - Easily navigate folders from the command line

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dbenham
Expert
Posts: 2451
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

CDX.BAT - Easily navigate folders from the command line

#1 Post by dbenham » 01 Dec 2013 12:48

I originally developed and posted the CDX.BAT utility over one year ago at http://ss64.org/viewtopic.php?pid=5780 based on an idea by SS64 user chakri292. I find I am using the utility frequently both at home and work, so I thought I would share it here.

The utility allows a user to store and manage a list of favorite folder paths in the %USERPROFILE% folder. Once the list is built, the user can quickly CD (or PUSHD) to any favorite location using either CDX Number or CDX SubString.

Full documentation is embedded within the script. I recommend putting CDX.BAT somewhere within your PATH.

Code: Select all

:::
:::CDX.BAT - Provides a convenient way to navigate to common folder locations,
:::          and provides a way to manage the list of common folders.
:::          A separate list is maintained for each user.
:::
:::          CDX.BAT was written by Dave Benham based on an original idea by
:::          SS64 user chakri292. See http://ss64.org/viewtopic.php?id=1488
:::
:::  cdx [/P] [Number|String]
:::
:::    Changes the current directory to either the folder Number,
:::    or else to the first folder found that contains the String.
:::
:::    If neither Number nor String is specified, then presents a menu
:::    to choose the folder number.
:::
:::    If /P option is specifed then performs a PUSHD instead of CD.
:::
:::  cdx /A FolderPath [Number]
:::
:::    Inserts the FolderPath immediately before the specified folder Number.
:::
:::    If Number is not found then appends FolderPath to the end.
:::
:::    If Number is not specified then presents a menu to choose where to
:::    insert the FolderPath.
:::
:::    A relative FolderPath may be specified and the absolute path will be
:::    inserted into the common list. The FolderPath will not be added if
:::    it does not exist.
:::
:::    If FolderPath already exists in the common list, then it will be moved
:::    as specified by the Number.
:::
:::  cdx /M [Number InsertNumber]
:::
:::    Moves the folder specified by Number and inserts it before the position
:::    specified by InsertNumber. Moves the folder to the end if InsertNumber
:::    does not yet exist.
:::
:::    If either Number or InsertNumber is not specified then presents
:::    a menu to select a folder and insertion point.
:::
:::  cdx /D [Number]
:::
:::    Deletes the folder specified by folder Number.
:::
:::    If Number is not specified then presents a menu to choose which
:::    folder Number to delete.
:::
:::  cdx /C
:::
:::    Clears the list of common folders.
:::
:::  cdx /L
:::
:::    Lists the common folders currently stored.
:::
:::  cdx /?
:::
:::    Displays this help
:::

@echo off
setlocal

set "data=%userprofile%\cdx_%username%.dat"
set "newData=%data%.new"

set forEachDir=for /f "tokens=1* delims=:" %%A in ('2^^^>nul findstr /n "^" "%data%"') do
set move=move /y "%newData%" "%data%" ^>nul^&for %%A in ("%data%") do if "%%~zA"=="0" del "%data%"
set checkEmpty=if not exist "%data%" echo(^&echo Your list of common folders is empty. Use CDX /? to get help.^&exit /b

if "%~1"=="/?" goto :HELP
>nul findstr /xblic:":%~1" "%~f0" && goto :%~1
%checkEmpty%
set choice=%~1
if not defined choice call :menu CD quit
set "action=cd /d"
goto :setDir

:/P
%checkEmpty%
set "choice=%~2"
if not defined choice call :menu PUSHD quit
set "action=pushd"
goto :setDir

:/M
%checkEmpty%
set "choice=%~2"
set "choice2=%~3"
if not defined choice set "choice2="
if not defined choice2 set "choice="
if not defined choice call :menu "folder to MOVE" quit
set "folder="
%forEachDir% if "%choice%"=="%%A" set "folder=%%B"
if not defined folder exit /b
if not defined choice2 (
  set choice2=end
  set /p "choice2=Enter Number for new insertion point, nothing to move to end: "
)
call :/A dummy "%folder%" %choice2%
exit /b

:/A
if "%~2"=="" exit /b
if not exist "%~2\" exit /b
set choice=%3
if defined choice (set choice=%~3) else if exist "%data%" call :menu INSERT APPEND
set found=
>"%newData%" (
  %forEachDir% (
    if "%%A"=="%choice%" (
      echo(%~f2
      set found=1
    )
    if /i "%~f2" neq "%%B" echo(%%B
  )
  if not defined found echo(%~f2
)
%move%
exit /b

:/D
%checkEmpty%
set "choice=%~2"
if not defined choice call :menu DELETE quit
>"%newData%" (
  %forEachDir% if not "%%A"=="%choice%" echo(%%B
)
%move%
exit /b

:/C
del "%data%" 2>nul
exit /b

:/L
%checkEmpty%
call :menu LIST
exit /b

:help
(
  for /f "tokens=* delims=:" %%A in ('findstr "^:::" "%~f0"') do @echo(%%A
)|more
exit /b

:menu  action  action2
%forEachDir% if %%A lss 10 (echo( %%A %%B) else (echo(%%A %%B)
if %1==LIST exit /b
echo(
set "choice="
set /p "choice=Enter the number for %~1, or nothing to %~2: "
exit /b

:setDir
%forEachDir% if "%choice%"=="%%A" endlocal & %action% "%%B" & exit /b
if defined choice for /f "tokens=* delims=:" %%A in (
  'findstr /i /c:"%choice%" "%data%"'
) do endlocal & %action% "%%A" & exit /b
exit /b


Dave Benham

Squashman
Expert
Posts: 4293
Joined: 23 Dec 2011 13:59

Re: CDX.BAT - Easily navigate folders from the command line

#2 Post by Squashman » 03 Dec 2013 15:23

Was searching for a cmd line utility today and stumbled upon this and it reminded me of your batch file.
http://waterlan.home.xs4all.nl/

brinda
Posts: 78
Joined: 25 Apr 2012 23:51

Re: CDX.BAT - Easily navigate folders from the command line

#3 Post by brinda » 04 Dec 2013 21:13

dave thanks for this. much easier.

Post Reply