简体   繁体   English

将TXT文件解析为批处理文件

[英]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: 该脚本首先获取计算机名称,然后使用WMIC安装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. 但是,当我尝试引入“ for”循环时,一次只能处理一百台或更多台计算机,因此什么也没有发生。 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! 为了记录,我尝试使用%%computer%%!computer!变量!computer! with no success. 没有成功。

I have also tried the following also with or without the /p in the "SET" command: 我也尝试过以下命令,无论是否在“ SET”命令中使用/ p:

@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. 您需要参考有关delayed expansion一些SO文章,以了解其工作原理的详细信息。

In order to use the !var! 为了使用!var! syntax (which would work in place of %var% in your first for loop) you need to have executed 您需要执行的语法(可以在第一个for循环中代替%var%语法)

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) 批处理中的较早时间(作为主线的一部分,而不是循环-例如,在@echo offfor循环之前)

However, in your case, since computer is set to %%i , replacing %computer% with %%i should be an easier solution. 然而,在你的情况下,由于computer设置为%%i ,更换%computer%%%i应该是一个简单的解决方案。


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: 格式化输入文件并将其保存为ANSI格式后,这是可以工作的最终代码:

@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. 在脚本的最终版本中,我将复制MSI文件位于本地的文件夹,安装MSI,然后在完成后删除该文件夹。 If the MSI is already installed you will get this error: 如果已经安装了MSI,则会出现此错误:

instance of __PARAMETERS
{
ReturnValue = 1603

For a successful install you will get a ReturnValue = 0 对于成功的安装,您将获得ReturnValue = 0

@magoo provided a lot of direction in troubleshooting, thank you, and the file format was the final piece of the puzzle. @magoo在故障排除方面提供了很多指导,谢谢,文件格式是最后的难题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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