検索結果を組み合わせて作成
' ファイルシステムオブジェクトを取得する。 Set fso = WScript.CreateObject("Scripting.FileSystemObject") ' ディレクトリを指定 chkDir = "C:\test\" ' エラーメッセージ errMsg = "エラーが発生しています!" ' ファイル名を指定 strDate = year(now()) & Right(00 & month(now()), 2) & Right(00 & day(now()), 2) errLog = chkDir & strDate & "_error.log" ' ファイルの存在を確認する If fso.FileExists( errLog) Then errNow = Now() WScript.Echo errMsg,"(",errNow,")" End If ' オブジェクトの開放 Set fso = Nothing
FileSystemObjectのCopyFile()、MoveFile()、DeleteFile()ではワイルドカードが使えるのに、FileExists()では使えません。
下記サイトの偉い人が書いた関数を使用してこの問題を克服したスクリプト。
http://scripting.cocolog-nifty.com/blog/2008/02/fileexists_3b40.html
' ファイルシステムオブジェクトを取得する。 Set fso = WScript.CreateObject("Scripting.FileSystemObject") strDate = year(now()) & Right(00 & month(now()), 2) & Right(00 & day(now()), 2) & Right(00 & Hour(now()), 2) & Right(00 & Minute(now()), 2) ' ディレクトリを指定 chkDir = "C:\test\" ' エラーメッセージ errMsg = "エラーが発生しています!" ' ファイル名を指定 errLog = chkDir & strDate ' ファイルの存在を確認する If FileExists( errLog & "*" ) Then errNow = Now() WScript.Echo errMsg,"(",errNow,")" End If ' オブジェクトの開放 Set fso = Nothing Function FileExists(Spec) Dim fso Dim ParentFolderName Set fso=CreateObject("Scripting.FileSystemObject") ParentFolderName=fso.GetParentFolderName(Spec) If ParentFolderName="" Then ParentFolderName="." On Error Resume Next fso.CopyFile Spec,ParentFolderName 'fso.MoveFile Spec,ParentFolderName FileExists=Err.Number<>53 End Function
' ファイルシステムオブジェクトを取得する。 Set fso = WScript.CreateObject("Scripting.FileSystemObject") ' 日付の生成 yDate = year(now()) moDate = Right(00 & Month(now()), 2) daDate = Right(00 & Day(now()), 2) hoDate = Right(00 & Hour(now()), 2) miDate = Right(00 & Minute(now()), 2) if miDate = 00 then hoDate = Right(00 & Hour(now()), 2) -1 miDate = "59" else miDate = Right(00 & Minute(now()), 2) -1 end if strDate = yDate & moDate & daDate & hoDate & miDate ' ' ディレクトリを指定 chkDir = "C:\Documents and Settings\PT4\デスクトップ\test\" ' エラーメッセージ errMsg = "エラーが発生しています!" ' ファイル名を指定 errLog = chkDir & strDate ' 音声ファイルの指定 strSoundFile = "C:\WINDOWS\Media\Windows XP Ringin.wav" ' ファイルの存在を確認する If FileExists( errLog & "*" ) Then set objectShell = CreateObject("wscript.Shell") strCommand = "sndrec32 /play /close " & chr(34) & strSoundFile & chr(34) objectShell.Run strCommand, 0, True errNow = Now() WScript.Echo errMsg,"(",errNow,")" End If ' オブジェクトの開放 Set fso = Nothing Function FileExists(Spec) Dim fso Dim ParentFolderName Set fso=CreateObject("Scripting.FileSystemObject") ParentFolderName=fso.GetParentFolderName(Spec) If ParentFolderName="" Then ParentFolderName="." On Error Resume Next fso.CopyFile Spec,ParentFolderName 'fso.MoveFile Spec,ParentFolderName FileExists=Err.Number<>53 End Function