Solved

how to get the directory where a cmd file was startet

Posted on 2011-03-10
3
413 Views
Last Modified: 2012-05-11
How can i get the path from where a cmd file was startet.
Actual i have a cmd File 'Update.cmd' located on an USB stick in e.g. directory u:\update and i want to get that path into a variable during execution of thar cmd file.
0
Comment
Question by:CumulusGranitus
  • 2
3 Comments
 
LVL 43

Accepted Solution

by:
Steve Knight earned 250 total points
ID: 35093194
There are two paths here possibly, the one that is the current directory when the batch is called, and the path of the batch file itself.  I imagine you want the latter.  This gives some examples:

@echo off
echo This batch file is called %~f0
echo and was run using %~0
echo and is stored in "%~dp0" with filename "%~nx0" and last edited on %~t0
echo.
echo and was started with default dir of "%cd%"
echo.
echo Now changing to the directory of the batch file location:
cd /d "%~dp0"
echo The current directory is now "%cd%"
pause


%0 is wht was used to call the batch file
the ~ removes any "" characters around it
the dpnx etc. gives drive, path, name, and extension parts of the path.  f is the full path and filename.

hth

Steve
0
 

Author Comment

by:CumulusGranitus
ID: 35093200
thanks, that was the missing hint.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 35093227
No problem.
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

Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

757 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

20 Experts available now in Live!

Get 1:1 Help Now