FileSystemObjectのFileExistsメソッドを使用することにより、ファイルの存在チェックをすることができます。
以下にFileExistsメソッドを使用したVBScriptサンプルコードおよびサンプルコードの実行結果を記します。
以下に、ファイルの存在チェックを行うVBScriptサンプルコードと実行結果を記します。
Set fso = CreateObject("Scripting.FileSystemObject") Const ForReading = 1, ForWriting = 2, ForAppending = 8 ' Create Text File Set tso = fso.OpenTextFile("foo.txt", ForWriting, true) tso.Close Set tso = Nothing If fso.FileExists("foo.txt") Then WScript.Echo "foo.txt file exists." Else WScript.Echo "foo.txt file not found." End If If fso.FileExists("bar.txt") Then WScript.Echo "bar.txt file exists." Else WScript.Echo "bar.txt file not found." End If Set fso = Nothing
上記のVBScriptサンプルコードは以下のように動作します。
C:\wsh>cscript /nologo fileexists.vbs foo.txt file exists. bar.txt file not found. C:\wsh>dir foo.txt ドライブ C のボリューム ラベルは Windows です ボリューム シリアル番号は 1234-5678 です C:\wsh のディレクトリ 2012/07/21 00:53 0 foo.txt 1 個のファイル 0 バイト 0 個のディレクトリ 114,663,075,840 バイトの空き領域 C:\wsh>dir bar.txt ドライブ C のボリューム ラベルは Windows です ボリューム シリアル番号は 1234-5678 です C:\wsh のディレクトリ ファイルが見つかりません