Direct vbs execution in Batch Processing

Source: Internet
Author: User

Example batch processing: test. bat

Code:Copy codeThe Code is 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 above code cleverly utilizes the syntax features of bat and vbs to allow the same file to be identified by cmd.exe for batch processing, so that wscrpt.exe can be recognized as vbs and comply with the syntax of both, and ensure that there are no errors, to ensure high compatibility between the two. It's called bat/vbs composite Programming (Hybrid Programming)
A rough explanation of the Code
Reference:
: On Error Resume Next

Cmd.exe identifies a comment
Wscript.exe identifies a branch in the vbs syntax, and then On Error Resume Next, that is, let WSH ignore some errors.

Reference:
Start wscript-e: vbs "% ~ F0"

Cmd.exe recognizes that wscript.exe is started with the following parameters:
①-E: vbs is used to parse the file itself
② "% ~ F0 "indicates the batch processing itself.

Wscript.exe recognizes this sentence as: Call a function called start. The function parameter is the variable wscript, And then subtract e from the result of this function. Next there is another one: branch, and then call a function named vbs. The parameter is a character: "% ~ F0"

This sentence is the most exquisite, because it successfully let the vbs engine explain a batch of processing, and there is no error! Of course, these start () and vbs () functions do not exist, but they are executed as commands by cmd.exe. Why not use wscript // e: vbs "% ~ F0 "to execute? Vbs parsing will go wrong.

The core idea of this code has been introduced. Next, in order for the batch processing to exit immediately after vbs calls itself, we need to exit or goto: eof, but goto call exit is a keyword in vbs, therefore, we can only use exit sub that complies with the vbs syntax, So we add
Sub bat, in fact, cmd.exe looks for a command called sub.exe, but this command does not exist, and cmd.exe skips. Add an exit sub and end sub in 6 or 7 sentences to end the batch processing and comply with the vbs syntax.

The echo off & cls indicates that the batch processing is equivalent to @ echo off, but the vbs does not recognize the @ symbol, so it is changed to echo off & cls. The vbs can be parsed to call an echo () function. The parameter is off & cls, that is, the two strings off and cls are added together.

Well, next, you can run the vbs script, which is as efficient as the real vbs!

The benefit of this Code is that you do not need to generate temporary files. In fact, using echo, more, or find to generate temporary vbs is a waste of system resources. Using the code I wrote completely saves these troubles. Directly mixed programming, start wscript-e: vbs "% ~ F0 "is the boundary. Write batch processing above and vbs below!

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.