「マイクロソフト系技術情報 Wiki」は、「Open棟梁Project」,「OSSコンソーシアム .NET開発基盤部会」によって運営されています。
VBScriptの勘所、debugの方法、FunctionとSubの違い、エラー処理、などのトピック情報を纏めています。
言語構文やプログラミングの解説は、ここでは割愛します。
下記のガイドや、「vbs|vbscript やりたいこと」でググるなどしてください。
豊富なサンプル、Q&Aが、ネット上に多数転がっています。
ネット上に転がっていたスクリプトを引用する際は、
権利関係に注意ください。(特に、まるまる引用するような場合)
VBScript は、 Excelの VBA に似ています。
Excel の モジュールシート に記述する感じで、VBScriptファイルに記述できます。
ただ、構文や機能に、できることの違いが少しあります。
上の ユーザーズガイドから、
を参照してください。
wscript, wshshell などの WSHのオブジェクトは、以下を参照。
Scripting.FileSystemObject? などの ファイル操作関連のオブジェクトは、以下を参照。
VBScriptのプログラミングは、下のユーザーズガイドから、必要なトピックを参照ください。
MS の Script Center では、豊富なサンプル例が検索できます。
左の選択肢から、用途や種類を絞り込んで、スクリプトの具体例が参照できます。
VBScriptファイルは、vbs と wsf の 2種類あり、どちらも Windows Scripting Host のスクリプトを記述します。
( そのほかにも、htmlの中、asp(active server pages)の中 にも VBScriptを記述できますが、ここでは割愛。)
\*.vbs は VBScriptのみを記述。
も記述することができます。
Windows Scripting Host の実行ファイルは cscript.exe (コマンドライン用) または wscript.exe (Window用) です。
エクスプローラで VBScriptファイルをダブルクリックすると、既定で wscript.exe が使用されます。
実行は、コマンドプロンプトからで
cscript.exe スクリプトファイル.vbs オプション
の様に実行。
cscript.exe 自身へのオプションは // で始まる形式で、
スクリプトファイル.vbs への引数と並べて書きます。
以下、個別に。
「&」は文字列を連結する演算子です。
数値や数字は文字列に変換して連結します。
文字列であることが自明な場合は 「+」でも文字列連結が出来ます。
wscript.echo は、引数の文字列を、標準出力(cscript時) または 画面(wscript時) に出力します。
wscript.echo "これはスクリプト。" for i = 0 to 9 wscript.echo "カウント " & i next wscript.quit 0
画面の場合は、 wscript.echo 1つにつき メッセージボックスが1つ表示され、 [OK] を押すのを待ちます。
wscript.stdout.writeline は 引数の文字列を、標準出力に出力します。
cscript.exe でのみ動きます。 wscript.exe ではエラーになります。
wscript.stdout.writeline "これはスクリプト。" for i = 0 to 9 wscript.stdout.writeline "カウント " & i next wscript.quit 0
スクリプトで作り込む際には、下の様に、 print 関数を定義する、などするといいでしょう。
sub print(v) wscript.stdout.writeline v end sub
上の例の様に、sub ... end sub は、 値を返さない関数を定義します。
値を返す場合は、 function を使います。
function amari(a, b) amari = ( a mod b ) end function print amari( 20, 3 )
上の例にある「a mod b」 は、 a を b で割った剰余を求める演算子です。
VBScriptで値を代入するときは、let 文を使います。 ただし、代入するものが object の場合は、 set 文を使います。
let str1 = "abcde" str2 = "ABCDE" set fso = CreateObject("Scripting.FileSystemObject")
普通は、let は 省略して、上の str2 の代入の様に書きます。
上の関数の戻り値(function) の場合も、同じです。戻り値を返す関数の場合は、 関数名へ set を使って代入します。
filename = "c:\temp\tempfile.txt" set fso = CreateObject("Scripting.FileSystemObject") set tmp1 = makeTempFile(filename) tmp1.writeline "てすと" tmp1.close set tmp1 = nothing function makeTempFile(fn) dim f set f = fso.OpenTextFile(fn, 2, True) ' 2=書き込み, True=作成 set makeTempFile = f end function
dim文のほかに、 定数を定義する const や、 class を作成した際に
class外部から参照させないための private もあります。
なお、VBScript では、変数の型がありません。
dim var1 as string
の様に 「as 型」 を書くとエラーになります。
VBScript では、
Empty と Null と Nothingを区別するには、
isempty(変数名) isnull(変数名) isobject(変数名) 変数名 is nothing
を使います。
dim str1 if isempty(str1) then print "str1 is empty" end if
str1 = 123 if NOT isempty(str1) then print "str1 is not empty. is " & typename(str1) end if
nullやemptyを代入することも出来ます。
str1 = null str1 = empty
set fso = CreateObject("Scripting.FileSystemObject") if isobject(fso) then print "is object" end if
set obj1 = nothing if isobject(obj1) then print "is object" end if if obj1 is nothing then print "is nothing" end if
function add(dims, e) Redim Preserve dims( ubound(dims) +1 ) if isobject(e) then set dims( ubound(dims) ) = e else dims( ubound(dims) ) = e end if end function dim arr arr = array() ' 要素0個の配列 add arr, "123" ' 文字列を追加 add arr, fso ' オブジェクトを追加
ファイル操作は、Scripting.FileSystemObject? を使用します。
const ForReading = 1 set fso = CreateObject("Scripting.FileSystemObject") set ts = fso.OpenTextFile("c:\temp\tempfile.txt", ForReading) print ts.readall ts.close set ts = nothing set fso = nothing
str1 = "456" int1 = cint(str1) long1 = clng(str1) int2 = int(int1/10) int3 = cint(int1/10) int4 = str1 + 123 str4 = str1 + "123"
str1 = "123456" str2 = mid(str1,3,1) str3 = """123""と""456"""
str2 = chr(9) str3 = "これは\0で区切っています" & chr(0) & "続きもある"
str4 = "これはどんな文字列かな" if mid(str4,3,1) = "ど" then print "「ど」があるね" if instr(str4,"文字") >= len(str4)/2 then print "後半に ""文字"" がある" end if
set obj = func1() if (NOT obj is nothing) and obj.flag = 2 then ' foo end if
if NOT obj is nothing then if obj.flag = 2 then ' foo end if end if
VBScriptでは、 「 _」で次の行へ継続( _ の前にスペースあり)、「'」 があると行末まで無視します。
str1 = "あいうえお" str2 = mid (str1, _ instr(str1, "う") -1 ) ' "う" 以後の文字列を取り出す
instrは、文字列を含む位置を返します(1~、含まないなら0)。
mid は 文字目が 0 なので、 instrの戻り値-1 の場所を指定することで、
「含むならその文字以後、含まないなら、全体」という文字列の取り出し方ができます。
err1 = 0 ' エラー番号 on error resume next 'ランタイムエラーでVBScriptを終了させない Err.Clear 'Err変数をクリア set obj1 = CreateObject("hogehoge.hogehoge1") if Err.Number <> 0 then err1 = Err.Number 'ランタイムエラー判定 if err1 = 0 then if obj1.hagehage("てすと") <> 0 then err1 = 1 ' 論理エラー end if end if if err1 <> 0 then print "エラー発生 " & err1 end if wscript.quit err1 ' エラー番号 を VBScriptの終了コードに
VBScriptのデバッグは、
Script Debuger, Script Editor は、 cscript.exe で実行するときに //x をオプションに指定して実行すると、debugger が起動して、ステップ実行、 変数の確認 などができます。