简体   繁体   中英

Parse TXT file to Batch file

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.

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"
)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM