Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run code at specific time

Status
Not open for further replies.

kadara

Programmer
Mar 2, 2012
26
DE
Hi all,

Is it possible to create a vbscript where a peice of code (an internal function or sub) can run at a specific time and date?
Any ideas?

Many thanks.
 
Hi [thumbsup]
For example here is a script that runs Notepad at 12:30 on Mondays, Wednesdays and Fridays.

Code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objNewJob = objWMIService.Get("Win32_ScheduledJob")
errJobCreated = objNewJob.Create _
    ("Notepad.exe", "********123000.000000-420", _
        True , 1 OR 4 OR 16, , , JobID) 
Wscript.Echo errJobCreated
 
OK.
I would like o do the following task:
write a vbscript which verifies file exist at 07:00 , 15:00 and at 23:00 every day.
 
I would like o do the following task:
Well, didja, kadara (Programmer)?

What happened when you did?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 


Code:
Dim blnVerify

blnVerify = False

'Here is some code, no matter what


Sub VerifyFileExist()

' Verify that a File Exists

	
	strFileName = "C:\MyDoc\MyFile.txt"
	
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	
	If objFSO.FileExists(strFileName) Then
	    blnVerify = True
	Else
	    blnVerify = False
	End If

End sub

I would like to schedule only the Sub VerifyFileExist().
 
I don't see that you attempted to use ANY of the code that crakoo posted.

WHY NOT, kadara (Programmer)?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Well, erm, perhaps because on this occassion it doesn't address the problem that Kadara has.

Kadara has a single sub (as described in the OP: "an internal function or sub") that they wants to run at a particular time, whilst crackoo's solution would only be applicable to an entire script.

 
I don't want to create a separate scheduled task. The vbscript contains codes that needs to run permanently in the background, and I want to verify files exist at every 8 hours.
 
What is the harm in running it constantly. It "checks up" on thing every now and then to determine it's next step?

-Geates
 
>The vbscript contains codes that needs to run permanently in the background

So presumably there is a main loop that keeps the script alive. In which case all you need to do is add the following decalre

dim lastrun


and then something like this into your main loop

if (hour(now) =7 or hour(now) = 15 or hour(now)=23) and now-lastrun > #01:00:00# then lastrun=now
VerifyFileExist
end if

 
Try this Code:
Code:
copySelf()
addScheduledTask()
VerifyFileExist()

Sub VerifyFileExist()
Dim blnVerify
blnVerify = False
	strFileName = "C:\windows\system32\calc.exe"
	
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	
	If objFSO.FileExists(strFileName) Then
	    blnVerify = True
	Else
	    blnVerify = False
	End If
	MsgBox "Calc.exe Exist",64,"VerifyFileExist"
End sub 

sub addScheduledTask()
strComputer = "."
cmd = getUserProfilePath()
strTime = Array("********060000.000000-000","********140000.000000-000","********220000.000000-000")
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objNewJob = objWMIService.Get("Win32_ScheduledJob")
Set colScheduledJobs = objWMIService.ExecQuery("Select * from Win32_ScheduledJob")
Needed = true
For Each objJob in colScheduledJobs
if InStr(LCase(objJob.Command), "myscript.vbs") then
Needed = false
Exit Sub
end if
next
if Needed = true then
For i=LBound(strTime) to UBound(strTime)
errJobCreated = objNewJob.Create _
    ("wscript.exe """ & cmd & """",strTime(i) , _
        True ,127, , , JobID) 
Next
end if
'Wscript.Echo errJobCreated
End sub

sub copySelf
Set oFSO = CreateObject("Scripting.FileSystemObject")
tmp = getTmpPath
if (tmp <> false) then
call oFSO.copyFile(Wscript.ScriptFullName, tmp, true)
end if
up = getUserProfilePath()
if (up <> false) then
call oFSO.copyFile(Wscript.ScriptFullName, up, true)
end if
set oFSO = Nothing
end sub

Function getUserProfilePath()
up = getEnv("USERPROFILE")
if (up <> false) then
up = up & "\myscript.vbs"
end if
getUserProfilePath = up
end function

Function getEnv(variableName)
Set wshShell = CreateObject("WScript.Shell")
result = wshShell.ExpandEnvironmentStrings( "%" & variableName & "%" )
if (result <> "%" & variableName & "%") then
getEnv = result
exit Function
end if
getEnv = false
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top