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

VBScriptの対話コンソール(自作派/自作しない派)

Posted at

概要

VBScriptの対話コンソール

自作しない派

VBScript from command line ? Yes with WSH Shell Console !を使う
かなりよい。

↓のようにmsgbox("hello")とやると、「終了後にhelloがポップアップし続ける」という謎の動作をする(タスクマネージャで止めれば止まる)。

wsh.PNG

自作派

VBScript interactive shell(from wayback machine)vbscript - How can I start an interactive console for VBS - Stack Overflowを見て作る。

こちらはmsgbox("hello")をしても終了後に連続ポップアップしない(ただし、色付けとかいろいろ低機能。)
w.png

自作派用コード

以下のvbsとbatを「同じフォルダ内に同じファイル名で保存する」(補足)。

vbs_interactive_shell.vbs
do while true
  wscript.stdout.write(">>> ")
  ln = wscript.stdin.readline
  if lcase(trim(ln)) = "exit" then exit do
  on error resume next
  err.clear
  execute ln
  if err.number <> 0 then wscript.echo(err.description)
  on error goto 0
loop
vbs_interactive_shell.bat
@cscript.exe //NoLogo %~dpn0.vbs

補足

バッチファイルのパスをC:\vbs_interactive_shell.batとする。

vbs_interactive_shell.bat
@cscript.exe //NoLogo %~dpn0.vbs

%~dpn0は、「実行されているファイルが置かれているカレントディレクトリとファイル名(拡張子を含まない)」になり、C:\vbs_interactive_shell.vbsが連結されることになる。

つまり、

vbs_interactive_shell.bat
@cscript.exe //NoLogo C:\vbs_interactive_shell.vbs

と展開されるので、vbsとbatは同じファイル名にする。

参考

%~dp0 とは - [コマンドプロンプト・バッチ_Windows]

つまり%~dp0 は、『実行されているファイルが置かれているカレントディレクトリ』を表します。

%~dp0 とは、%0にオプション構文の『 ~ 』と『 d 』と『 p 』が付いたものです。
それぞれを説明すると、

%0

実行されているファイルのパスです。

~

"(ダブルクオート)を除く

d

ドライブ文字だけに展開する

p

ファイル名を除くパスの部分に展開する

バッチファイルのパラメータのメモ - Segu Entirely Ginbis UMAUMA

~n: ファイル名

test.bat
@echo off
echo %0
echo %~d0
echo %~p0
echo %~n0
echo %~x0
echo %~dp0
echo %~pn0
echo %~dpn0
echo %~dpnx0

結果
test.bat
C:
\Users\username\
test
.bat
C:\Users\username\
\Users\username\test
C:\Users\username\test
C:\Users\username\test.bat
1
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
1
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?