Resolve absolute path from relative path and/or file name

前端 未结 14 1786
被撕碎了的回忆
被撕碎了的回忆 2020-11-27 09:38

Is there a way in a Windows batch script to return an absolute path from a value containing a filename and/or relative path?

Given:

         


        
14条回答
  •  忘掉有多难
    2020-11-27 10:04

    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.

提交回复
热议问题