如何使用批处理脚本将文件内容的输出存储在变量中
我的文件名为abc.txt带有内容:
Ryan is an engineer.
Nikola is a data engineer.
Mike is doctor.
现在,我创建了一个将显示文件内容的脚本:
@echo off
set /p file="Please enter file Name: "
for /f "tokens=1" %%a in (%file%) do (echo %%a)
pause
我正在获取输出:
Ryan
Nikola
Mike
现在,我想要的是我想存储或将Ryan分配给一个变量,Nikola到一个变量,将Mike到一个变量,以便我可以将这些变量用于进一步的操作。
那么,有人可以帮我吗? 提前致谢。
注意:我希望这件事纯粹是使用PowerShell或任何其他工具的脚本实现的。
I have file named abc.txt with content:
Ryan is an engineer.
Nikola is a data engineer.
Mike is doctor.
Now I have created one script which will display the content of file:
@echo off
set /p file="Please enter file Name: "
for /f "tokens=1" %%a in (%file%) do (echo %%a)
pause
And I am getting output as:
Ryan
Nikola
Mike
Now, what I want is that I want to store or assign Ryan to one variable, Nikola to one variable and Mike to one variable so that I can use those variable for my further operations.
So, can someone please help me with this?
Thanks in advance.
Note: I want this thing to be purely achieve using script not by powershell or any other tool.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅
for /?
文档和findstr /?
文档。findstr
查找包含字符的所有行(默认为提供的正则表达式 =.
),并以serial 格式对它们进行编号
。使用/n
:行内容:
和 space 作为分隔符,标记 1 成为行号,2 成为后面的第一个单词,直到空格。从各个部分构建变量并显示结果。
See
for /?
documentation andfindstr /?
documentation.findstr
finds all lines that contain a character (default is regular expression provided =.
) and/n
numbers them in the formatserial:linecontents
. Using:
and space as delimiters, token 1 becomes the line number and 2 the first word after, up to the space.Build the variable from the parts and show result.