0

I am wanting to set a variable one line at a time from a text file, run a particular command using this variable, then loop back, grab the second line, process the command, and so on until the file ends.

The batch file works well and asks for user input, then processes using the input as the variable with no problems at all. The script gets the computer name first, then copies the install files to the local machine the uses WMIC to install the MSI:

@echo off
SET /P computer=Enter Computer Name
echo.
XCopy "\\server\netlogon\temp\*.*" "\\%computer%\c$\temp\*.*" /E /C /H /R /Y 
WMIC /Node:"%computer%" product call install true,"","c:\temp\msi_to_install.msi"

The text file will look something like this:

PC-01
PC-02
PC-27
Odd-PC

However, when I try to introduce a "for" loop, so that I can process a hundred or more machines one at a time, nothing happens. Even if I have only one line in the file I cannot get it to work. This is what I have tried:

@echo off
For /F %%i in (c:\test.txt) do (
set computer=%%i
echo.
XCopy "\\server\netlogon\temp\*.*" "\\%computer%\c$\temp\*.*" /E /C /H /R /Y 
WMIC /Node:"%computer%" product call install true,"","c:\temp\msi_to_install.msi"
)

For the record I have tried the variables with %%computer%% and !computer! with no success.

I have also tried the following also with or without the /p in the "SET" command:

@echo off
SET /P computer=<"c:\test.txt"
echo.
XCopy "\\server\netlogon\temp\*.*" "\\%computer%\c$\temp\*.*" /E /C /H /R /Y 
WMIC /Node:"%computer%" product call install true,"","c:\temp\msi_to_install.msi"

I would appreciate any suggestions or input.

2 Answers 2

1

You'd need to reference some of the SO articles on delayed expansion for details about how it works.

In order to use the !var! syntax (which would work in place of %var% in your first for loop) you need to have executed

setlocal enabledelayedexpansion

earlier in the batch (as part of the mainline, not the loop - for instance, directly after the @echo off or before the for loop)

However, in your case, since computer is set to %%i, replacing %computer% with %%i should be an easier solution.


Expected resultant code:

@echo off
setlocal enabledelayedexpansion
For /F %%i in (c:\test.txt) do (
set computer=%%i
echo.
XCopy "\\server\netlogon\temp\*.*" "\\!computer!\c$\temp\*.*" /E /C /H /R /Y 
WMIC /Node:"!computer!" product call install true,"","c:\temp\msi_to_install.msi"
)

or

@echo off
For /F %%i in (c:\test.txt) do (
echo.
XCopy "\\server\netlogon\temp\*.*" "\\%%i\c$\temp\*.*" /E /C /H /R /Y 
WMIC /Node:"%%i" product call install true,"","c:\temp\msi_to_install.msi"
)
Sign up to request clarification or add additional context in comments.

6 Comments

OK thanks for that. Now I get an interesting error: Invalid drive specification However, running my initial script example against the same machines listed in my text file works as expected.
I've removed a redundant set from the second script. You haven't indicated which script you are using, nor whether this error is appearing on some or all data lines from test.txt. I would change xcopy to echo xcopy and wmic to echo wmic and see whether the expected instructions are reported - and if not, is there something common to (any) failures?
Both examples were used and I no longer get the error because I had inadvertently erased a line. When I run this now, nothing happens at all, I am back at a prompt within a second and no error message at all. I am truly stumped on this one.
I had assumed you were running this directly from the prompt. If you run directly by clicking it, you need to append a pause command to the end of the batch to observe any reports that may appear.
My apologies, this is a batch file so that it is easier for others to run and so that I can ensure there are no mistakes in the syntax. When running direct from the command line I get %%i was not expected at this time. Tried my batch file again and whether appending pause or timeout \t 60 makes no difference to the batch file execution.
|
0

After formatting your input file and saving it in ANSI format, this is the final code that works:

@echo off
setlocal enabledelayedexpansion
For /F %%i in (c:\computers.txt) do (
set computer=%%i
echo.
XCopy "\\server\netlogon\temp\msifolder\*.*" "\\!computer!\c$\temp\msifolder\*.*" /E /C /H /R /Y 
WMIC /Node:"!computer!" product call install true,"","c:\temp\msi_to_install.msi"
RD /S /Q "\\!computer!\c$\temp\msifolder\"
)

In my final rendition of the script, I am copying the folder the MSI file is located in locally, installing the MSI, then deleting the folder after completion. If the MSI is already installed you will get this error:

instance of __PARAMETERS
{
ReturnValue = 1603

For a successful install you will get a ReturnValue = 0

@magoo provided a lot of direction in troubleshooting, thank you, and the file format was the final piece of the puzzle.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.