Is there a way to run VBScript as a command line argument and not a file?

Say you have a very simple one-line VBScript command to run, something like getting the day of the month for yesterday:

wscript.echo day(date()-1)

      

At the moment, I must either have a separate file .vbs

and then run it with:

for /f %%a in ('cscript //nologo myscript.vbs') do set dd=%%a

      

or, if I don't want to maintain the script separately:

echo wscript.echo day(date()-1) >temp.vbs
for /f %%a in ('cscript //nologo temp.vbs') do set dd=%%a
del /s temp.vbs >nul: 2>nul:

      

Now I don't mind the nasty statement for

for capturing output, I just wanted to know if there is a way to avoid having to maintain a separate VBScript file or create and destroy it on the fly.

Something like:

cscript //nologo //exec wscript.echo day(date()-1)

      

would be perfect, but I know I do cscript

n't support this.

Is there a way to achieve this, keeping in mind I want to write its output to a variable from within a cmd

script?

+3


source to share


1 answer


You can write one script that iterates over Eval : argument (s):

WScript.Echo Eval(WScript.Arguments(0))

      



or one script that echoes the result of the calculation indicated by its (first) argument:

Select Case WScript.Aruments(0)
  Case "prevday"
    wscript.echo day(date()-1)
  Case "answer"
    WScript.Echo 4711
  ...

      

+2


source







All Articles