简体   繁体   English

要读取的批处理文件

[英]Batch File To Read

Can someone help me?有人能帮我吗? I needed a BAT that read my TXT file containing the information:我需要一个 BAT 来读取包含信息的 TXT 文件:

File.txt
======================
SAGEM BETA PORT (COM1)
SAGEM TELIUM COMM PORT (COM2)
MAGIC COMM (COM1)
======================

And I would always find the device "SAGEM TELIUM COMM PORT" that will be in the file and inform me of a variable which COM is connected to (Ex: COM2).而且我总是会在文件中找到设备“SAGEM TELIUM COMM PORT”,并通知我 COM 连接到的变量(例如:COM2)。 It's possible?这是可能的?

To read a file in a batch file you can use the FOR /F command.要读取批处理文件中的文件,您可以使用FOR /F命令。 But in your instance you also need to find a specific line within the input file.但是在您的实例中,您还需要在输入文件中找到特定的行。 Luckily the FOR /F command can also parse the output of console commands as well.幸运的是FOR /F命令也可以解析控制台命令的输出。 So you can use the FIND command with your search string to find the correct line.因此,您可以使用带有搜索字符串的FIND命令来查找正确的行。 The FOR /F command allows you split up a line by defining delimiters. FOR /F命令允许您通过定义分隔符来拆分一行。 In this case it will be the parentheses.在这种情况下,它将是括号。 In your example you would want the FOR /f command to grab the 2nd delimited part of your data.在您的示例中,您希望FOR /f命令获取数据的第二个分隔部分。

@echo off
FOR /F "tokens=2 delims=()" %%G IN ('find "SAGEM TELIUM COMM PORT" file.txt') do echo %%G

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

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