Using the "%~dp0" with command line arguments not working

12,706

You could use %CD%\ which stands for Current Directory instead of %~dp0.

I haven't noticed any difference between these two.

On a side note if you used start command, the best practice is to add an extra "" before program-to-start and include all parameters within one quote, and escape quoted parameters when necessary. Example:

start /wait "" "%CD%\MyProgram.exe /link \"MyOtherDLL.dll\""
Share:
12,706

Related videos on Youtube

Alex Pilon
Author by

Alex Pilon

Updated on September 18, 2022

Comments

  • Alex Pilon
    Alex Pilon almost 2 years

    Been concocting a deployment script for a software, which needs to install Visual C++ 2013 Redistributable, in silent mode, from a moving source (aka, the folder might be on a key, on a server, locally, etc.), as Admin.

    For all my other executables (MSI mostly), using "%~dp0" before the file name (like this: "%~dp0Antidote9.msi", followed by arguments like /quiet, /q, etc., works fine.

    But for VC Redist, it doesn't accept my arguments.

    Here is my full command:

    start /wait "%~dp0vcredist_x64.exe" /install /quiet /norestart
    

    It pulls the file fine, but then says that my arguments are invalid, even though if I remove the %~dp0 it works fine, and I got the arguments from doing vcredist_x64.exe /?.

    Is there something I'm doing wrong or that I need to modify for my arguments to be working ?

    Thanks in advance!

  • Alex Pilon
    Alex Pilon about 8 years
    Hey! %CD% doesn't work. The reason I'm using %~dp0 is because it needs to pull up the path where the CMD file is located, since it's gonna be run as admin. If I use %CD%, it pulls up C:\Windows\system32 instead.
  • Vassile
    Vassile about 8 years
    @AlexPilon Try adding a \ (back-slash) denoting to the end of path after %~dp0, then continue with your program executable. Also have you tried adding extra ""? By syntax, start takes the first parameter as window title (for old DOS-style program probably) and the second parameter as executable path (and corresponding arguments for that specific program, included in the same quote, with properly-escaped subquotes).
  • Alex Pilon
    Alex Pilon about 8 years
    Actually, I was editing my comment just before you posted lol I found a way to do it, with some of your help. Adding the extra "" at the start did the trick. If I added the whole path & arguments in quotation marks though it would consider the path & arguments as being the filename. So my final line is: start /wait "" "%~dp0vcredist_x64.exe" /install /quiet /norestart And it works perfectly! Thanks for your help :)
  • Vassile
    Vassile about 8 years
    @AlexPilon Cool. You also helped me clarified the differences between the two.