为什么要写一个新文件的行号?

时间:2014-07-28 16:48:22

标签: string batch-file file-io

这基本上将文件作为输入(%textfile%)并逐步遍历每一行,用%replace%替换%search%的任何实例。它运作良好,但它正在编写行号。请参阅代码下面的示例i / o。

我在别处找到了这段代码。我认为我在批处理脚本方面相当不错,但我不熟悉什么!正在做。

(for /f "delims=" %%i in ('findstr /n "^" "%textfile%"') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:%search%=%replace%!"
    echo(!line!
    endlocal
))>"%newfile%"

输入文件:

:host {
  display: block;
}

/* To ensure your styling works under the Shadow DOM polyfill, see
   www.polymer-project.org/docs/polymer/styling.html#directives */
polyfill-next-selector { content: ':host > h2'; }
::content h2 {
  color: blue;
}

输出文件:

1::host {
2:  display: block;
3:}
4:
5:/* To ensure your styling works under the Shadow DOM polyfill, see
6:   www.polymer-project.org/docs/polymer/styling.html#directives */
7:polyfill-next-selector { content: ':host > h2'; }
8:::content h2 {
9:  color: blue;
10:}

1 个答案:

答案 0 :(得分:1)

这是原因。来自findstr /?

/N         Prints the line number before each line that matches.

你的命令有:

'findstr /n "^" "%textfile%"'