Solved

Shell script syntax convert to VBScript

Posted on 2006-10-19
4
1,980 Views
Last Modified: 2008-01-09
I have a script which does a great job of using the uptime.exe to output a barebones uptime number. I am wanting to use a VBScript to do the same task and have it acquire hostnames from "servers.txt". The text file has one servername per line.

Uptime.exe <servername> is the syntax for remote access.

I am inluding the existing shell script below.

@echo off
setlocal
for /f "tokens=6,7,8,10 delims=, " %%a in ('"uptime.exe"') do (
  set dd=%%a%%b
  set hh=%%c
  set mm=%%d
)
if %hh% LSS 10 set hh=0%hh%
if %mm% LSS 10 set mm=0%mm%
set Uptime=%dd%,%hh%:%mm%
echo Uptime: %Uptime%
pause
0
Comment
Question by:johndarby
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 23

Expert Comment

by:basicinstinct
ID: 17770582
Can I ask why you want to convert this to a VBScript if it "does a great job"...?
If it ain't broke, don't fix it.
0
 
LVL 28

Accepted Solution

by:
vinnyd79 earned 400 total points
ID: 17770693
Maybe something like this:

Dim fso,wShell,fil,Server,wExec,upData,upTime,arrTime,x

Set fso = CreateObject("Scripting.FileSystemObject")
Set wShell = CreateObject("Wscript.Shell")

Set fil = fso.OpenTextFile("C:\Servers.txt",1,True)

While Not fil.AtEndOfStream

Server = fil.ReadLine

' make sure line is not blank
if Trim(Server) <> "" Then

Set wExec = wShell.Exec("uptime " & Server)

Do Until wExec.Status
    Wscript.Sleep 200
Loop

' get results
upData = wExec.StdOut.Readall

' get data after the ':' into upTime var
upTime = Trim(Mid(upData,Instr(upData,":") + 1))

' split it by comma
arrTime = Split(upTime,",")

' reset upTime to hold days
upTime = Left(arrTime(0),Instr(arrTime(0),"d") -1) & "day(s),"

' add hours. if len is 1 add a 0 to the front
Select Case Len(Trim(Left(arrTime(1),Instr(arrTime(1),"h") -1)))
      Case 1
            upTime = upTime & "0" & Trim(Left(arrTime(1),Instr(arrTime(1),"h") -1)) & ":"
      Case 2
            upTime = upTime & Trim(Left(arrTime(1),Instr(arrTime(1),"h") -1)) & ":"
End Select

' add minutes. if len is 1 add a 0 to the front
Select Case Len(Trim(Left(arrTime(2),Instr(arrTime(2),"m") -1)))
      Case 1
            upTime = upTime & "0" & Trim(Left(arrTime(2),Instr(arrTime(2),"m") -1))
      Case 2
            upTime = upTime & Trim(Left(arrTime(2),Instr(arrTime(2),"m") -1))
End Select

MsgBox  Server & " Has been up for " & upTime

End If

' get next line
Wend

fil.Close
Set fil = Nothing
Set fso = Nothing
Set wShell = Nothing
0
 
LVL 23

Assisted Solution

by:basicinstinct
basicinstinct earned 100 total points
ID: 17770882
How about this (just change "c:\whatever\mytst.bat" to the path to your batch file):

'Copy this code and paste it into an empty text file, then save as whatever.vbs
Set oShell = CreateObject("WScript.Shell")
oShell.Run "c:\whatever\mytst.bat", 0, True
0
 
LVL 1

Author Comment

by:johndarby
ID: 17777645
Vinny, you are a great instructor! I appreciate the time you took to explain the details. When I finally get a handle on scripting in VB, I plan to tackle C#. But until then, I am thankful for your splendid tutelage.

basicinstinct,
thanks for your input as well. Maybe I should give a more through explanation with my next question. I am definately the learner here and am seeking ways to identify and remember good syntax. Sometimes, porting from shell to VBS helps.

Thanks both!
JohnD
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question