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

查看:49
本文介绍了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 的输出.呸!所以,我尝试回显 !a! (结果:ECHO 关闭.);我试过回显 %a (result: 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 代码:从文本读取第一行的 Windows 批处理命令文件
除了而不是回显每一行 - 我只是回显找到的最后一行.并不像人们想象的那么简单.我注意到我的计数器由于某种原因保持在零;我想知道 set/a counter+=1 是否在做我认为它在做的事情.

I figured the easy thing to do would be to modify the head.bat code found here: Windows batch command(s) 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...

Lee,您关于%%a"为什么不在 for 循环之外工作的推理是正确的.%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 语句.看看下面的代码:

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"').

The first thing I did was change (c:file_list.txt).to ('findstr /n .* "c:file_list.txt"').

  • 'findstr/n .* "PATHFILENAME"' 读取文件并将行号 ('/n') 添加到每一行 ('/n')>.*' 是匹配0 个或多个"任何字符的正则表达式).由于现在每一行的开头都有一个行号(即使是空行),for 循环不会跳过任何行.
  • 'findstr /n .* "PATHFILENAME"' 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

接下来,我们使用 "tokens=1* delims=:" 来拆分行号和内容.

Next, we use "tokens=1* delims=:" to break up the line number and the content.

  • 'tokens=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存储当前行内容:if "%%a" =="%1" set line=%%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",因为上面的代码消除了在 for 循环中间读取计数器变量.

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 %line%"更改为echo.%line%".这将正确显示空行,而不是ECHO is off.".已更改 ' 输入 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,我想我已经解决了所有特殊字符问题.试一试:

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天全站免登陆