1

I got a script to ping servers which I used to execute going to the path where my file is located on CMD and type the following line

FileName ip logname.log

this is the script I run

hostIp      = wscript.arguments(0)
logfilename = wscript.arguments(1)
Set fso     = CreateObject("Scripting.FileSystemObject")
Set Shell   = CreateObject("Wscript.Shell")
' OpenTextFile Method requires a Const value
' (Over)Write = 2  Append = 8   
Set logfile = fso.OpenTextFile(logfilename, 8, True)
shellstring = "%comspec% /c ping -t -f -l 32 -w 1000 " & hostIP
Set oExec   = Shell.Exec(shellstring)
wscript.echo "Ping Error log With Timestamp - Ctrl + C to halt"
Do While oExec.StdOut.AtEndOfStream <> True
      pingline = Date & " " & Time & " " & oExec.StdOut.ReadLine
'      If InStr(pingline, "TTL=") = 0 Then
         logfile.WriteLine(pingline)
'      End If
Loop

however, it doesn't work anymore whenever I execute it now it opens the file instead of executing it. any ideas on how to fix this issue?

2
  • This looks like vbScript. This is executed with cscript from the command line. I.e. cscript <filename> <ip> <log> what is filename extension? Is it .vbs? You may have changed your file associations somehow. Commented Oct 12, 2017 at 4:15
  • nothing was changed and yes the extension is .vbs, what exactly do you mean by file associations? Commented Oct 12, 2017 at 5:22

1 Answer 1

0

If you use the syntax:

Filename ip logname.log

then the action will be derived from the registry.

On my computer, I have the following.

  1. Computer\HKEY_CLASSES_ROOT.vbs
    • (Default) REG_SZ VBSFile
  2. Computer\HKEY_CLASSES_ROOT\VBSFile\Shell\Open\Command
    • (Default) REG_EXPAND_SZ "%SystemRoot%\System32\WScript.exe" "%1" %*

If you don't want to leave it to chance, you will have to explicitly include WScript or CScript in your commandline, e.g.

CScript Filename ip logname.log

The most notable difference between CScript and WScript is the behavior of WScript.Echo. With CScript, text is echoed to stdout and is non blocking. With WScript, text is displayed in a modal dialog box and is blocking. The former is generally preferred if (1) you're invoking the script from a Command Prompt, (2) you're invoking the script from a scheduled task.

4
  • thanks for your answer, ill try it when I get back to work. what's the difference between WScript and CScript? and im trying to add the dale to the filelog like this Set logdate = Format(Now(),"dd/mm/yy") Set logfile = fso.OpenTextFile(logfilename & logdate , 8, True). is that ok? Commented Oct 12, 2017 at 5:13
  • @RildoGomez no. That is not correct. That would result in logname.log10/13/17 you've obliterated the file extension and I don't think slashes are valid in a filename. Commented Oct 12, 2017 at 12:15
  • @rildo-gomez I've updated my answer Re: difference between WScript and CScript? Commented Oct 12, 2017 at 21:15
  • It worked!! Thanks, im still working on my date thing tho :) Commented Oct 14, 2017 at 18:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .