Solved

how can i find the current working directory using batch files

Posted on 2008-10-23
6
2,037 Views
Last Modified: 2012-06-21
how can i find the current working directory using batch files
0
Comment
Question by:manajitrath
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

710 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