Windows shell command to get the full path to the current directory?

664,983

Solution 1

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).

Solution 2

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

Solution 3

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.

Solution 4

On Unix?

pwd

Solution 5

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
Share:
664,983

Related videos on Youtube

user62958
Author by

user62958

Updated on July 14, 2021

Comments

  • user62958
    user62958 almost 3 years

    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?

    • Admin
      Admin about 12 years
      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" {} \;
    • Vishrant
      Vishrant about 4 years
      have a look at this answer stackoverflow.com/a/52301748/2704032
  • darjab
    darjab about 15 years
    How did you understand what he was trying to say from that ? And, under dos and windows cmd, its usually just "cd"
  • Trevor Bramble
    Trevor Bramble about 15 years
    Honestly, I couldn't think of anything else they might be trying to ask as the question stated.
  • darjab
    darjab about 15 years
    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.
  • user62958
    user62958 about 15 years
    Can I store this path inside a variable in a .bat file?
  • darjab
    darjab about 15 years
    @unknown - you might be better off by describing the original problem in the first place.
  • René Nyffenegger
    René Nyffenegger almost 11 years
    According to the question, this answer should actually be the accepted one.
  • jeb
    jeb almost 11 years
    But it doesn't work, as SET var = %cd% put the value in the variable var<space> not into var. You should avoid spaces in the SET command
  • Harry Johnston
    Harry Johnston over 9 years
    That does the wrong thing - finds the path of the batch script, not the current directory.
  • Stephan
    Stephan over 8 years
    very old, but just for completeness: yes, windows does have a varible for this. It's named (guess...) %cd%
  • Shridutt Kothari
    Shridutt Kothari about 8 years
    According to the question, this answer should actually be the accepted one.
  • jeb
    jeb over 7 years
    There are already 10 answers with the same solution, but more explanations
  • Tisch
    Tisch over 7 years
    OP originally asked for a "command" and didn't specify OS. OS has now been specified so this answer is no longer relevant.
  • A-Diddy
    A-Diddy over 6 years
    Upvoted due to answering the OP's question... and giving me the exact solution I was looking for. Thanks!
  • Tam Le
    Tam Le over 4 years
    Nice catch. Current directory could refer to the command that call the file instead of the file's location itself.
  • Garric
    Garric almost 4 years
    His answer is better by definition because your answer is wrong. It does not work for paths with spaces.
  • René Nyffenegger
    René Nyffenegger almost 4 years
    Adding tokens=* fixes that.
  • Дмитро Олександрович
    Дмитро Олександрович almost 3 years
    You can just "cd | clip" to copy current path to the clipboard