LoginSignup
29
28

More than 5 years have passed since last update.

.batで自分がよく使うコマンド集(2016/2/5更新)

Last updated at Posted at 2015-09-02

バッチファイルで長いプログラムを書く際に自分が多用しているコマンドラインを片っ端から載せています。

もしかしたらお目当てのコマンドが見つかるかも?

※ネット上で見つけた便利そうなやつを自分流に書き換えて掲載していることがあります。
※自分は基本コピペして使い変更を加えることは無いので、可読性は皆無です。
 用途に合わせて書き換える必要がある際は、頑張って解読するかコメントで言ってください。
※基本的に、ラベルをcallして使うようになっています。引数なども多用しているので、可読性は(略


追加
15/9/5
①選択フォルダ内に存在する全ての空フォルダを削除

15/12/01
①コマンドの出力を非表示にする方法
②()内で変数を2重に展開する方法

15/12/16
①横に伸びる棒グラフ(ゲージ?)を作る
②現在時刻をミリ秒で取得を更新

15/12/28
①現在時刻をミリ秒で取得を更新

16/1/24
①現在時刻を利用して任意の範囲の時間を計測する方法を別のページに挙げたので、それを紹介
②変数の内容をシフトする方法を別のページに挙げたので、それを紹介

16/2/3
①任意の範囲の時間の時間計測方法のコマンドが微妙に間違っていたので修正
②1~nまでの特定の配列をシャッフルするを更新

16/2/5
①1~nまでの特定の配列をシャッフルするを更新
(fvh_Pさんからの情報提供により)



現在時刻をミリ秒で取得

使用法:call :gettime [変数名]

15/12/28更新版
:gettime
for /f "tokens=1,2,3,4 delims=:." %%a in ("%time%") do set /a %1=%%a0*36000+1%%b*6000+1%%c*100+1%%d-610100
exit /b
15/12/16更新版
:gettime
for /f "tokens=1,2,3,4 delims=:." %%a in ("%time%") do (
    set /a hour=%%a0,min=1%%b,sec=1%%c,ms=1%%d
    set /a hour/=10,min-=100,sec-=100,ms-=100,%1=hour*360000+min*6000+sec*100+ms
)
exit /b
更新前
:gettime
for /f "tokens=1,2,3,4 delims=:." %%a in ("%time%") do (
set /a hour=%%a,min=1%%b,sec=1%%c,ms=1%%d&if !hour! leq 9 (set hour=10!hour!) else (set hour=1!hour!)
set /a hour-=100,min-=100,sec-=100,ms-=100,%1=hour*360000+min*6000+sec*100+ms
)
exit /b

GUI風カーソルセレクト画面

 6つのラベルを使います。やばそう

 改良版をこちらに投稿しました。
【自分用】バッチファイルで選択肢をGUI表示するときのテンプレ

使用法:call :cini
 カーソルの位置をリセットするときに。

:cini
set currentcursor1=1
set currentcursor2=1
exit /b

使用法:call :cset [行数] [列数]
 カーソルを動かす縦横の範囲を指定します。
 選択肢を2*2で表示するなら、call :cset 2 2
 選択肢を4*1(縦に4つ)で表示するなら、call :cset 4(列を指定しないと1になるので省略可能)

:cset
set int1=%1
set int2=%2
if "%2" == "" set int2=1
set loop=0
    :csetloop
    set /a loop+=1
    for /l %%i in (1,1,%int1%) do set cursor[%%i][%loop%]= 
    if %loop% neq %int2% goto csetloop
set cursor[%currentcursor1%][%currentcursor2%]=exit /b

③④使用法:call :cup/down [行数]
 カーソル上下が選択されたときにそれぞれ:cup/:cdownにジャンプ。
 一番上(下)からさらに上(下)を押すと、行の一番下(上)に移動します(ループ。)

:cup
set int1=%1
set /a currentcursor1=currentcursor1+int1-2
set /a currentcursor1%%=int1
set /a currentcursor1+=1
exit /b
:cdown
set int1=%1
set /a currentcursor1%%=int1
set /a currentcursor1+=1
exit /b

⑤⑥使用法:call :cleft/right [列数]
 ③④の左右版。

:cleft
set int1=%1
set /a currentcursor2=currentcursor2+int1-2
set /a currentcursor2%%=int1
set /a currentcursor2+=1
exit /b
:cright
set int1=%1
set /a currentcursor2%%=int1
set /a currentcursor2+=1
exit /b

1~nまでの特定の配列をシャッフルする

使用法:call :rnd1 [シャッフルする変数名] [シャッフルする変数の数]
 例)call :rnd1 int 5
   とされたら、
   set int1=4
   set int2=1
   set int3=5
   set int4=3
   set int5=2
   といったふうにセットしてくれます
 
 更新版は配列表示([ ]囲み)になっています。
 2016/2/5追記:fvh_Pさんから情報提供を頂き、大幅な時間の短縮が実現しました。
 ありがとうございます!

20160205更新版
:rnd1
for /l %%i in (1,1,%2) do set %1[%%i]=%%i
for /l %%i in (%2,-1,1) do (
    set /a tempnum=%1[%%i],int=!random!*%%i/32768+1
    set /a %1[%%i]=%1[!int!],%1[!int!]=tempnum
)
exit /b
20160203更新版
:rnd1
set count=%2
for /l %%i in (1,1,%count%) do set %1[%%i]=%%i
set k=%count%
:rnd2
set /a tempnum=%1[%k%],int=%random%*k/32768+1
set /a %1[%k%]=%1[%int%],%1[%int%]=tempnum,k-=1
if %k% neq 0 goto rnd2
exit /b
旧版
:rnd1
set fname=%1
set /a floop=%2,count=floop*2
for /l %%i in (1,1,%floop%) do set %fname%%%i=%%i
:rnd2
set /a sel1=%random%*floop/32768+1,sel2=%random%*floop/32768+1
set /a count-=1,%fname%tmp=%fname%%sel1%,%fname%%sel1%=%fname%%sel2%
set %fname%%sel2%=!%fname%tmp!
if %count% neq 0 goto rnd2
exit /b


選択フォルダ内に存在する全ての空フォルダを削除

使用法:call :deletejunkfolder [対象とするフォルダのフルパス]

:deletejunkfolder
for /f "delims=" %%f in ('dir %1\* /ad /b /s ^| sort /r') do rd "%%f"
exit /b


コマンドの出力を非表示にする方法

コマンド >nul

rem NULLに出力されて、コマンドの出力が画面に出なくなる。pauseで多用。choiceも/nよりこちらを


()内で変数を2重に展開する方法

使用法:()内で、内側は!変数!、外側は%%変数%%をcall(call ifは不可)

for /l %%i in (1,1,9) do (
    set /a tempnum=%%i+1
    call set tempnum=%%num[!tempnum!]%%
    if !num[%%i]! equ !tempnum! コマンド
)


横に伸びる棒グラフ(ゲージ?)を作る

使用法:call :gaugeset [分子の数値or数値が入っている変数] [分母の数値or数値が入っている変数] [ゲージの文字列を代入したい変数名] [ゲージのサイズ(100の約数)]
 例)call :gaugeset 12 100 HP 25
   とした場合、
   ■■■□□□□□□□□□□□□□□□□□□□□□□(12/100)
   という25マスのゲージ+数値を記した文字列がHPという変数に入ります。
   最後の分数表記はset %3=%gauge%(%1/%2)の行の最後を消せば消えます。

:gaugeset
set /a tempint1=%1*%4/%2
set /a tempint2=%1*%4%%%2
set gauge=
set /a blank=0,division=0
for /l %%i in (1,1,%4) do (
    if %%i leq %tempint1% (
        set tempstr1=set division=%%i
    ) else (
        if !tempint2! neq 0 (
            set tempstr1=set tempint2=0
        ) else (
            set tempstr1=)
    )
    set gauge=!gauge!!tempstr1!
)
set %3=%gauge%%1/%2exit /b


バッチファイルで、任意の範囲の実行時間を計測する方法(詳しくはリンク先。qiitaです)

使用法:call :GetTime [現在の時間を登録する番号] [すでに登録済みの番号を入力すると、引数1で登録した時間との差を表示]

:GetTime
    set T[%1]=%TIME%
    for /f "tokens=1-4 delims=:." %%a in ("!T[%1]!") do set /a MT[%1]=%%a0*36000+1%%b*6000+1%%c*100+1%%d-610100
    if "%2" == "" exit /b
    set /a D=MT[%1]-MT[%2]
    set /a D1=%D:~0,-2%0/10,D2=10%D:~-2,2%%%100+100
    echo.
    echo 開始時間:!T[%2]!
    echo 終了時間:!T[%1]!
    echo 経過時間:%D1%.%D2:~1,2%sec
    pause >nul
    exit /b


バッチファイルで、配列式変数の中身をシフトする方法(詳しくはリンク先。qiitaです)

使用法:call :dShift [シフトする変数の数字以外の部分] [数字の初項] [公差、正の値だと左シフト、負だと右シフト] [末項]

通常のシフト(非リングバッファ)
:dShift
    set str=
    for /l %%i in (%2,%3,%4) do set str=!str! %1[%%i]
    call :dShift2%str%
    exit /b
    :dShift2
        set /a %1=%2
        shift
        if "%2" == "" exit /b
        goto dshift2
シフトで押し出された数字を逆側に代入(リングバッファ)
 :dShift_r
    set str=
    for /l %%i in (%2,%3,%4) do set str=!str! %1[%%i]
    set temp=!%1[%2]!
    call :dShift2_r%str%
    set %1[%4]=!temp!
    exit /b
    :dShift2_r
        set /a %1=%2
        shift
        if "%2" == "" exit /b
        goto dshift2_r

29
28
2

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
29
28