Advanced push and pop commands

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

Advanced push and pop commands

Post by MigrationUser »

22 Jun 2012 17:00
chakri292

Hi,

Here we go with advanced pushd and popd commands i name them as pushx and popx

copy the below code and name it pushx.bat and put in c:\windows folder
code for pushx

Code: Select all

::Author chakri

@echo off
set file=%temp%\~ph.txt
::Clear list
if "%1" == "-c" ( 
 if exist %file% del %file% )
::Addto list
if "%1" == "." ( cd >> %file% )
::List the directories
if "%1" == "-l" ( if exist %file% ( findstr /R /N "^" %file% ) else ( echo Empty List ) )


copy the below code and name it popx.bat and put in c:\windows folder

[b]code for popx[/b]

::Author chakri
@echo off
set file=%temp%\~ph.txt
if "%1" == "-l" ( if exist %file% ( findstr /R /N "^" %file% && goto :eof ) else ( echo Empty List ) )
if "%1" NEQ "" (
                    if exist %file% ( findstr /R /N "^" %file% | findstr /R "%1:" > %temp%\~temp.txt
                    for /f "tokens=1,* delims=:" %%a in ('type %temp%\~temp.txt') do cd /d "%%b"
                    del %temp%\~temp.txt
                    )  else ( Echo Empty List ) )               
:eof

How to Use:

1. use pushx . to add a directory to the list
2. use pushx -l,popx -l to list the directories
3. to pop a directory use popx <NUMBER>
for eg., popx 2 will change ur directory
to the 2nd directory listed using pushx -l or pox -l

Please do add your comments for improving it

----------------------------

#2 23 Jun 2012 15:31
dbenham


That's a cool idea cool

I'm not sure I like your choice of names. It implies that the functionality is similar to PUSHD and POPD, but I find them quite different.

PUSHD pushes the current directory onto a history stack before changing to a new directory. Your PUSHX adds a directory to a list (not a stack), and does not change the current directory.

POPD changes directory to the previous directory as specified by the history stack. (It pops the stack). Your POPX changes directory to a directory from the list that you may not have visited yet.

In my mind your POPX is more similar to an advanced CD command, and PUSHD is simply an admin tool to maintain the list of directories.

I've taken your basic idea and put all the functionality into a single CDX.BAT.

I've added a number of enhancements:

- Offers a menu option in case you don't remember the folder numbers
- Enables you to specify a folder by a substring or folder number, whichever you prefer
- Provides an option to PUSHD instead of CD
- Provides an option to move the location of a folder within the list
- Maintains a separate directory list for each user
- Provides online help

The full usage documentation is embedded within the script.

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

Last edited by dbenham (01 Dec 2013 19:21)

----------------------------

#3 23 Jun 2012 16:15
Simon Sheppard


Interesting, the big advantage of using PushD over CD is that it can change drive as well as directory.

In the bash shell the command 'cd - ' will jump back to the last directory used (and a Powershell version can be found here: https://ss64.com/ps/syntax-cd.html ) [Link fixed]
Although it's a very simple approach, it is pretty quick and easy to remember.

Last edited by Simon Sheppard (23 Jun 2012 20:34)

----------------------------

#4 23 Jun 2012 18:02
dbenham


@Simon -The link you posted is broken.

I used the CD /D option in my code, so it is able to change the drive as well.

I added the /P option just so that you can use POPD afterward.
Post Reply