7
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ファイルの行数をカウントして変数に入れるワンライナーbat

Posted at

Assign number of lines in a file to a variable with one line command on Windows.

探したけど無かったので書きました。
しばらくしたらbash使うのかな。とりま置きメモ。

countLine.bat
FOR /f "delims=: tokens=2" %%A IN ('FIND filename.ext /c /v ""') DO SET /A LINES=%%A

FIND /c /v "" はファイル最後の空行はカウントしないのでそれで良ければ使えます。ダメな場合は以下

countLineFull.bat
FOR /f %%A IN ('TYPE filename.ext ^| FIND /c /v ""') DO SET /A LINES=%%A

2つ目のカウント、遅くなりませんか?
最後の空行はむしろ数え上げない方がいいときもあるので両方置いときます。

7
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?