Solved

how can i find the current working directory using batch files

Posted on 2008-10-23
6
2,038 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

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…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

635 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