首页 > 解决方案 > 如何在第一次触发时跳过 bat 文件的运行但之后运行?

问题描述

我已安排以下任务在用户登录后每 XXX 次重复一次(这是触发 bat 文件的时间)。我想在消息框触发时跳过/不显示(意味着它第一次运行时),但在以下重复中不显示。关于如何解决这种情况的任何建议?

@echo off

SET client=Dear ABC,
SET MESSAGE=XYZ

@echo WScript.CreateObject("WScript.Shell").Popup "%client% %MESSAGE% & I will dissapear after 00 seconds.", 00, "title" > %TEMP%\wait.vbs 
wscript %TEMP%\wait.vbs

标签: batch-filevbscript

解决方案


解决此问题的一种简单方法如下:

' Checks for the existence of a file which will
' serve as a flag, indicating if the script in 
' question has already been run or not, acting
' accordingly afterwards.

Set objShell = CreateObject("WScript.Shell")
Set objFSO=CreateObject("Scripting.FileSystemObject")

homeFolder = objShell.ExpandEnvironmentStrings("%USERPROFILE%")

flagFile= homeFolder & "\flag.txt"

If (objFSO.FileExists(flagFile)) Then

    ' Display your message
    msgbox("This is my message")

    '...(do other things you want)

Else

    ' Do not display your message

    ' Creates flag to indicate that
    ' this script has already been executed
    Set objFile = objFSO.CreateTextFile(flagFile,True)

    '...(do other things you want)

End If

推荐阅读