如何使用批处理脚本将文件内容的输出存储在变量中

发布于 2025-01-19 04:00:42 字数 492 浏览 2 评论 0原文

我的文件名为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你如我软肋 2025-01-26 04:00:42
for /f "tokens=1,2delims=: " %%a in ('findstr /n . %file%') do set "name[%%a]=%%b"
set name

请参阅 for /? 文档和 findstr /? 文档。

findstr 查找包含字符的所有行(默认为提供的正则表达式 = .),并以 serial 格式对它们进行编号 /n :行内容。使用 :space 作为分隔符,标记 1 成为行号,2 成为后面的第一个单词,直到空格。

从各个部分构建变量并显示结果。

for /f "tokens=1,2delims=: " %%a in ('findstr /n . %file%') do set "name[%%a]=%%b"
set name

See for /? documentation and findstr /? documentation.

findstr finds all lines that contain a character (default is regular expression provided = .) and /n numbers them in the format serial: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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文