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.
Update: if you do a SET var = %cd%
instead of SET var=%cd%
, below is what happens. Thanks to jeb.
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
Related videos on Youtube
Author by
user62958
Updated on July 14, 2021Comments
-
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 about 12 yearsfind /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 about 4 yearshave a look at this answer stackoverflow.com/a/52301748/2704032
-
-
darjab about 15 yearsHow did you understand what he was trying to say from that ? And, under dos and windows cmd, its usually just "cd"
-
Trevor Bramble about 15 yearsHonestly, I couldn't think of anything else they might be trying to ask as the question stated.
-
darjab about 15 yearsI 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 about 15 yearsCan I store this path inside a variable in a .bat file?
-
darjab about 15 years@unknown - you might be better off by describing the original problem in the first place.
-
René Nyffenegger almost 11 yearsAccording to the question, this answer should actually be the accepted one.
-
jeb almost 11 yearsBut it doesn't work, as
SET var = %cd%
put the value in the variablevar<space>
not intovar
. You should avoid spaces in the SET command -
Harry Johnston over 9 yearsThat does the wrong thing - finds the path of the batch script, not the current directory.
-
Stephan over 8 yearsvery old, but just for completeness: yes, windows does have a varible for this. It's named (guess...)
%cd%
-
Shridutt Kothari about 8 yearsAccording to the question, this answer should actually be the accepted one.
-
jeb over 7 yearsThere are already 10 answers with the same solution, but more explanations
-
Tisch over 7 yearsOP originally asked for a "command" and didn't specify OS. OS has now been specified so this answer is no longer relevant.
-
A-Diddy over 6 yearsUpvoted due to answering the OP's question... and giving me the exact solution I was looking for. Thanks!
-
Tam Le over 4 yearsNice catch. Current directory could refer to the command that call the file instead of the file's location itself.
-
Garric almost 4 yearsHis answer is better by definition because your answer is wrong. It does not work for paths with spaces.
-
René Nyffenegger almost 4 yearsAdding
tokens=*
fixes that. -
Дмитро Олександрович almost 3 yearsYou can just "cd | clip" to copy current path to the clipboard