351

Is there a Windows command line command that I can use to get the full path to the current working directory?

Also, how can I store this path inside a variable used in a batch file?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user62958
  • 4,669
  • 5
  • 32
  • 35
  • find /dir/to/start/from -type f -ls This format the date to numeric find /dir/to/start/from -type f -exec ls -l --time-style="+ %Y %m %e %H:%M" {} \; –  May 07 '12 at 00:54

14 Answers14

556

Use cd with no arguments if you're using the shell directly, or %cd% if you want to use it in a batch file (it behaves like an environment variable).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Trevor Bramble
  • 8,523
  • 4
  • 29
  • 29
128

You can set a batch/environment variable as follows:

SET var=%cd%
ECHO %var%

sample screenshot from a Windows 7 x64 cmd.exe.

enter image description here

Update: if you do a SET var = %cd% instead of SET var=%cd% , below is what happens. Thanks to jeb.

enter image description here

Capturing the current directory from a batch file

gmaran23
  • 2,118
  • 2
  • 17
  • 18
59

Quote the Windows help for the set command (set /?):

If Command Extensions are enabled, then there are several dynamic
environment variables that can be expanded but which don't show up in
the list of variables displayed by SET.  These variable values are
computed dynamically each time the value of the variable is expanded.
If the user explicitly defines a variable with one of these names, then
that definition will override the dynamic one described below:

%CD% - expands to the current directory string.

%DATE% - expands to current date using same format as DATE command.

%TIME% - expands to current time using same format as TIME command.

%RANDOM% - expands to a random decimal number between 0 and 32767.

%ERRORLEVEL% - expands to the current ERRORLEVEL value

%CMDEXTVERSION% - expands to the current Command Processor Extensions
    version number.

%CMDCMDLINE% - expands to the original command line that invoked the
    Command Processor.

Note the %CD% - expands to the current directory string. part.

Patrick Cuff
  • 28,540
  • 12
  • 67
  • 94
36

On Unix?

pwd

Stephen Curial
  • 1,686
  • 15
  • 14
  • 3
    OP originally asked for a "command" and didn't specify OS. OS has now been specified so this answer is no longer relevant. – Tisch Dec 28 '16 at 16:13
34

This has always worked for me:

SET CurrentDir="%~dp0"

ECHO The current file path this bat file is executing in is the following:

ECHO %CurrentDir%

Pause
Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
Fred Scales
  • 357
  • 3
  • 2
  • 5
    That does the wrong thing - finds the path of the batch script, not the current directory. – Harry Johnston Feb 09 '15 at 23:47
  • 4
    Upvoted due to answering the OP's question... and giving me the exact solution I was looking for. Thanks! – A-Diddy Feb 06 '18 at 18:56
  • 1
    Nice catch. Current directory could refer to the command that call the file instead of the file's location itself. – Tam Le Dec 18 '19 at 10:23
  • 1
    %cd% is the wrong answer. If launching a CMD window with admin access, the directory shows as %windir%\system32\. %~dp0% shows the appropriate directory, even if in an admin launched CMD window. – Aaron Reed Dec 19 '22 at 21:49
25

For Windows we can use

cd

and for Linux

pwd

command is there.

Nikunj K.
  • 8,779
  • 4
  • 43
  • 53
12

Create a .bat file under System32, let us name it copypath.bat the command to copy current path could be:

echo %cd% | clip

Explanation:

%cd% will give you current path

CLIP

Description:
    Redirects output of command line tools to the Windows clipboard.
    This text output can then be pasted into other programs.

Parameter List:
    /?                  Displays this help message.

Examples:
    DIR | CLIP          Places a copy of the current directory
                        listing into the Windows clipboard.

    CLIP < README.TXT   Places a copy of the text from readme.txt
                        on to the Windows clipboard.

Now copypath is available from everywhere.

Vishrant
  • 15,456
  • 11
  • 71
  • 120
11

On Windows:

CHDIR Displays the name of or changes the current directory.

In Linux:

PWD Displays the name of current directory.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ajay Jayavarapu
  • 471
  • 1
  • 6
  • 16
9

For Windows, cd by itself will show you the current working directory.

For UNIX and workalike systems, pwd will perform the same task. You can also use the $PWD shell variable under some shells. I am not sure if Windows supports getting the current working directory via a shell variable or not.

Michael Trausch
  • 3,187
  • 1
  • 21
  • 29
  • I can't however understand why he needs "cd" to see his current dir. By default, it is visible as day. And if he's changed it, than he certainly knows what "cd" does. – Rook Mar 03 '09 at 19:13
  • very old, but just for completeness: yes, windows does have a varible for this. It's named (guess...) `%cd%` – Stephan Jan 30 '16 at 10:21
5

Based on the follow up question (store the data in a variable) in the comments to the chdir post I'm betting he wants to store the current path to restore it after changeing directories.

The original user should look at "pushd", which changes directory and pushes the current one onto a stack that can be restored with a "popd". On any modern Windows cmd shell that is the way to go when making batch files.

If you really need to grab the current path then modern cmd shells also have a %CD% variable that you can easily stuff away in another variable for reference.

Mark
  • 1,058
  • 6
  • 13
4

In a Windows command prompt, chdir or cd will print the full path of the current working directory in the console.

If we want to copy the path then we can use: cd | clip.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Samrat Debroy
  • 396
  • 7
  • 9
3
@echo off
for /f "usebackq tokens=*" %%x in (`chdir`) do set var=%%x
echo The currenct directory is: %var%

But, of course, gmaran23's answer is the much easier one.

René Nyffenegger
  • 39,402
  • 33
  • 158
  • 293
0

On Windows, type cd for the working current path.

On Linux, pwd for the current working path.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shafeer khan
  • 464
  • 5
  • 8
0

As one of the possible codes

    echo off
    for /f "usebackq tokens=* delims= " %%x in (`chdir`) do set var=%var% %%x
    echo The current directory is: "%var:~1%"
Garric
  • 591
  • 3
  • 10