10

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
Cœur
  • 37,241
  • 25
  • 195
  • 267
spassen
  • 1,550
  • 8
  • 20
  • 32

1 Answers1

12

%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%%

dbenham
  • 127,446
  • 28
  • 251
  • 390
  • Turns 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. – spassen Jul 27 '12 at 16:55