Categories
Useful Scripts vbscript Windows Batch WSH

Generate Yesterday’s Date Through Windows Batch

Generating past date in a windows batch can be tricky. There doesn’t seem to be any date arithmetic functionality within the DOS kernel. There are a number of ways you can do it, but the one that works for me is through the generation of a text file. There is only one pre-requisite which is listed below

  • Windows Script Hosting (Installed on most XP Pro machines but can be downloaded here.)

 

You need to create two files, in this example, I’ve referred to them as gendate.vbs and gendate.bat. Gendate.vbs is written in vbscript and it takes two integer parameters:

  • Days Integer Paramater eg. -1 for yesterday’s date, 1 for tomorrow’s date and 0 for today’s date
  • Date output format parameter i.e. 0 returns YYYY-MM-DD, 1 returns DD-MON-YY

 

The first parameter is an integer value which is the number of days before/after the current date – used to determine the date you want returned. The second parameter determines the date format you want returned i.e 0 returns YYYY-MM-DD format, while 1 returns DD-MON-YY format. This module can be extended to return whatever format you require. Here’s the content of gendate.vbs:

'Wscript.Arguments(0) - Days Integer Paramater eg. -1 for yesterday's date, 1 for tomorrow's date and 0 for today's date
'Wscript.Arguments(1) - Date output format parameter i.e. 0 returns YYYY-MM-DD, 1 returns DD-MON-YY

dateRequested = dateadd("d", Wscript.Arguments(0) ,date)
yestStr = dateToString(dateRequested, Wscript.Arguments(1))

'Write response to file
Dim oFs, oTextFile, iMode
iMode = 2
set oFs = createobject("Scripting.FileSystemObject")
set oTextFile = oFs.OpenTextFile("date.txt", iMode, True)
oTextFile.Write yestStr
oTextFile.Close
set oTextFile = nothing
set oFS = nothing

'**************************************************************************************
'* dateToString
'* -------------------------------
'* Returns date object as a string
'* if opt = 0 then returns date string in YYYY-MM-DD format
'* otherwise return DD-MON-YY
'**************************************************************************************
Function dateToString(dteObj, opt)
  dayStr = Day(dteObj)
  if CInt(dayStr) < 10 then
    dayStr = "0" & dayStr
  end if
  monStr = Month(dteObj)
  if CInt(monStr) < 10 then
    monStr = "0" & monStr
  end if

  if opt = 0 then
    retStr = Year(dteObj) & "-" & monStr & "-" & dayStr
  else
    monName = MonthName(monStr,true)
    retStr = dayStr & "-" & UCASE(monName) & "-" & Right(Year(dteObj), 2)
  end if
dateToString = retStr
End Function

Gendate.bat is windows batch file that drives the process. It calls gendate.vbs and outputs the date string requested. Of course you need to add your business process in this file so that it performs tasks that you require. Here’s the content of gendate.bat:

@echo off

if "%1"=="" goto :usage
if "%2"=="" goto :usage

REM If you're familiar with VBSCRIPT you can modify this file to return any date format that you require
gendate.vbs %1 %2

setlocal enabledelayedexpansion
set SEPARATOR=
set filecontent=
for /f "delims=" %%a in (date.txt) do (
  set currentline=%%a
  set filecontent=!filecontent!%SEPARATOR%!currentline!
)

set datestr=%filecontent%
echo The date requested is: %datestr%
echo This value is stored in a variable called "datestr" and can be used within the script

goto :done

:Usage

echo usage 		gendate [Days Integer Paramater] [Date format type]

echo Example to generate yesterday's date in YYYY-MM-DD format: 	gendate -1 0
echo Example to generate yesterday's date in DD-MON-YY format: 	gendate -1 1

echo Example to generate tomorrow's date in YYYY-MM-DD format: 	gendate -1 0
echo Example to generate tomorrow's date in DD-MON-YY format: 	gendate -1 1

echo Example to generate today's date in YYYY-MM-DD format: 	gendate 0 0
echo Example to generate today's date in DD-MON-YY format: 	gendate 0 1

:done

To use the script, call the following in a command prompt window:

echo Example to generate yesterday’s date in YYYY-MM-DD format: gendate -1 0

echo Example to generate yesterday’s date in DD-MON-YY format: gendate -1 1

echo Example to generate tomorrow’s date in YYYY-MM-DD format: gendate -1 0

echo Example to generate tomorrow’s date in DD-MON-YY format: gendate -1 1

echo Example to generate today’s date in YYYY-MM-DD format: gendate 0 0

echo Example to generate today’s date in DD-MON-YY format: gendate 0 1

Categories
Useful Scripts vbscript

VBScript to Save Web File to Computer

This post is an extension from the URL2File script that I posted previously. While the URL2File script allowed you to save web pages to a local file, the following VbScript will allow you to save a binary file on the web to a local file.

This VbScript requires Windows script hosting which you can download from:

http://www.microsoft.com/downloads/details.aspx?FamilyID=47809025-D896-482E-A0D6-524E7E844D81&displaylang=en

Save the following code as save2file.vbs:

'Wscript.Arguments(0) - Website  URL
'Wscript.Arguments(1) - Full File Path

Const adTypeBinary = 1
Const adSaveCreateOverWrite = 2

'Create Stream object
Dim BinaryStream
Set BinaryStream = CreateObject("ADODB.Stream")

'Specify stream type - we want To save binary data.
BinaryStream.Type = adTypeBinary

'Open the stream And write binary data To the object
BinaryStream.Open
BinaryStream.Write BinaryGetURL(Wscript.Arguments(0))

'Save binary data To disk
BinaryStream.SaveToFile Wscript.Arguments(1), adSaveCreateOverWrite
Function BinaryGetURL(URL)
  'Create an Http object, use any of the four objects
  Dim Http
'  Set Http = CreateObject("Microsoft.XMLHTTP")
'  Set Http = CreateObject("MSXML2.ServerXMLHTTP")
  Set Http = CreateObject("WinHttp.WinHttpRequest.5.1")
'  Set Http = CreateObject("WinHttp.WinHttpRequest")

  'Send request To URL
  Http.Open "GET", URL, False
  Http.Send
  'Get response data As a string
  BinaryGetURL = Http.ResponseBody
End Function

To run the script in a command prompt session:

save2file.vbs [URL] [FULL OUTPUT FILE PATH]
eg. save2file.vbs http://localhost/test.XLS c:\temp\test.XLS

Categories
Useful Scripts vbscript

URL2File

I ran into a problem recently with my windows batch script files because I use this great utility called URL2FILE.exe which is a free 32bit Windows console-mode application able to retrieve and save the content of a World Wide Web content to a local file. Well, for some unknown reason our company virus scan software, VirusScan, decided that URL2FILE.exe was a Generic PUP.z virus and cascade deleted all copies of the file on our server! In an instant all my services that use the file was rendered useless!

Virus Console

With limited knowledge of Windows Script Hosting and VbScript, I’ve managed to write up a small vbscript file that pretty much does what url2file aims to do with text files (please note that the script does NOT work with binary files). To effectively create a script that replicates the download of web pages similar to what url2file.exe does, then do the following:

Download and install Windows script hosting from:

http://www.microsoft.com/downloads/details.aspx?FamilyID=47809025-D896-482E-A0D6-524E7E844D81&displaylang=en

Save the following code as URL2FILE.vbs:

'Arguments
'Wscript.Arguments(0) - Website  URL
'Wscript.Arguments(1) - Full File Path
Dim lngResolveTimeout, lngConnectTimeout, lngSendTimeout, lngReceiveTimeout, retVal
'30 Min timeout when accessing web page
lngResolveTimeout = 1800000
lngConnectTimeout = 1800000
lngSendTimeout = 1800000
lngReceiveTimeout = 1800000
'Valid URL continue validation
Dim xml, txresponse
Set xml = CreateObject("MSXML2.ServerXMLHTTP")
xml.setTimeouts lngResolveTimeout, lngConnectTimeout, lngSendTimeout, lngReceiveTimeout
xml.Open "GET", Wscript.Arguments(0), False
xml.Send
'Write response to file
Dim oFs, oTextFile, iMode
iMode = 2
set oFs = createobject("Scripting.FileSystemObject")
set oTextFile = oFs.OpenTextFile(Wscript.Arguments(1), iMode, True)
oTextFile.Write xml.responseText
oTextFile.Close
set oTextFile = nothing
set oFS = nothing

To run the script in a command prompt session:

url2file.vbs [URL] [FULL OUTPUT FILE PATH]
eg. url2file.vbs http://localhost/test.htm c:\temp\test.htm