简体   繁体   English

批处理文件-读取特定行,并将该行中的特定字符串另存为变量

[英]Batch File - Read specific line, and save a specific string in that line as a variable

Is there any way to get for /f loop (or anything else) to read a specific line? 有什么方法可以让/ f循环(或其他任何方式)读取特定行?

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. 如果有人能指出我正确的方向,将不胜感激。

Thanks 谢谢

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

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. 由于您尚未完全定义“特定位置”的含义,因此我将做出一些假设(我认为是合理的),尽管无论您的定义是什么,我提出的方法都同样有效。

You can get arbitrary lines and arbitrary words on that line by using a line counter variable in conjunction with tokens . 通过将行计数器变量与tokens结合使用,可以在该行上获得任意行和任意单词。

Let's assume your text file name can be found as the second argument on the fourth line of the infile.txt file. 假设您的文本文件名可以作为infile.txt文件第四行的第二个参数找到。 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: 这实际上使用了一些“技巧”,值得进一步说明:

  • 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. line计数器,以确保仅从特定行中获取所需内容,尽管您可以将test !line!==4更改为所需的内容,例如以#开头的行,包含字符串xyzzy的第五行,依此类推。
  • the use of setlocal/endlocal to effectively give you a scope from which variables cannot leak. 使用setlocal/endlocal可以有效地为您提供一个变量不能泄漏的范围。 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). 使用endlocal & set ,以绕过范围,使thing是, 居然漏(因为它应该)的唯一的事情。
  • the use of delayed expansion and !..! 使用延迟扩展和!..! variables to ensure they're correct within the for loop. 变量以确保它们在for循环中正确无误。 Without this, the %..% will always be expand to the value they were set to when the for loop started. 如果没有此设置, %..%将始终扩展为for循环启动时设置的值。

Those last two bullet points are actually related. 最后两个要点实际上是相关的。 %..% variables are expanded when the command is read rather than when it is executed. 读取命令而不是执行命令时, %..%变量将被扩展。

For a for loop, the command is the entire thing from the for to the final ) . 对于for循环,该命令是从整个事情for到最后) 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). 这意味着,如果在循环中使用%line% ,则会在循环开始运行之前对其进行评估,这将导致该值始终为0 (变量本身可能会更改,但扩展已经发生)。 However, !line! 但是, !line! will be evaluated each time it is encountered within the loop so will have the correct value. 会在循环中每次遇到时进行评估,因此将具有正确的值。

Similarly, while endlocal would normally clear out all variables created after the setlocal , the command: 同样,虽然endlocal通常会清除在setlocal之后创建的所有变量,该命令:

endlocal & set thing=%thing%

is a single command in the context of expansion. 是扩展上下文中的单个命令。 The %thing% is expanded before endlocal is run, meaning it effectively becomes: %thing% 运行endlocal 之前已展开,这意味着它实际上变为:

endlocal & set thing=whatever_thing_was_set_to_before_endlocal

That's why the use of setlocal and endlocal & set is a very useful way to limit variables "escaping" from a scope. 这就是使用setlocalendlocal & set来限制变量从范围“转义”的非常有用的原因。 And, yes, you can chain multiple & set stanzas to allow more variables to escape the scope. 而且,是的,您可以链接多个& set节以允许更多变量逃脱作用域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM