Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how can i find the current working directory using batch files

Posted on 2008-10-23
6
Medium Priority
?
2,040 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 21

Accepted Solution

by:
AmazingTech earned 2000 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

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…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

730 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