Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to turn a relative path into an absolute path?

3,798 views
Skip to first unread message

Dennis Halver

unread,
Jul 7, 2009, 11:13:29 AM7/7/09
to
Assume I start a DOS batch script which can be stored in various directories
on various computers.

One of the first instructions is:

set basedir=%~dp0\..\..

How can I turn/convert this relative path into an absolute path (with no ".." back parent references)?

Dennis

Tim Meddick

unread,
Jul 7, 2009, 8:31:22 PM7/7/09
to

I believe this is an absolute path!

Despite the use of two 'back-tracking directories' - the path does begin
with a drive letter.

An absolute path (as far as I am aware) is one that can be worked out
from the information given, whereas :

.\system\ikons\

...would NOT be an absolute path, but :

c:\windows\system32\shellext\..\

..would be (simply being the equivalent of C:\windows\system32)


(I could be wrong about this but did think that an absolute path was one
that could be resolved from any location...)

==

Cheers, Tim Meddick, Peckham, London. :-)


"Dennis Halver" <ha...@persrec.com> wrote in message
news:4a536618$0$31870$9b4e...@newsspool3.arcor-online.net...

Todd Vargo

unread,
Jul 8, 2009, 5:08:20 PM7/8/09
to
Tim Meddick wrote:
>
> I believe this is an absolute path!
>
> Despite the use of two 'back-tracking directories' - the path does begin
> with a drive letter.
>
> An absolute path (as far as I am aware) is one that can be worked out
> from the information given, whereas :
>
> .\system\ikons\
>
> ...would NOT be an absolute path, but :
>
> c:\windows\system32\shellext\..\
>
> ..would be (simply being the equivalent of C:\windows\system32)
>
>
> (I could be wrong about this but did think that an absolute path was one
> that could be resolved from any location...)

Sure %~dp0 returns an absolute path but, AIUI, what the OP really wants to
get is the basedir (which is the first folder after the drive letter). In
your example that would be "c:\windows".

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

Todd Vargo

unread,
Jul 8, 2009, 5:33:51 PM7/8/09
to
Tim Meddick wrote:
> I believe this is an absolute path!
>
> Despite the use of two 'back-tracking directories' - the path does begin
> with a drive letter.
>
> An absolute path (as far as I am aware) is one that can be worked out
> from the information given, whereas :
>
> .\system\ikons\
>
> ...would NOT be an absolute path, but :
>
> c:\windows\system32\shellext\..\
>
> ..would be (simply being the equivalent of C:\windows\system32)
>
>
> (I could be wrong about this but did think that an absolute path was one
> that could be resolved from any location...)

Please do not multi-post. The appropriate method is cross-post.

Tim Meddick

unread,
Jul 9, 2009, 1:54:56 AM7/9/09
to
In the original path the OP wants the references to parent dirs taken
out, but I say there is no way to do this.

If you have some program that has specified that the base dir must be
%~dp0\..\.. this will turn out to be two dirs above that the batch
file is in (specified by parameter 0) - wherever it is.

If the batch file were in C:\WINDOWS\system32\macromed\flash to a batch
file containing %~dp0\..\.. then the result would be base dir =
C:\windows\system32 .... two dirs above
C:\windows\system32\macromed\flash....

==

Cheers, Tim Meddick, Peckham, London. :-)


"Todd Vargo" <tlv...@sbcglobal.netz> wrote in message
news:h331s6$2iju$1...@adenine.netfront.net...

Todd Vargo

unread,
Jul 9, 2009, 7:08:21 PM7/9/09
to
<crosspost restored>

"Tim Meddick" <timme...@gawab.com> wrote in message
news:h340o6$3tc$1...@aioe.org...


> In the original path the OP wants the references to parent dirs taken
> out, but I say there is no way to do this.
>
> If you have some program that has specified that the base dir must be
> %~dp0\..\.. this will turn out to be two dirs above that the batch
> file is in (specified by parameter 0) - wherever it is.
>
> If the batch file were in C:\WINDOWS\system32\macromed\flash to a batch
> file containing %~dp0\..\.. then the result would be base dir =
> C:\windows\system32 .... two dirs above
> C:\windows\system32\macromed\flash....

Many OP's seem to explain what they want using poor pseudo examples or
incorrect nomenclature causing multiple interpretations as to what they
really meant/wanted.

However...

Since %~dp0 returns a trailing backslash, if current folder was


C:\WINDOWS\system32\macromed\flash

then %~dp0\..\.. would then return
C:\WINDOWS\system32\macromed\flash\batname.cmd\\..\..
(Note the double backslash)

Following even your logic, it is still unclear if OP wanted basedir to
return


C:\WINDOWS\system32\macromed\flash

or C:\WINDOWS\system32\macromed
because real examples were not provided.

We will have to wait for OP to post back to clarify which was actually meant
(basedir=x folders up, or basedir=rootfolder). Meanwhile, the previous code
I posted demonstrates walking up the path to the root folder regardless of
depth. Stepping back a fixed number of folders is even easier (what you
claim "there is no way to do this").

@echo off
for %%i in ("%~dp0..\..") do (set basedir=%%~dpi)
echo basedir=%basedir%

Tim Meddick

unread,
Jul 10, 2009, 9:31:42 PM7/10/09
to
The OP says the batch file can be in different folders.

The program that wrote the batch, specified :

set BASEDIR=%~dp0\..\..

How would you specify it any different?

i.e. to resolve it to an "absolute path" as the OP requested, for a
batch file run from *any* directory?


And quit the "...even your logic" jibes - they are beneath you.

==

Cheers, Tim Meddick, Peckham, London. :-)


"Todd Vargo" <tlv...@sbcglobal.netz> wrote in message

news:h35tav$2nck$1...@adenine.netfront.net...

Todd Vargo

unread,
Jul 10, 2009, 10:50:14 PM7/10/09
to
Tim Meddick wrote:
> The OP says the batch file can be in different folders.
>
> The program that wrote the batch, specified :
>
> set BASEDIR=%~dp0\..\..
>
> How would you specify it any different?
>
> i.e. to resolve it to an "absolute path" as the OP requested, for a
> batch file run from *any* directory?
>
>
> And quit the "...even your logic" jibes - they are beneath you.

Honestly! The OP has not chimed in to clarify but you are still trying to
debate the OPs request based on the code. Did a program write it or did the
OP? OP never stated a program wrote it. Put this simple command in a batch
and see for yourself if it returns a valid path. Then move the batch to
other folders and try it. No folder you put it in will return a valid path.

@echo BASEDIR=%~dp0\..\..

While we are waiting for the OP to chime in, it would be particularly
appropriate for you to take the time to read up on proper quoting practices.

http://lipas.uwasa.fi/~ts/http/quote.html

0 new messages