Page 1 of 1

Script directory url

Posted: 24 Aug 2011 15:15
by kkuba
Hi,

I need to get url of directory where is my batch script.
Example:

Code: Select all

echo %0


displays:

C:\Users\kkuba\Desktop\test.bat


but I need:

C:\Users\kkuba\Desktop\


so here is my question - how can I do that?
%dir% is not that what I want. If I drag and drop any file to my batch script, itself value is url to file that I drag and drop, not url to batch script.

Hope you understand what I'm talking about - my english is soooo baaad : ]

ps I thought to split a string, extract substrings by delimiter, etc, but I think there is simpler way.

Re: Script directory url

Posted: 24 Aug 2011 15:43
by dbenham
kkuba wrote:ps I thought to split a string, extract substrings by delimiter, etc, but I think there is simpler way.
You are correct - there is a much simpler way :)

Code: Select all

echo %~dp0

Use HELP CALL for a complete list of available batch parameter expansion modifiers. The same modifiers are available for FOR variable expansion. (see HELP FOR)

Dave Benham

Re: Script directory url

Posted: 28 Aug 2011 16:02
by Ranguna173
Lol :lol: :lol: :lol: :lol:

People always complicate things..

Code: Select all

echo %cd%


That will display the directory where your batch file is executing..

Dave is the best!
But sometimes he complicates things..

Re: Script directory url

Posted: 28 Aug 2011 17:36
by dbenham
Ranguna173 wrote:Lol :lol: :lol: :lol: :lol:

People always complicate things..

Code: Select all

echo %cd%


That will display the directory where your batch file is executing..

Dave is the best!
But sometimes he complicates things..

There is no reason to expect that the current working directory is the same as the directory from which the batch file was launched. The batch file could have been called with an explicit path, or it could have been found via the PATH, or it could have changed the working directory after it started. Any of these conditions could lead to %CD% giving an answer that does NOT correspond to the batch file's directory.

Dave Benham

Re: Script directory url

Posted: 28 Aug 2011 17:46
by aGerman
You're absolutely right, Dave. But ... there is a SHIFT command. Nothing protects you from a wrong result if you're doing the right things in wrong order :wink:

Code: Select all

@echo off
:: some code
shift
echo %~dp0
pause


Regards
aGerman