The problem solved here is two-fold:
- The ~dp syntax for getting the directory/path part only works for parameters and loop indexes, not on variables.
You can work around this by having a 1-iteration for-loop. - The :~ syntax for getting the substring works only for variables, not for parameters and loop indexes.
You can work around this by assigning to a temporary variable.
Example:
echo batchfile=%0 echo full=%~f0 setlocal ::http://stackoverflow.com/questions/636381/what-is-the-best-way-to-do-a-substring-in-a-batch-file set Directory=%~dp0 echo Directory=%Directory% :: strip trailing backslash set Directory=%Directory:~0,-1% echo %Directory% :: ~dp does not work for regular environment variables: :: set ParentDirectory=%Directory:~dp% set ParentDirectory=%Directory:~dp% :: ~dp only works for batch file parameters and loop indexes for %%d in (%Directory%) do set ParentDirectory=%%~dpd echo ParentDirectory=%ParentDirectory% endlocal
This will show:
- The directory of the batch file
- The directory but without the trailing backslash
- The parent directory of the batch file
Hope you can give this some use.
–jeroen