Category Archives: Batch

Read output from VBS in CMD scripts

Here is a sample VB script, test.vbs:
  1. wscript.echo "Testoutput"
  2. wscript.echo "This is a test"
The strings echoed from the VBS script can be read from a calling CMD script, with a standard FOR loop using the options /F and delims as follows, test.cmd:
  1. @echo off
  2. FOR /F "usebackq delims=" %%i IN (`cscript test.vbs`) DO Set myvar=%%i
  3. echo Srcipt result = "%myvar%"
Notice that only the last line echoed from the VB script is stored. If you want to store multiple lines, use the FOR loop token option also. See Get date in Windows CMS script for an example.
Posted in Batch | Leave a comment

Get date in Windows CMD script

Apparently it depends on the Windows installation language, how the date function or %DATE% environment variable is formatted. So here is a very ugly hack to get a julian date (YYYYMMDDHHiiss) in a Windows batch script, independent off system locale settings:

  1. @echo off
  2. FOR /F "usebackq tokens=1-10 delims=+|NOW():- " %%a IN (`mysql -e "SELECT NOW();" -u root -ptest`) DO set datetime=%%a%%b%%c%%d%%e%%f
  3. echo %datetime%

Notice that this requires MySQL. If you do not like to use your MySQL root code, you can create a new user with absolutely no rights granted.

But again, this solution is stupid, I hope you will never need it or use it. I just thought I would share, since it was driving my co-worker crazy, and we ended up using it to name a MySQL dump.

Posted in Batch, MySQL | Tagged , , , , | 2 Comments