Solved

how to get the directory where a cmd file was startet

Posted on 2011-03-10
3
414 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

Being driven mad by email signature updates?

Having to make a change to your users’ email signatures, yet again? Feel like your head is going to explode? Rely on an Exclaimer email signature management solution to make the process simple!

Question has a verified solution.

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

A Bare Metal Image backup allows for the restore of an entire system to a similar or dissimilar hardware. They are highly useful for migrations and disaster recovery. Bare Metal Image backups support Full and Incremental backups. Differential backup…
In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

910 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

16 Experts available now in Live!

Get 1:1 Help Now