简体   繁体   中英

Batch File To Read

Can someone help me? I needed a BAT that read my TXT file containing the information:

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). It's possible?

To read a file in a batch file you can use the FOR /F command. 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. So you can use the FIND command with your search string to find the correct line. The FOR /F command allows you split up a line by defining delimiters. 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.

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

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