Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Execute a bat file with a variable name in the path

I am having a problem executing a powershell script where I want to point to different versions of a bat file based on some arguments

Hard coding the path as follows works with no issues:

    & 'C:\Program Files\MATLAB\R2022a\bin\mcc.bat'  -W $command -T link:exe  ...

but replacing a folder name with a variable causes it to fail:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    $Matlab_version = "R2022a"
    & 'C:\Program Files\MATLAB\$Matlab_version\bin\mcc.bat'  -W $command -T link:exe

with the following output:

 & : The term 'C:\Program Files\MATLAB\%$Matlab_version%\bin\mcc.bat' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:3
+ & 'C:\Program Files\MATLAB\%$Matlab_version%\bin\mcc.bat'  -W $comman ...

I’ve tried even creating a variable with the path name

$mcc_loc = & 'C:\Program Files\MATLAB\$Matlab_version\bin\mcc.bat'
Write-Host $mcc_loc

which produces a string with the correct path. Any help is greatly appreciated. My dumb solution is to have an if statement for each $Matlab_version and hard code it in there but I’m sure there has to be a more elegant solution

>Solution :

It’s because you are using single quotation marks.

variables will not be interpreted within single marks, everything will be taken as a literal string.

use double quotes instead when passing in variables mid string.

example:
quotes in action

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading