1

I am using the solution from here to convert .csv to .xlsx:

Convert .CSV to .XLSX using command line

Dim file, WB

With CreateObject("Excel.Application")
On Error Resume Next
For Each file In WScript.Arguments
    Set WB = .Workbooks.Open(file)
    WB.SaveAs Replace(WB.FullName, ".csv", ".xlsx"), 51
    WB.Close False
Next    
.Quit
End With

WScript.Echo "Done!"

I have tried running this from .cmd and everything works, but when you run it from cmd, the command just finishes right away though the vb script still processes. Is there a way to let cmd know that vb has finished? I'm trying to create a batch file, so it would be great to know when this part is finished before moving on to the next step. Thanks!

Community
  • 1
  • 1
jimjamian
  • 93
  • 1
  • 10
  • Maybe not quite a duplicate, but the following seems relevant: http://stackoverflow.com/q/187040/4996248 – John Coleman Nov 05 '16 at 22:53
  • 1
    Maybe something like [`start "" /wait wscript yourscript.vbs`](http://ss64.com/nt/start.html) could suffice. – JosefZ Nov 06 '16 at 00:53

2 Answers2

3

You're probably running the script by itself:

C:\path\to\your.vbs

Doing so runs the script with the associated interpreter, which in a normal installation is wscript.exe. Basically, the above is the same as

wscript.exe C:\path\to\your.vbs

wscript.exe launches scripts asynchronously, meaning it returns immediately while the script continues running in the background. For running scripts synchronously you need to use the commandline interpreter cscript.exe instead. Add the parameter //NoLogo to suppress the copyright/version info message.

cscript.exe //NoLogo C:\path\to\your.vbs

With that execution of your batch script will continue only after the VBScript terminates. Using the call command is not required.

You can change the default interpreter from wscript.exe to cscript.exe by running wscript.exe //h:cscript or cscript.exe //h:cscript as an administrator.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • What's the `//` all about? Command switches are `/`. From the `cmd` prompt `cscript.exe /nologo` works fine. – user692942 Nov 06 '16 at 16:33
  • Take a look at the output of `cscript /?`. I suppose the original intention was to distinguish between parameters for the interpreter and parameters for the script. Both `//NoLogo` and `/NoLogo` seem to work nowadays, but using the double slashes for interpreter parameters became a habit of mine. – Ansgar Wiechers Nov 06 '16 at 16:35
  • Ok, never noticed as host parameters have always worked for me with `/`. Always learning. – user692942 Nov 06 '16 at 16:38
  • Thank you very much. cscript works perfectly for me. – jimjamian Nov 08 '16 at 13:34
0

By using call cscript the batch script will wait until the script engine process terminates. /T allows you to set a timeout. The process gets interrupted if the run time exceeds the limit specified in seconds.

@echo off
call cscript //T:10 "%~dp0example.vbs" 
ECHO %ERRORLEVEL%
pause

Use wscript.quit in your vbs script to return an exit code. Error Numbers are stored in the Err.number property.

On Error Resume Next
WScript.Sleep 2000
Err.Raise 507 ' An exception occurred
wscript.quit Err.number