将 .txt 逐行读入批处理变量 [英] Read .txt line by line into batch variables

查看:45
本文介绍了将 .txt 逐行读入批处理变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理脚本,可以向某些 PC 发送 ping.可用的 PC 被写入一个文本文件,因此每台 PC 都站在自己的行中.另一个批处理脚本将一些文件复制到 PC(目前它尝试复制到所有 PC).

I have a batch script which sends pings to some PCs. The available PCs are written into a text file, so each PC stands in its own line. Another batch script copies some files to the PCs (at the moment it tries to copy to all PCs).

现在我想修改脚本,以便批处理读出文本文件并只复制到可用的 PC 上……但是如何?
它应该逐行读取文本文件的写入行并检查 EOF.

Now I want to modify the script(s) so the batch reads out the text file and only copies to the available PCs … but how?
It should read the written lines of the text file line by line and check for EOF.

推荐答案

我不确定你想要什么,但根据你的问题逐行读取 .txt 并将其放入变量",这可能对您有用.

I'm not sure what do you want, but according to your question "Read .txt line by line and put it into variables", this might be useful for you.

@echo off
setlocal enabledelayedexpansion

set count=0

for /f "tokens=*" %%x in (textfile.txt) do (
    set /a count+=1
    set var[!count!]=%%x
)
echo %var[4]%
pause >nul

echo %var[4]% 将打印出 textfile.txt(或任何文本文件)中的第 4 行.

echo %var[4]% will print out the 4th line in textfile.txt (or any text file).

这篇关于将 .txt 逐行读入批处理变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆