Run any file with vbs at a specific time

2020-03-31 06:18发布

问题:

Is it possible to run any type of file (like,. mp3, doc, exe..) with a Vbscript file at a specific time?
I looked in many places but there were no success..

回答1:

Give a try for this example that can schedule Notepad to be executed everyday at 10:00 AM

So, you need just to modify those 3 arguments in this script to yours :

  1. TaskName
  2. App_FullPath
  3. strTime

Option Explicit
Dim TaskName,App_FullPath,strTime
TaskName = "Execute_Notepad"
App_FullPath = "C:\windows\notepad.exe"
strTime = "10:00"
Call CreateTask(TaskName,App_FullPath,strTime)

'*****************************************************************
Sub CreateTask(TaskName,App_FullPath,strTime)
Dim ws,strtask,exitcode
Set ws = CreateObject("Wscript.Shell")
strtask = "schtasks /create /sc Daily /tn "& qq(TaskName) & _
          " /tr "& qq(App_FullPath) & _
          " /st " & strTime & " /f"

exitcode = ws.Run(strtask, 0, True)

If exitcode <> 0 Then
  WScript.Echo "External command failed: " & Hex(exitcode)
Else
    wscript.echo "Success !"
End If
End Sub
'*****************************************************************
Function qq(str)
    qq = chr(34) & str & chr(34)
End Function
'*****************************************************************

Edit : Batch file to show or delete a numbered task names

@echo off
Mode 100,3 & color 0A
Title Delete Tasks with their time tasks execution by Hackoo 2017
:Menu
Mode 100,3 & color 0A
cls & echo(
echo    Type the time with this format "hh:mm" or a task name to show or delete for scheduled Tasks
set /a "count=0"
set /p "strTime="
cls & echo( 
echo              Please wait a while ... looking for a scheduled Tasks for "%strTime%"
Setlocal EnableDelayedExpansion
@for /f "tokens=1 delims=," %%a in ('schtasks /query /fo csv ^| find /I "%strTime%"') do (
    set /a "Count+=1"
    set "TaskName[!Count!]=%%~a"
)
Rem Display numbered Task Names
Mode 90,30 & cls & echo( 
@for /L %%i in (1,1,%Count%) do (
    set "Task=[%%i] - !TaskName[%%i]:~1!"
    echo !Task!
)

If not defined Task (
    Mode 90,3 & Color 0C
    echo(
    echo                     No Scheduled Tasks found with this criteria
    Timeout /T 3 /nobreak >nul & goto Menu
) 

echo(
Rem Asking user if he wants to delete or not the numbered task names
echo Type the number of the task to delete ! 
set /p "Input="
@for /L %%i in (1,1,%Count%) Do (
    If "%INPUT%" EQU "%%i" (
        schtasks /delete /tn "!TaskName[%%i]:~1!"
    )
)
echo(
echo Type any key to show and delete another task !
Pause>nul
Goto Menu


回答2:

In this vbscript you can change 4 arguments :

  1. TaskName
  2. AppFullPath
  3. StartTime
  4. Frequency

Option Explicit
Dim TaskName,AppFullPath,StartTime,Frequency
'************* Four params can be changed here********************
TaskName = "Execute Notepad by Hackoo"
AppFullPath = "C:\windows\notepad.exe"
StartTime = "10:00"
Frequency = "Minute"
REM The value of frequency can be taken 
Rem as "MINUTE", "HOURLY", "DAILY", "WEEKLY" or "MONTHLY"
REM https://technet.microsoft.com/en-us/library/bb490996.aspx
REM Don't change anything under this line
'************************** Main *********************************
Call CreateTask(TaskName,AppFullPath,StartTime,Frequency)
'*****************************************************************
Sub CreateTask(TaskName,AppFullPath,StartTime,Frequency)
Dim ws,strtask,exitcode
Set ws = CreateObject("Wscript.Shell")
strtask = "schtasks /create /sc "& Frequency &" /tn "& qq(TaskName) & _
          " /tr "& qq(AppFullPath) & _
          " /st " & StartTime & " /f"

exitcode = ws.Run(strtask, 0, True)

If exitcode <> 0 Then
  WScript.Echo "External command failed: " & Hex(exitcode)
Else
    wscript.echo "The Task "& qq(TaskName) & " is created successfully !"& vbcrlf &_
    "to be run "& qq(Frequency) &" with a StartTime at " & qq(StartTime) & ""
End If
End Sub
'*****************************************************************
Function qq(str)
    qq = chr(34) & str & chr(34)
End Function
'*****************************************************************


标签: vbscript