VBSでIPアドレスを取得する(DOS窓非表示)
objShell.Exec(“ipconfig.exe”) などと Execを使うとDOS窓が表示されてしまい少々気になっていた。
DOS窓が非表示できるのは Run を使って書き直してみたので、メモしておきます
Dim wsh, env, exe
Dim ipaddress,str
Dim strLine, iColon
Dim objFSO,objFile,objFolder,temp_path,strFile
Set wsh = WScript.CreateObject("WScript.Shell")
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.getSpecialFolder(2)
strFile = objFolder.Path & "\" +"temp_ipconfig.txt"
str="cmd /c ipconfig.exe > " + strFile
wsh.Run str,0,True
Set objFile = objFSO.OpenTextFile(strFile)
Do While objFile.AtEndOfStream <> True
strLine = objFile.ReadLine
If InStr(strLine, "IP Address") <> 0 Then
iColon = Instr(strLine, ":")
ipaddress = Mid(strLine, iColon + 2)
ipaddress = Replace(ipaddress, vbCr, "")
End If
If InStr(strLine, "IPv4 アドレス") <> 0 Then
iColon = Instr(strLine, ":")
ipaddress = Mid(strLine, iColon + 2)
ipaddress = Replace(ipaddress, vbCr, "")
exit do '最初の"IPv4 アドレス"を見つけてアバウトに終了
End If
Loop
msgbox ipaddress
Set objFile = Nothing
Set objFolder = Nothing
Set objFSO = Nothing
Set wsh = Nothing