Pass Variable into Cmd

Pass Variable into Cmd

I have setup a script and tested that it work with user input via set /p but now I want to pass that variable automatically to the cmd.

Script w/ user input:

@ECHO OFF

set /p id="Enter Rom: "

start /d "G:\Emulators\N64\Project64" Project64.exe "G:\Emulators\N64\Roms\%id%"

What I want to pass to the CLI:

C:\WINDOWS\system32>set id = "Goldeneye 007.v64" & "C:\script.cmd"
6

1 Answer

You can use batch arguments and pass the value of the ROM to the call of the batch script when you execute it such as C:\script.cmd "<Value Argument>".

Rather than using set /p id="Enter Rom: " use set id=%~1 and that's all that is needed to allow the first argument passed to the script to be used for the value to set the id variable.

Batch Script Example

@ECHO OFF

set id=%~1

start /d "G:\Emulators\N64\Project64" Project64.exe "G:\Emulators\N64\Roms\%id%"
Sarah Jenkins
Author

Sarah Jenkins

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.