Solved

how can i find the current working directory using batch files

Posted on 2008-10-23
6
2,027 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 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

9 Experts available now in Live!

Get 1:1 Help Now