Solved

how to get the directory where a cmd file was startet

Posted on 2011-03-10
3
418 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
[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
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …

737 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