(First, I'd like to recommend this useful reference site for batch:
[To see links please register here]
)
Then just another useful explanation:
[To see links please register here]
> # The %~dp0 Variable #
>
> The `%~dp0` (that’s a zero) variable when referenced within a Windows
> batch file will expand to the **d**rive letter and **p**ath of that batch
> file.
>
> The variables `%0-%9` refer to the command line parameters of the batch
> file. `%1-%9` refer to command line arguments after the batch file name.
> `%0` refers to the batch file itself.
>
> If you follow the percent character (`%`) with a tilde character (`~`),
> you can insert a modifier(s) before the parameter number to alter the
> way the variable is expanded. The `d` modifier expands to the drive
> letter and the `p` modifier expands to the path of the parameter.
>
> Example: Let’s say you have a directory on `C:` called `bat_files`, and
> in that directory is a file called `example.bat`. In this case, `%~dp0`
> (combining the `d` and `p` modifiers) will expand to `C:\bat_files\`.
>
> Check out [this Microsoft article][1] for a full explanation.
>
> Also, check out [this forum thread][2].
And a more clear reference from [here][3]:
- `%CmdCmdLine%` will return the entire command line as passed to CMD.EXE
- `%*` will return the remainder of the command line starting at the first command line argument (in Windows NT 4, %* also includes all leading spaces)
- `%~dn` will return the drive letter of %n (n can range from 0 to 9) if %n is a valid path or file name (no UNC)
- `%~pn` will return the directory of %n if %n is a valid path or file name (no UNC)
- `%~nn` will return the file name only of %n if %n is a valid file name
- `%~xn` will return the file extension only of %n if %n is a valid file name
- `%~fn` will return the fully qualified path of %n if %n is a valid file name or directory
ADD 1
---
Just found some good reference for the *mysterious* `~` tilde operator.
The `%~` string is called [percent tilde][4] operator. You can find it in situations like: `%~0`.
The `:~` string is called [colon tilde][5] operator. You can find it like `%SOME_VAR:~0,-1%`.
ADD 2 - 1:12 PM 7/6/2018
---
`%1-%9` refer to the command line args. If they are **not** valid path values, `%~dp1` - `%~dp9` will all expand to the same value as `%~dp0`. But if they **are** valid path values, they will expand to **their own** driver/path value.
For example:
(batch.bat)
@echo off
@echo ~dp0= %~dp0
@echo ~dp1= %~dp1
@echo ~dp2= %~dp2
@echo on
Run 1:
D:\Workbench>batch arg1 arg2
~dp0= D:\Workbench\
~dp1= D:\Workbench\
~dp2= D:\Workbench\
Run 2:
D:\Workbench>batch c:\123\a.exe e:\abc\b.exe
~dp0= D:\Workbench\
~dp1= c:\123\
~dp2= e:\abc\
[1]:
[To see links please register here]
[2]:
[To see links please register here]
[3]:
[To see links please register here]
[4]:
[To see links please register here]
[5]:
[To see links please register here]