Solved

how to get the directory where a cmd file was startet

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

An article on effective troubleshooting
This article helps those who get the 0xc004d307 error when trying to rearm (reset the license) Office 2013 in a Virtual Desktop Infrastructure (VDI) and/or those trying to prep the master image for Microsoft Key Management (KMS) activation. (i.e.- C…
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…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

726 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