Windows批处理文件来回应特定的行号 [英] Windows Batch file to echo a specific line number

查看:170
本文介绍了Windows批处理文件来回应特定的行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我目前的困境的第二部分,我夹在列表C:\\ file_list.txt 。我需要根据行号,因为这个批处理脚本是由一个反复的过程宏调用可以提取它们(当然,他们呼应了一些MODS)。我传球的线路号作为参数

So for the second part of my current dilemma, I have a list of folders in c:\file_list.txt. I need to be able to extract them (well, echo them with some mods) based on the line number because this batch script is being called by an iterative macro process. I'm passing the line number as a parameter.

@echo off
setlocal enabledelayedexpansion
set /a counter=0
set /a %%a = ""
for /f "usebackq delims=" %%a in (c:\file_list.txt) do (
   if "!counter!"=="%1" goto :printme & set /a counter+=1
)
:printme
echo %%a

这给了我的输出一个%。卫生署!所以,我已经试过呼应​​ A :;!(结果 ECHO处于关闭状态)。我试过呼应%A (结果:A)

which gives me an output of %a. Doh! So, I've tried echoing !a! (result: ECHO is off.); I've tried echoing %a (result: a)

我想通容易的事会修改 head.bat code在这里找到:
<一href=\"http://stackoverflow.com/questions/130116/dos-batch-commands-to-read-first-line-from-text-file\">http://stackoverflow.com/questions/130116/dos-batch-commands-to-read-first-line-from-text-file

除了不附和每一行 - 我只希望找到呼应的最后一行。人们想象的那么简单。我注意到,我的柜台是停留在零由于某种原因;我想知道,如果设定/计数器+ = 1 是做什么的,我认为它在做什么。

I figured the easy thing to do would be to modify the head.bat code found here: http://stackoverflow.com/questions/130116/dos-batch-commands-to-read-first-line-from-text-file
except rather than echoing every line - I'd just echo the last line found. Not as simple as one might think. I've noticed that my counter is staying at zero for some reason; I'm wondering if the set /a counter+=1 is doing what I think it's doing.

推荐答案

我知道这是一个老问题,但在这里是一个任何人一些额外的信息有一个类似的问题...

I know this is an old question, but here is some additional info for anyone with a similar issue...

李,你为什么%%一个不工作外循环推理是正确的。在%A-Z和%A-Z变量(%%一个批处理文件中A-Z)是的for循环结构,不存在在它之外。

Lee, your reasoning on why "%%a" isn't working outside the for loop is correct. The %a-z and %A-Z variables (%%a-z inside a batch file) are a construct of the for loop, and do not exist outside of it.

我想推荐一个替代的解决这个问题相匹配的正确的行号(无空行跳过),并且不需要延迟扩展,柜台,或goto语句。看看下面code:

I would like to recommend an alternative solution to this problem that matches the correct line numbers (no empty lines skipped) and does not require delayed expansion, counters, or a goto statement. Take a look at the following code:

@echo off
for /f "tokens=1* delims=:" %%a in ('findstr /n .* "c:\file_list.txt"') do if "%%a"=="%1" set line=%%b
echo.%line%

下面是什么导致我到上述变化。比方说,你有以下文件的内容:

Here is what led me to the above changes. Let's say you had the following file contents:

Some text on line 1
Blah blah blah
More text

我做的第一件事情是变化的(C:\\ file_list.txt)。要('FINDSTR / N *C:\\ file_list.txt。')


  • FINDSTR / N。*路径\\文件名'读取该文件,并添加行号( / N ),以每行('<强>。* 是一个普通的前pression匹配任何字符0个或多个)。因为每行现在将在开始时(即使在空的)的线数,没有线将由循环跳过。

  • 'findstr /n .* "PATH\FILENAME"' reads the file and adds a line number ('/n') to every line ('.*' is a regular expression matching "0 or more" of any character). Since every line will now have a line number at the beginning (even the empty ones), no lines will be skipped by the for loop.

每个行现在看起来像这里面的for循环:

Each line will now look like this inside the for loop:

1:Some text on line 1
2:Blah blah blah
3:More text

接下来,我们使用令牌= 1 * delims =:打破了行号和内容


  • 标记= 1 * 设置第一个令牌(存储在 %% A ),以分隔符之前的所有内容,而第二个令牌(存储在 %% b )之后的一切。

  • delims =:设置。作为分隔符使用,打破了字符串

  • 'tokens=1*' sets the first token (stored in %%a) to everything before the delimiter, and the second token (stored in %%b) to everything after it.
  • 'delims=:' sets ":" as the delimiter character used to break up the string.

现在,我们遍历文件, %% A 将返回当前的行号和 %% b 将返回该行的内容。

Now as we loop through the file, %%a will return the current line number and %%b will return the content of that line.

所有剩下的就是到%1 参数比较 %% A (而不是一个计数器变量),并使用 %% b 存储当前行的内容:如果%%一个==%1设置行= %% b

All that's left is to compare the %1 parameter to %%a (instead of a counter variable) and use %%b to store the current line content: if "%%a" =="%1" set line=%%b.

另外一个好处是, enabledelayedexpansion 不再是必要的,因为上面的code消除的中间为循环读取计数器变量。

An added bonus is that 'enabledelayedexpansion' is no longer necessary, since the above code eliminates reading a counter variable in the middle of a for loop.

修改:'回声%的线路% 修改回声%的线路%来。这将正确显示现在的空行,而不是ECHO处于关闭状态。改变了键入c:\\ file_list.txt ^ | FINDSTR / N * '到' FINDSTR / N *C:\\ file_list.txt。,因为在 FINDSTR 命令已经可以直接读取文件

changed 'echo %line%' to 'echo.%line%'. This will correctly display blank lines now, instead of "ECHO is off.". Changed 'type c:\file_list.txt ^| findstr /n .*' to 'findstr /n .* "c:\file_list.txt"', since the findstr command can already read files directly.

杰布,我想我已经解决了所有特殊字符的问题。给这一个镜头:

Jeb, I think I've solved all the special character issues. Give this a shot:

for /f "tokens=*" %%a in ('findstr /n .* "c:\file_list.txt"') do (
  set "FullLine=%%a"
  for /f "tokens=1* delims=:" %%b in ("%%a") do (
    setlocal enabledelayedexpansion
    set "LineData=!FullLine:*:=!"
    if "%%b" equ "%1" echo(!LineData!
    endlocal
  )
)

这篇关于Windows批处理文件来回应特定的行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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