Methods for directly executing VBS in batch processing _dos/bat

Source: Internet
Author: User
Tags goto
Sample Batch: Test.bat

Code:
Copy Code code as follows:

: On Error Resume Next
Sub bat
echo Off & CLS
echo Batching_codez_here_following_vbs_rules & Pause
Start Wscript-e:vbs "%~f0"
Exit Sub
End Sub
MsgBox "This is VBS"

The code above skillfully utilizes the syntax features of BAT and VBS, allowing the same file to be Cmd.exe recognized in batches, allowing the wscrpt.exe to be recognized as a VBS, and conforming to both the syntax and ensuring that there are no errors to ensure a high degree of compatibility between the two. Call it bat/vbs complex programming bar (Hybrid programming)
A rough explanation of the code
Reference:
: On Error Resume Next

Cmd.exe recognized as a piece of annotation
Wscript.exe This recognition, in the VBS syntax for the branch, then on Error Resume Next, that is, let WSH ignore some errors

Reference:
Start Wscript-e:vbs "%~f0"

Cmd.exe recognized as: Start Wscript.exe, its parameters are:
①-e:vbs to resolve file itself with VBS
② "%~f0" refers to this batch process itself.

Wscript.exe This sentence: Call a function called start, the function argument is wscript this variable, and then use the result of this function to subtract E. Next is another:, a branch, and then a function called VBS, and the argument is a character: "%~f0"

This is the most ingenious, because it successfully let the VBS engine explain a batch, and there is no error! Of course, these start (), VBS () functions do not exist, but they are executed by Cmd.exe as commands. Why not wscript//e:vbs "%~f0" to perform? VBS parsing is going to go wrong, huh?

The core idea of this code has been introduced. Below, to get the batch to call itself with a VBS, exit immediately, we need to exit or goto:eof, but Goto calls exit is a keyword in the VBS, so we can only use the Exit sub with the VBS syntax, so we add one in the second sentence
Sub bat, in fact, Cmd.exe looked for a command called Sub.exe, but this command does not exist, Cmd.exe skipped. Then add an Exit sub as well as an End sub in 6, 7 sentences so that the batch ends and the VBS syntax

The echo off & CLS, which means that the batch is equivalent to @echo off, but the VBS does not recognize the @ symbol, so it is changed to the echo off & CLS, and the VBS can parse to call a function called echo () with the argument off & CLS, i.e. two string off and CLS addition

Well, next, you can run the VBS script, and the efficiency is as high as the real VBS!

The advantage of this code is that you do not have to generate temporary files. In fact, the use of echo or more or find to generate temporary VBS is a waste of system resources, with the code I wrote, it completely eliminates these problems. Direct mixing programming, to start Wscript-e:vbs "%~f0" as the boundary, the above write batch processing, below write VBS, parallel!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.