Skip to content

Instantly share code, notes, and snippets.

@programus
Last active October 15, 2020 06:59
Show Gist options
  • Select an option

  • Save programus/2d2738b2a746140186f7738b678bdcec to your computer and use it in GitHub Desktop.

Select an option

Save programus/2d2738b2a746140186f7738b678bdcec to your computer and use it in GitHub Desktop.
Extended windows cd command
@echo off
rem ***********************************************************************
rem This file is provided under MIT License:
rem http://www.opensource.org/licenses/mit-license.phprem
rem
rem This batch file is a extended cd command.
rem This could save the history and also jump in the histories.
rem For detail help, type <filename> /?
rem
rem Author: Programus ([email protected])
rem gist url:
rem https://gist.github.com/programus/2d2738b2a746140186f7738b678bdcec
rem
rem this tool need or create 2 temporary files:
rem %TEMP%\dir-list.tmp
rem %TEMP%\dir-prev.tmp
rem ***********************************************************************
if /i "%1"=="/help" goto :printhelp
if /i "%1"=="/h" goto :printhelp
if "%1"=="/?" goto :printhelp
goto :main
:printhelp
echo %~n0 - display all saved path with leading id
echo %~n0 ^<path^> - save current path and jump to ^<path^>
echo %~n0 :^<n^> - jump to the Nth path in the saved list
echo %~n0 : - jump to previous path
echo %~n0 rm [:]^<n^> - remove the Nth path from the list
echo %~n0 clear - clear the list
echo %~n0 /help - print out this help
echo %~n0 /? - same as above
goto :allover
:main
setlocal EnableDelayedExpansion
set tp=%TEMP%\dir-prev.tmp
set tf=%TEMP%\dir-list.tmp
rem extract previous path
set /p ppth=<%tp%
rem read list from file
set len=0
for /f "tokens=*" %%p in (%tf%) do (
set /a "len+=1"
set list[!len!]=%%p
)
rem list all history
if [%1]==[] (
for /l %%i in (1,1,%len%) do (
echo [%%i] !list[%%i]!
)
echo.
goto :end
)
rem goto previous directory and store current
if [%1]==[:] (
rem get the previous path
set pth=%ppth%
goto :savecd
)
rem clear all
if /i [%1]==[clear] (
set len=0
goto :write
)
rem mark index should be removed
if /i [%1]==[rm] (
set rmno=%2
goto :write
)
rem other cases
rem store the argument as path
set pth=%1
rem remove all double-quotation
set pth=%pth:"=%
if [%pth:~0,1%]==[:] (
rem start with ':', mean jump to a history record
rem get the real path
set pth=!list[%pth::=%]!
)
:savecd
rem store current path into history
set /a "len+=1"
set list[!len!]=%cd%
:write
rem write back to file
echo off
set j=0
(for /l %%i in (1,1,%len%) do (
rem do not save rm directory
if not [%%i]==[%rmno::=%] (
set pt=!list[%%i]!
rem find out whether current directory is written already
for /l %%k in (0,1,!j!) do (
if /i "!pt!"=="!written[%%k]!" (
rem if it is written already, mark as included
set included=T
)
)
if not "!included!"=="T" (
rem only write the directory if it is not written already, not included.
echo !pt!
rem mark the directory as written
set /a "j+=1"
set written[!j!]=!pt!
)
)
)) > %tf%
rem save current path as previous path
if /i not "%pth%"=="%cd%" (
if not "%pth%"=="" (
echo %cd%>%tp%
)
)
:end
endlocal && set ph=%pth%
rem change directory
if not "%ph%"=="" (
cd /d "%ph%"
set ph=
)
:allover
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment