VBS判断视频文件是否存在并弹框告警,查看指定前缀的文件是否存在

摄像头经常掉线,掉线后就不会生成视频文件,自己弄个简单的检查报警程序,相对来说比较简陋。

使用vbs判断文件夹是否存在

版本一

Dim fso, dt, sDate, folderPath

folderPath = "\\192.168.1.226\A005G2\"
dt = Date
sDate = Year(dt) & Right("0" & Month(dt), 2) & Right("0" & Day(dt), 2)
folderPath = folderPath & sDate

Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(folderPath) Then
    MsgBox "视频文件夹不存在:" & folderPath, vbExclamation   + vbSystemModal, "提示"
End If

Set fso = Nothing

 

版本二

优化:根据日期时间计算视频文件数量,,写运行日志

Dim dt, sDate, sTime, baseFolderPath, folderPath,prefixFileName,fileTotal

baseFolderPath = "\\192.168.1.226\A005G2\"
dt=Now()
'y = Year(dt)      ' 年份
'm = Month(dt)     ' 月份
'd = Day(dt)       ' 日
'h = Hour(dt)      ' 小时
'min = Minute(dt)  ' 分钟
's = Second(dt)    ' 秒
sDate = Year(dt) & Right("0" & Month(dt), 2) & Right("0" & Day(dt), 2)
sTime = Right("0" & Hour(dt), 2)' & Right("0" & Minute(dt), 2)
'msgbox "sDate:" & sDate & vbCrLf & "sTime:" & sTime
folderPath = baseFolderPath & sDate & "\1"
'folderPath = baseFolderPath & "20261002" & "\1"
prefixFileName = "video" & sDate & sTime
'prefixFileName = "video" & "20261002" & "20"
'msgbox "folderPath:" & folderPath & vbCrLf & "prefixFileName:" & prefixFileName
fileTotal = CountVideoFiles(folderPath,prefixFileName)
WriteLog("获取的"&folderPath&"\"&prefixFileName&"*文件总数是:"&fileTotal)

' 置顶弹窗版本(4096=顶层)
'WScript.Popup "符合条件文件总数:" & total, 0, "统计结果", 4096

If fileTotal = -1 Then
    MsgBox "当天视频文件夹不存在:" & folderPath, vbExclamation   + vbSystemModal, "提示"
ElseIf fileTotal = 0 Then
    MsgBox "未发现视频文件:" & vbCrLf & prefixFileName & "*.mp4" & vbCrLf & "请重启摄像头!"
else
    'MsgBox prefixFileName & "*.mp4" & " 文件个数:" & fileTotal
End If


Function CountVideoFiles(tmpFolderPath,pattern)
    Dim fso, folder, file, cnt
    Set fso = CreateObject("Scripting.FileSystemObject")
    cnt = 0    
    If Not fso.FolderExists(tmpFolderPath) Then
        CountVideoFiles = -1
        Exit Function
    End If
    
    Set folder = fso.GetFolder(tmpFolderPath)
    For Each file In folder.Files
        ' 视频文件video20261104000003.mp4名,且时长是10分钟,建议超过10分钟后获取
        'If Left(file.Name,Len(pattern)) = pattern And LCase(fso.GetExtensionName(file.Path)) = "mp4" Then
        If Left(file.Name,Len(pattern)) = pattern Then
            cnt = cnt + 1
        End If
    Next
    Set fso = Nothing
    CountVideoFiles = cnt
End Function


Function WriteLog(sText)
    Dim logFile, fso, logMsg
    Set fso = CreateObject("Scripting.FileSystemObject")    
    logFilePath = fso.GetFile(Wscript.ScriptFullName).Path & "." & Left(sDate,6) & ".log"
    ' 参数说明:8 表示追加模式,True 表示文件不存在则新建
    Set logFile = fso.OpenTextFile(logFilePath, 8, True)    
    logMsg = "[" & Now() & "]" & sText
    logFile.WriteLine logMsg
    logFile.Close
    Set logFile = Nothing    
    Set fso = Nothing
End Function

 

版本三

优化:增加自动重启视频摄像头的操作,使用python脚本重启摄像头参考:使用python+Playwright实现网页自动化操作

Dim dt, sDate, sTime, baseFolderPath, folderPath,prefixFileName,fileTotal

baseFolderPath = "\\192.168.1.226\A005G2\"
dt=Now()
'y = Year(dt)      ' 年份
'm = Month(dt)     ' 月份
'd = Day(dt)       ' 日
'h = Hour(dt)      ' 小时
'min = Minute(dt)  ' 分钟
's = Second(dt)    ' 秒
sDate = Year(dt) & Right("0" & Month(dt), 2) & Right("0" & Day(dt), 2)
sTime = Right("0" & Hour(dt), 2)' & Right("0" & Minute(dt), 2)
'msgbox "sDate:" & sDate & vbCrLf & "sTime:" & sTime
folderPath = baseFolderPath & sDate & "\1"
folderPath = baseFolderPath & "20261002" & "\1"
prefixFileName = "video" & sDate & sTime
'prefixFileName = "video" & "20261002" & "20"
'msgbox "folderPath:" & folderPath & vbCrLf & "prefixFileName:" & prefixFileName
fileTotal = CountVideoFiles(folderPath,prefixFileName)
WriteLog("获取的"&folderPath&"\"&prefixFileName&"*文件总数是:"&fileTotal)

' 置顶弹窗版本(4096=顶层)
'WScript.Popup "符合条件文件总数:" & total, 0, "统计结果", 4096


If fileTotal = -1 Then
    WriteLog("未发现当天视频文件夹:" & folderPath & ",将尝试使用python重启摄像头!")
    If RestartVideo() <> 0 Then
        MsgBox "当天视频文件夹不存在:" & folderPath & vbCrLf & "请使用APP重启摄像头!", vbExclamation   + vbSystemModal, "提示"
    End If    
ElseIf fileTotal = 0 Then
    WriteLog("未发现视频文件:" & prefixFileName & "*.mp4" & ",将尝试使用python重启摄像头!")
    If RestartVideo() <> 0 Then
        MsgBox "未发现视频文件:" & vbCrLf & prefixFileName & "*.mp4" & vbCrLf & "请使用APP重启摄像头!"
    End If
Else
    'MsgBox prefixFileName & "*.mp4" & " 文件个数:" & fileTotal
End If

Function RestartVideo()
    Dim wsh, cmd, ret
    Set wsh = CreateObject("WScript.Shell")
    cmd = """C:\Windows\py.exe"" ""C:\Tmp\restartVideo.py"""
    '0=隐藏窗口,True=等待程序结束再往下走
    ret = wsh.Run(cmd, 0, True)
    If ret = 0 Then
        WriteLog("已完成重启摄像头。")
    Else
        WriteLog("python 脚本重启摄像头返回错误码:ret=" & ret)
    End If
    RestartVideo = ret
End Function

Function CountVideoFiles(tmpFolderPath,pattern)
    Dim fso, folder, file, cnt
    Set fso = CreateObject("Scripting.FileSystemObject")
    cnt = 0    
    If Not fso.FolderExists(tmpFolderPath) Then
        CountVideoFiles = -1
        Exit Function
    End If
    
    Set folder = fso.GetFolder(tmpFolderPath)
    For Each file In folder.Files
        ' 视频文件video20261104000003.mp4名,且时长是10分钟,建议超过10分钟后获取
        'If Left(file.Name,Len(pattern)) = pattern And LCase(fso.GetExtensionName(file.Path)) = "mp4" Then
        If Left(file.Name,Len(pattern)) = pattern Then
            cnt = cnt + 1
        End If
    Next
    Set fso = Nothing
    CountVideoFiles = cnt
End Function


Function WriteLog(sText)
    Dim logFile, fso, logMsg
    Set fso = CreateObject("Scripting.FileSystemObject")    
    logFilePath = fso.GetFile(Wscript.ScriptFullName).Path & "." & Left(sDate,6) & ".log"
    ' 参数说明:8 表示追加模式,True 表示文件不存在则新建
    Set logFile = fso.OpenTextFile(logFilePath, 8, True)    
    logMsg = "[" & Now() & "]" & sText
    logFile.WriteLine logMsg
    logFile.Close
    Set logFile = Nothing    
    Set fso = Nothing
End Function

 

posted on 2026-11-08 22:28  jack_Meng  阅读(10)  评论(0)    收藏  举报

导航