{"id":533,"date":"2014-04-23T15:15:52","date_gmt":"2014-04-23T15:15:52","guid":{"rendered":"http:\/\/tomvanmaanen.nl\/?p=533"},"modified":"2014-04-23T15:15:52","modified_gmt":"2014-04-23T15:15:52","slug":"starting-vbscript","status":"publish","type":"post","link":"http:\/\/archief.van-maanen.com\/?p=533","title":{"rendered":"Starting vbscript"},"content":{"rendered":"<p>I recently came across vbscript. I was a bit curious to see how this works. As a first step, I created a small batch file (&#8220;tom.bat&#8221;) that invokes a vbscript. This script reads as:<\/p>\n<pre>\ncscript C:\\Users\\TOM.VAN-MAANEN\\tom.vbs \"KINLW7D6LPVM1\"\nexit %ERRORLEVEL%\n<\/pre>\n<p>This script shows how such vbscripts can be invoked. One may start a script from the command line with the command &#8220;cscript&#8221;. This command is followd by a scriptname and a parameter. The script is &#8221; C:\\Users\\TOM.VAN-MAANEN\\tom.vbs&#8221;  and the parameter is &#8220;KINLW7D6LPVM1&#8221;. The script returns a runcode that can be displayed with exit %ERRORLEVEL%. As an example, I also provide the script:<\/p>\n<pre>\nOn Error Resume Next\nConst CONVERSION_FACTOR = 1024\nConst ONE_SEC = 1000\nIf WScript.Arguments.Count = 0 Then\n Wscript.Echo \"Usage: tom.vbs server1 [server2] [server3] ...\"\n WScript.Quit\nEnd If\nFor Each Computer In WScript.Arguments\n  Set objWMIService = GetObject(\"winmgmts:\/\/\" & Computer)\n  Set colLogicalDisk = objWMIService.InstancesOf(\"Win32_LogicalDisk\")\n  wscript.Echo colLogicalDisk.Count & \" drives found on \" & Computer \n  For i = 1 to 1\n    For Each objLogicalDisk In colLogicalDisk\n    wscript.Echo \"There are \" & int((objLogicalDisk.FreeSpace \/ (CONVERSION_FACTOR * CONVERSION_FACTOR * CONVERSION_FACTOR))) & _ \n    \" Gigabytes on \"  & \" \" & objLogicalDisk.DeviceID\n    Next\n    Wscript.Sleep ONE_SEC\n  Next\nNext\n<\/pre>\n<p>What does this script do? This script checks the disk space on each drive on the computer &#8220;KINLW7D6LPVM1&#8221;. It uses existing objects that are available in a windows environment with the WMI service. A class within this object provides information on the drives. It provides information on the number of drives and the free space that is available. The information is gathered and exported in a loop that displays the information on each of the drives. I also created a loop to allow for a regular check on available diskspace. Within this loop a delay is included.<\/p>\n<p>Another nice example is this vbs programme:<\/p>\n<pre>\n\nConst cAppend = 8\n\n\nparamStr = Wscript.Arguments.Item(0)\n\n\nif (paramStr = \"ONZIN\") then\n\tparamFile = \"C:\\Users\\TOM.VAN-MAANEN\\test_list_parm.txt\" '''''''''''''''BW parameter txt file for LASTSHIP DATE and others which run once a day\n\tInboundFolder = \"C:\\Users\\TOM.VAN-MAANEN\\\"  '''''''''''''''BW TD folder path \n\tlogfile = \"C:\\Users\\TOM.VAN-MAANEN\\test_list_log.txt\" \nend if\n\n\nSet objFSO = CreateObject(\"Scripting.FileSystemObject\")\nSet objLogFile = objFSO.OpenTextFile(logfile, cAppend, True) 'open the text file for appending\n\n\nif (trim(paramStr) = \"\") then\n  objLogFile.WriteLine paramStr & \" file checking aborted, error in input parameter value\"\n  set objFSO = nothing\n  WScript.Quit(1)  ''''FAILURE\nend if\n\nIF objFSO.FileExists(paramFile) Then \n\n  Set fileList = objFSO.OpenTextFile(paramFile, 1) 'open the text file for reading only\n  i = 0\n\n'''' ------------------- Missing file listing ----------------\n  Do While Not fileList.AtEndOfStream 'loop until the end of the file\n   \tstrfilename = \"\"\n\tstrfilename = fileList.ReadLine \n\t\t\n    If (Not strfilename = \"\") then ''''check filename is not empty\n\tIf NOT (objFSO.FileExists(InboundFolder & strfilename))  then ''''check file exists in inbound folder\n  \t\tif (i =0) then \n\t\tobjLogFile.WriteLine \"-------------------------------**** Missing file list *****----------------------------------\"\n\t\tend if\n\n\t\ti = i + 1\n\n\t\tobjLogFile.WriteLine cstr(i) & \". \" & strfilename\n    \tEnd If\n\n    End If\t    \n  Loop '''' end loop for missing files \n  fileList.Close\n \n\n  If (i > 0) then '''' atleast some missing files, terminate\n\t\tobjLogFile.WriteLine \" \"\n \t\tobjLogFile.WriteLine \"=========================================================\"\t  \n   \t\tobjLogFile.WriteLine \"File list not complete in Landed folder for \" & paramStr & \" at \" & Now\n\t  \tobjLogFile.WriteLine \"=========================================================\"\n  \t\tfileList.Close \n  \t\tobjLogFile.Close\n\t\tset objFSO = nothing\n \t\tWScript.Quit(1) ''''FAILURE\n  End If\n\n  objLogFile.WriteLine \" \"\n  objLogFile.WriteLine \"=========================================================\"\n  objLogFile.WriteLine \"===== \" & paramStr & \" File checking process COMPLETED SUCCESSFULLY====\" & Now\n  objLogFile.WriteLine \"=========================================================\"\n\n  fileList.Close\n  objLogFile.Close\n  set objFSO = nothing\n  WScript.Quit(0) '''''SUCCESS\n\nELSE\n  objLogFile.WriteLine paramStr & \" file checking Process terminated, parameter file is missing in scripts folder for \" & paramStr\n  objLogFile.WriteLine \"=========================================================\"\n\n  objLogFile.Close\n  set objFSO = nothing\n  WScript.Quit(1) ''''FAILURE\nEND IF\n<\/pre>\n<p>This script can be started with: cscript C:\\Users\\TOM.VAN-MAANEN\\test_list_scr.vbs &#8220;ONZIN&#8221;. The idea in this script in that file is read that contains a list of file names. For each file name, it is checked as to whether such a file exits in a given directory. If that is not the case, such file name is added to an array. In a subsequent step, the list with file names that do appear in a given location are written to a file.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently came across vbscript. I was a bit curious to see how this works. As a first step, I created a small batch file (&#8220;tom.bat&#8221;) that invokes a vbscript. This script reads as: cscript C:\\Users\\TOM.VAN-MAANEN\\tom.vbs &#8220;KINLW7D6LPVM1&#8221; exit %ERRORLEVEL% This script shows how such vbscripts can be invoked. One may start a script from the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-533","post","type-post","status-publish","format-standard","hentry","category-nice-to-know"],"_links":{"self":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts\/533","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=533"}],"version-history":[{"count":0,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts\/533\/revisions"}],"wp:attachment":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=533"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}