Running variable string as command in batch scripting
%command%
will not work because it is expanded at parse time, so the expanded value is the value of command prior to the loop executing.
I don't know why !command!
does not work. Normally you want to use normal expansion instead of delayed expansion when executing code in a variable because delayed expansion limits some of the operations you can do. It has to do with how the CMD parser works. But I don't see anything in your command that should cause problems with delayed expansion.
Try call %%command%%
spassen
Updated on July 09, 2022Comments
-
spassen almost 2 years
EDIT: There was nothing wrong with the code below. The error was coming from elsewhere.
The command variable is the command I want to execute. The name variable is pulling a list of computer names. When I echo !command! it returns the value I want to use. That should run the command needed to delete all of the machines, however, when I actually run !command! or %command%, the name variable isn't added and it fails.
d: cd "Program Files\admin" setlocal EnableDelayedExpansion SET string=% for /f "tokens=*" %%a in (oldMachines.txt) do ( set name=%%a set command=sbadmcl.exe AdminUser:admin -AdminPwd:password -Command:DeleteMachine -Machine:!name! REM echo !name! REM echo !command! REM !command! %command% ) pause
-
spassen almost 12 yearsTurns out there was an issue running it against the program I'm using. The code is right but the program isn't allowing it. Your answer was helpful though, thanks.