批处理文件-读取特定行,并将该行中的特定字符串另存为变量 [英] Batch File - Read specific line, and save a specific string in that line as a variable

查看:114
本文介绍了批处理文件-读取特定行,并将该行中的特定字符串另存为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过/f循环(或其他任何方式)读取特定行?

Is there any way to get for /f loop (or anything else) to read a specific line?

这是我到目前为止的代码,它读取每一行的第一个单词.

Here is the code I have so far, it reads first word of every line.

@echo off
set file=readtest.txt
for /f "tokens=1 delims= " %%A in (%file%) do (echo %%A)
pause

如果有人可以指出我正确的方向,那将不胜感激.

If someone can point me in the right direction, it'd be much appreciated.

谢谢

其他信息:我想制作一个批处理文件,它将一个TXT文件重命名为该TXT文件中位于特定位置的字符串.我已经弄清楚了如何重命名文件,我需要学习的就是在文件中检索一个字符串(位于特定位置),该字符串将成为该TXT文件的名称.

Additional Information: I want to make a batch file which will rename a TXT file to a string within that TXT file, located at a specific location. I have figured out how to rename files, all I need to learn to do is to retrieve a string (located at a specific location) with in the file which will go into the name of that TXT file.

推荐答案

由于您尚未完全定义特定位置"的含义,因此我将做出一些(在我看来是合理的)假设,尽管无论您的定义是什么,我介绍的方法同样有效.

Since you haven't fully defined what you mean by "a specific location", I'll make some (reasonable, in my opinion) assumptions, though the method I present is equally valid no matter what your definition turns out to be.

通过将行计数器变量与tokens结合使用,可以在该行上获得任意行和任意单词.

You can get arbitrary lines and arbitrary words on that line by using a line counter variable in conjunction with tokens.

假设您的文本文件名可以作为infile.txt文件第四行的第二个参数找到.您可以通过以下方式获得该信息:

Let's assume your text file name can be found as the second argument on the fourth line of the infile.txt file. You can get that with something like:

@setlocal enableextensions enabledelayedexpansion
@echo off
set /a "line = 0"
for /f "tokens=2 delims= " %%a in (infile.txt) do (
    set /a "line = line + 1"
    if !line!==4 set thing=%%a
)
endlocal & set thing=%thing%
echo %thing%

这实际上使用了一些技巧",值得进一步说明:

This actually uses a few "tricks" which warrant further explanation:

  • line计数器可确保仅从特定行中获取所需内容,尽管您可以将测试!line!==4更改为所需的内容,例如以#开头的行,第五行包含字符串xyzzy,依此类推.
  • 使用setlocal/endlocal可以有效地为您提供一个变量不能泄漏的范围.即使对于通常与此类事物通常不相关的语言,这也是一种良好的编程习惯:-)
  • 使用endlocal & set绕过该范围的 ,以便thing确实泄漏的唯一内容(应该如此).
  • 使用延迟扩展和!..!变量来确保它们在for循环中是正确的.否则,%..%将始终扩展为for循环启动时设置的值.
  • the line counter to ensure you only grab what you want from a specific line, though you could change the test !line!==4 into anything you need such as a line beginning with #, the fifth line containing the string xyzzy and so on.
  • the use of setlocal/endlocal to effectively give you a scope from which variables cannot leak. This is good programming practice even for a language often not normally associated with such things :-)
  • the use of endlocal & set to bypass that scope so that thing is the only thing that does actually leak (as it should).
  • the use of delayed expansion and !..! variables to ensure they're correct within the for loop. Without this, the %..% will always be expand to the value they were set to when the for loop started.

最后两个要点实际上是相关的. %..%变量在 read 而不是在执行时扩展.

Those last two bullet points are actually related. %..% variables are expanded when the command is read rather than when it is executed.

对于for循环,该命令是从for到最后一个)的整个内容.这就是说,如果您在循环中使用%line%,则会在循环运行前 求值,这将导致循环始终为0(变量本身可能会发生变化,但它已经发生了).但是,!line!将在每次在循环中遇到时进行评估,因此将具有正确的值.

For a for loop, the command is the entire thing from the for to the final ). That means, if you use %line% within the loop, that will be evaluated before the loop starts running, which will result in it always being 0 (the variable itself may change but the expansion of it has already happened). However, !line! will be evaluated each time it is encountered within the loop so will have the correct value.

类似地,虽然endlocal通常会清除setlocal之后创建的所有变量,但是命令:

Similarly, while endlocal would normally clear out all variables created after the setlocal, the command:

endlocal & set thing=%thing%

是扩展上下文中的单个命令. %thing%在运行 endlocal之前被展开,这意味着它实际上变为:

is a single command in the context of expansion. The %thing% is expanded before endlocal is run, meaning it effectively becomes:

endlocal & set thing=whatever_thing_was_set_to_before_endlocal

这就是为什么使用setlocalendlocal & set是限制变量从范围转义"的非常有用的方法的原因.而且,是的,您可以链接多个& set节以允许更多变量逃脱作用域.

That's why the use of setlocal and endlocal & set is a very useful way to limit variables "escaping" from a scope. And, yes, you can chain multiple & set stanzas to allow more variables to escape the scope.

这篇关于批处理文件-读取特定行,并将该行中的特定字符串另存为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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