views:

33

answers:

2

I know that %0 contains the full path of the batch script, e.g. c:\path\to\my\file\abc.txt

I would path to be equal to c:\path\to\my\file

How could I achieve that ?

+4  A: 

%~dp0 will be the directory. Here's some documentation on all of the path modifiers. Fun stuff :-)

To remove the final slash, you can use the :n,m substring syntax, like so:

SET mypath=%~dp0
echo %mypath:~0,-1%

I don't believe there's a way to combine the %0 syntax with the :~n,m syntax, unfortunately.

Dean Harding
Excellent... I've been using `%~0\..` -- knew there had to be a better way! Also, you will probably want to enclose `%~dp0` in double quotation marks (`""`) in case there's spaces in the directory name, etc.
Cameron
Nice ! But, `%~dp0` contains the `\` at the end. Do you have an idea how to remove it ?
Misha Moroshko
@Misha: I assume you mean how to remove the '\' on the end. I've updated my answer with details.
Dean Harding
@Dean: You are great, thanks a lot !!
Misha Moroshko
A: 

That would be the %CD% variable.

@echo off
echo %CD%

%CD% returns the current directory the batch script is in.

Ruel
%cd% returns the directory the script was run from, not the directory the script is in.
Misha Moroshko
Nope, actually I tested it my self: http://imgur.com/PzAMU.jpg
Ruel
@Ruel: it only works if your script doesn't modify the the working directory. Try `CD C:\Temp <CR> ECHO %CD%` (`<CR>` is newline...)
Dean Harding
I see, thanks for the clarification.
Ruel
@Ruel: Also, if you right-click on the script and select "Run as Administrator", the starting current directory is C:\Windows\System32 regardless of where the script is located.
Cameron