batch-filecmd

How to run a script and enter an argument before running it


I'd like to know how can I run a script in cmd and enter the arguments before it even runs, I tried googling it but I don't know how to describe it. Anyways, this is what I'm trying to do

C:> script.bat 2 4

so basically run the script with 2 and 4 as variables and be able to use them inside the script for different actions. Any help would be great, thanks!


Solution

  • script.bat 2 4

    The values you give are Arguments but internally are Parameters %1 to %9 so it's very common and easily done to mix the terms. (Parameter %0 is usually the script name so Script.Bat with its path is often used as %~dp0)

    @echo off
    set "arg1=%1"
    if "%2"=="" echo Silly Billy I need 2 arguments & pause & exit
    set "arg2=%2"
    echo Parameter 1 is =%arg1%
    echo Parameter 2 is =%arg2%
    pause and continue
    

    enter image description here

    enter image description here