Run some section in batch only on certain days using VBS script

Posted: June 9, 2015 in Scripts
Tags:

We have a block of code which needs to be executed only on weekdays,how to accomplish that.First,create a file with vbs extension and paste a following code in it:

dtmToday = Date()

dtmDayOfWeek = DatePart(“w”, dtmToday)

Select Case dtmDayOfWeek

Case 1

Set objFileToWrite = CreateObject(“Scripting.FileSystemObject”).OpenTextFile(“C:\@IT\Batch\day.txt”,2,true)
objFileToWrite.WriteLine(“Sunday”)
objFileToWrite.Close
Set objFileToWrite = Nothing
Case 2

Set objFileToWrite = CreateObject(“Scripting.FileSystemObject”).OpenTextFile(“C:\@IT\Batch\day.txt”,2,true)
objFileToWrite.WriteLine(“Monday”)
objFileToWrite.Close
Set objFileToWrite = Nothing
Case 3

Set objFileToWrite = CreateObject(“Scripting.FileSystemObject”).OpenTextFile(“C:\@IT\Batch\day.txt”,2,true)
objFileToWrite.WriteLine(“Tuesday”)
objFileToWrite.Close
Set objFileToWrite = Nothing

Case 4

Set objFileToWrite = CreateObject(“Scripting.FileSystemObject”).OpenTextFile(“C:\@IT\Batch\day.txt”,2,true)
objFileToWrite.WriteLine(“Wednesday”)
objFileToWrite.Close
Set objFileToWrite = Nothing
Case 5

Set objFileToWrite = CreateObject(“Scripting.FileSystemObject”).OpenTextFile(“C:\@IT\Batch\day.txt”,2,true)
objFileToWrite.WriteLine(“Thursday”)
objFileToWrite.Close
Set objFileToWrite = Nothing
Case 6

Set objFileToWrite = CreateObject(“Scripting.FileSystemObject”).OpenTextFile(“C:\@IT\Batch\day.txt”,2,true)
objFileToWrite.WriteLine(“Friday”)
objFileToWrite.Close
Set objFileToWrite = Nothing
Case 7

Set objFileToWrite = CreateObject(“Scripting.FileSystemObject”).OpenTextFile(“C:\@IT\Batch\day.txt”,2,true)
objFileToWrite.WriteLine(“Saturday”)
objFileToWrite.Close
Set objFileToWrite = Nothing
End Select

name file check.vbs,save and close the file.

This code create a file called C:\@IT\Batch\day.txt in which everu day of the week is written.We will use thata in this file later on.

Now open a batch file with block code which needs to be executed every day

Our batch code is in file called everyday.bat (it this file is code which runs everyday,plus block which needs to be run only on weekdays).

Let’s say our block code is called weekday (runs only by weekdays)

rem code which is executed every day

//some code

Find “Satyrday” “C:\@IT\Batch\day.txt”>”C:\@IT\Batch\vidi day.txt” || Find “Sunday” “C:\@IT\Batch\day.txt”>”C:\@IT\Batch\desired day.txt”

if %ERRORLEVEL% NEQ 0 (
goto :end
) else (
goto:weekday
)

:weekday

some code which runs on weekday

:end

exit

Find command searchs for string saturday or sunday (||) in file day.txt and writes results in file desired day.txt.If it find the match then code in :weekend is run,othervise,batch exits.

Now we need to create batch file which first calls check.vbs and then executes everyday.bat

create a new file with bat.extension and write:

start check.vbs

start everyday.bat

name this file whatever You like and include it in task scheduler.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s