PUSHD

Change the current directory/folder and store the previous folder/path for use by the POPD command.

Syntax
      PUSHD [drive]path

      PUSHD

Key
   drive  The drive to switch to.
   path   The folder to make 'current' (UNC names accepted).

If the drive is not specified, the current drive will be assumed.
If neither drive nor path are specified PUSHD will just display a list of previous pathnames, you can switch back to any of these by using POPD one or more times.
The number of pushed directories can be displayed on the command line with PROMPT $+
If the path specified is a file not a folder, or if it does not exist, PUSHD will return %errorlevel% =1

Batch files

When PUSHD is used in a batch script it is strongly recommended that you check the %errorlevel% to be sure that the directory exists and was successfully connected before continuing:

PUSHD \\install_server\wsus_share
:: If this fails then exit
If %errorlevel% NEQ 0 goto:eof
:: other commands...

:: or in one line:
PUSHD "%~1" 2>nul && goto sub-error-failed

Run as Admin

When a batch script is 'Run as Admin', the current directory will be set to C:\windows\system32\.
Using the following pushd command at the start of the script will restore the normal current directory.

This works by setting the current directory to the location of the batch script, using the %0 parameter

pushd "%~dp0"

UNC Network paths

When a UNC path is specified, PUSHD will create a temporary drive map and will then use that new drive.
The temporary drive letters are allocated in reverse alphabetical order, so if Z: is free it will be used first.

Errorlevels

If the Current directory was changed: %ERRORLEVEL% = 0
If the Directory does not exist or is not accessible or if a bad switch given: %ERRORLEVEL% = 1

PUSHD is an internal command. If Command Extensions are disabled the PUSHD command will no longer map temporary drives to UNC paths, and POPD will not delete such drives.

Examples

C:\demo> pushd \work 
C:\work> echo %errorlevel%
0
C:\work> popd
C:\demo> pushd "F:\monthly reports" F:\monthly reports> popd
C:\demo> C:\> pushd "C:\demo\demo.1" 2>nul || Echo Error not a folder

#Ah, push it - push it good
Ah, push it - p-push it real good# ~ Salt 'N' Pepa

Related commands

CD - Change directory.
CMD - UNC options.
PROMPT - Display the level of the PUSHD stack ($+).
Equivalent PowerShell: Push-Location - Push a location to the stack (pushd).
Equivalent PowerShell: cd - Jump to the previous working directory.
Equivalent bash command (Linux): pushd - Save and then change the current directory.


 
Copyright © 1999-2024 SS64.com
Some rights reserved