How to pass a command as a command line argument to a batch file

I want to pass a command as a command line argument from one batch file to another.

eg.

first.bat:

call test.bat "echo hello world" "echo welcome "

test.bat:

set initialcommand=%1

set maincommand=%2

%maincommand%

%initialcommand%
+3
source share
1 answer

Here is what you need:

first.cmd:

@echo off
set maincommand=echo hello world!
call test.cmd %maincommand%

test.cmd:

@echo off
%*

In this case, it first.cmdpasses a valid command (your example just passed the string constant "maincommand", not its value).

In addition, it test.cmdexecutes a command consisting of each parameter, and not just the first.

When you create these two files and execute first.cmd, you get:

hello world!

as was expected.

+4
source

Source: https://habr.com/ru/post/1746512/


All Articles