Suppose I have a script package that expects a folder path in argument %1 . I want to add the file name to the path and use it in a command. Is there an easy way to do this in such a way that it is reliable in all situations?
I don't want PUSHD %1 and just ignore the path later. Suppose my logic requires that the current directory remain unchanged.
The problem is that argument %1 may or may not have a trailing backslash - i.e. c:\path or c:\path\ .
Usually I can just use "%~1\file.ext" because Windows treats "c:\path\file.ext" and "c:\path\\file.ext" identically.
But there is one unpleasant case - c:\
The following commands create this error: The filename, directory name, or volume label syntax is incorrect.
dir "c:\\file.ext" del "c:\\file.ext"
Strange, these commands work very well:
if exist "c:\\file.ext" echo OK echo text>"c:\\file.ext" copy "c:\\file.ext" "somePath" xcopy "c:\\file.ext" "somePath"
There is another problematic case - c: that is, the current directory on the C: drive will give the wrong result if I add a backslash. "c:file.ext" does not match "c:\file.ext" .
Is there an easy way to safely add a file name to any given folder path?
I do not deal with UNC paths, as I do not use them very often, and I believe that there are some commands that do not play well with UNC paths.
Note. This is the case when I submit a paired question and answer. This problem has been a thorn on my side for a number of years, and I recently discovered a simple solution that I thought might interest others.