Solved

how to get the directory where a cmd file was startet

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

This is an article about Leadership and accepting and adapting to new challenges. It focuses mostly on upgrading to Windows 10.
While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

770 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