LoginSignup
1
1

More than 5 years have passed since last update.

コマンドプロンプト・プログラミング 々

Last updated at Posted at 2018-12-03

目次

ユーティリティ

前回作ったユーティリティを修正した。

  • token -> words
  • line -> -conc

words:トークンを二重引用符付きで得る

words.cmd
@echo off
for %%w in (%*) do (
  if "%%~w" == "" goto :eof
  echo "%%~w"
)
exit /b
cmd.exe
> words

> words a,b,c; one=1 two=2 three=3
"a"
"b"
"c"
"one"
"1"
"two"
"2"
"three"
"3"

> words   Hello   World!   "   yeah!"
"Hello"
"World!"
"   yeah!"

cmd.exe のデリミタを回避するには二重引用符が欠かせないので、
二重引用符付きで出力することにした。

-conc:出力を一行にまとめる

-conc.cmd
@echo off
setLocal
  set line=
  for /f "delims=" %%l in ('find /v ""') do (
    call :join-space "%%~l"
  )
  if "%line%" == "" goto :eof
  echo %line%
endLocal
exit /b

:join-space
  if "%line%" == "" (
    set line=%~1
  ) else (
    set line=%line% %~1
  )
  exit /b
cmd.exe
> words | -conc

> words a,b,c; "one=1 two=2 three=3" | -conc
a b c one=1 two=2 three=3

> words   Hello   World!   "   yeah!" | -conc
Hello World!   yeah!

パイプから拾うことを強調したくて -conc- を前置した。

puts:標準出力

puts.cmd
@echo off
call words %* | call -conc
exit /b
cmd.exe
> puts

> puts 1,2,3,4,5
1 2 3 4 5

> puts Hello World! "   bye..."
Hello World!   bye...

これにて nix の echo ライクになった。

for の挙動

nix の head tail を作ろうとしたのだが、

cmd.exe
> type sample.txt
1
2

4
5

> type sample.txt |^
More? for /f "delims=" %l in ('find /v ""') do @echo %l
1
2
4
5

改行のみの行を捨てやがる。。。

次回

for の呪縛から逃れる術が見つかればなー。

1
1
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
1
1