Script directory url

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
kkuba
Posts: 1
Joined: 24 Aug 2011 15:03

Script directory url

#1 Post by kkuba » 24 Aug 2011 15:15

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.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Script directory url

#2 Post by dbenham » 24 Aug 2011 15:43

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

Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

Re: Script directory url

#3 Post by Ranguna173 » 28 Aug 2011 16:02

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..

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Script directory url

#4 Post by dbenham » 28 Aug 2011 17:36

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Script directory url

#5 Post by aGerman » 28 Aug 2011 17:46

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

Post Reply