Solved

how can i find the current working directory using batch files

Posted on 2008-10-23
6
2,025 Views
Last Modified: 2012-06-21
how can i find the current working directory using batch files
0
Comment
Question by:manajitrath
  • 4
6 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 22785452
Easy one :-)

echo cd%

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 22785454
Or even
echo %cd%

0
 
LVL 13

Expert Comment

by:Brum07
ID: 22785456
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 21

Accepted Solution

by:
AmazingTech earned 500 total points
ID: 22794281
Depends on what you define as the current working directory.

I like to think of the current working directory as the directory where the batch file is running from. So no matter where I run my first batch file from I can reference additional batch files or helper exes more consistently.

I almost always use %~dp0.

To illustrate: Use the code to test what output you get.

Place your batch file in a directory let's say C:\TestFolder

In a command prompt goto C:\TestFolder and type the name of your batch file.
All is good both will output:
CD='C:\TestFolder'
DP0='C:\TestFolder\'

Move you batch file to C:\Windows\System32
Normal windows machines will have this directory in the search path. Staying in the C:\TestFolder type the name of your batch file.
Opps. They're different.
CD='C:\TestFolder'
DP0='C:\Windows\System32\'

Now move the batch file to a server share. Using a drive letter to your server share will produce the same results as if it were on C:
Let's open the UNC path to your server. eg \\servername\share
Double click on your batch file.
'\\servername\share'
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported.  Defaulting to Windows directory.

C:\WINDOWS>echo CD='C:\WINDOWS'
CD='C:\WINDOWS'

C:\WINDOWS>echo DP0='\\servername\share\'
DP0='\\servername\share\'

C:\WINDOWS>PAUSE
Press any key to continue . . .

Once again there is a difference between %CD% and %~dp0.

I find that %~dp0 is the most consistent and predictable.
echo CD='%cd%'

echo DP0='%~dp0'

PAUSE

Open in new window

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 22794575
Good point... and do similar when I need to pickup files from batch file dir. but the current working directory is the default directory used if you do, say, an unqualified dir, for instance.  Same as unix pwd.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 22959433
I like AmazingTech's suggestions for getting the batch file path to pickup associated files from the same dir BUT %CD% IS the current working directory which is what the question asked. Period.  If you do "copy something.txt d:\somewherelse.txt" it will pick it up from the %cd% directory.  That's what working directory is.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now