views:

3585

answers:

3

In a Windows Batch script is there any way to return an absolute path from a value containing a filename and/or relative path?

Given:

"..\"
"..\somefile.txt"

I need the absolute path relative to the batch file.

Example:

  • "somefile.txt" is located at "C:\Foo\"
  • "test.bat" is located in "C:\Foo\Bar".
  • User opens a command window at "C:\Foo" and calls Bar\test.bat ..\somefile.txt
  • In the batch file "C:\Foo\somefile.txt" would be obtained using %1
+1  A: 

In your example, from Bar\test.bat, DIR /B /S ..\somefile.txt would return the full path.

George Sisco
I am not the one who downvoted you.
Nathan Taylor
That's not a bad idea... should I loop over the result of the DIR with a FOR and just grab the first result?
Nathan Taylor
I thought about the problem with multiple files. You didn't specify whether multiples were a problem, so I went with it.As for a FOR loop, I'm having trouble feeding "../" to a for loop, but ymmv. If you can't get it going on the DIR command, you might redirect output to a file and loop through that.I'm not saying the 'standard' way of extracting the path is bad, it's just that I thought mine did more of what you asked. Both solutions do 'something', and they both have their own set of issues.
George Sisco
Oh..and, if you loop through DIR successfully, you could overwrite redirect to a file and get the last result. If you reverse the order in a manner that is acceptable to you, you can get just the first result. If you have to loop through the file, reversing the order to gain the first result which would be in the last position may also help. The reason I suggest that is it may be easier to get and discard lots of things rather than actually deal with them. I don't know if my way of thinking makes sense to you. Just putting it out there as an idea.
George Sisco
+7  A: 

in batch files, as in a standard C program, argument 0 contains the path to the currently executing batch. you can use %~dp0 to get only the path portion of the 0th argument (which is the current script). this path is always a fully qualified path.

you can also get the fully qualified path of your first argument by using %~f1, but this gives a path according to the current path, which is obviously not what you want.

unfortunately, i don't know how to mix the 2 together... however, i can tell you that, personally, i often use the %~dp0%~1 idiom in my batch file, which interpret the first argument relative to the path of the executing batch. it does have a shortcoming though: it miserably fails if the first argument is fully-qualified.

Adrien Plisson
@Adrien: You can handle `%0` and `%1` likewise: `%~dpnx0` for fully qualified path+name of the batchfile itself, `%~dpnx1` for fully qualified path+name of its first argument [if that's a filename at all]. (But how on earth would you name a file on a different drive if you wouldn't give that full path info on the commandline anyway?)
pipitas
A: 

This is to help fill in the gaps in Adrien Plisson's answer (which should be upvoted as soon as he edits it ;-):

you can also get the fully qualified path of your first argument by using %~f1, but this gives a path according to the current path, which is obviously not what you want.

unfortunately, i don't know how to mix the 2 together...

One can handle %0 and %1 likewise:

  • %~dpnx0 for fully qualified drive+path+name+extension of the batchfile itself,
    %~f0 also suffices;
  • %~dpnx1 for fully qualified drive+path+name+extension of its first argument [if that's a filename at all],
    %~f1 also suffices;

%~f1 will work independent of how you did specify your first argument: with relative paths or with absolute paths (if you don't specify the file's extension when naming %1, it will not be added, even if you use %~dpnx1 -- however.

But how on earth would you name a file on a different drive anyway if you wouldn't give that full path info on the commandline in the first place?

However, %~p0, %~n0, %~nx0 and %~x0 may come in handy, should you be interested in path (without driveletter), filename (without extension), full filename with extension or filename's extension only. But note, while %~p1 and %~n1 will work to find out the path or name of the first argument, %~nx1 and %~x1 will not add+show the extension, unless you used it on the commandline already.

pipitas
the problem with `%~dpnx1` is that it gives the fully qualified path of the argument relative to the _current directory_, while the OP wants the path relative to _the directory where the batch file resides_.
Adrien Plisson
@Adrien Plisson: `%~dpnx1` gives fully qualified path of 1st argument. Not *relative* at all, and not relative to current dir either. The `d` is for drive, the `p` is for path, the `n` is for filename sans suffix, the `x` is for suffix, the `1` is for first argument. -- And Nathan's question was: *"Is there any way to return an absolute path from a value containing a filename and/or relative path?"*
pipitas