Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to execute vbs directly in batch processing

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to execute vbs directly in batches. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Sample batch: test.bat

Code:

The 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 skillfully makes use of the syntax features of bat and vbs, so that the same file is recognized by cmd.exe as a batch, so that wscrpt.exe recognizes as vbs, and conforms to the syntax of both, and ensures that there are no errors, ensuring a high degree of compatibility between the two. Call it bat/vbs compound programming (Hybrid Programming).

Give a rough explanation of the code

Reference:

: On Error Resume Next

Cmd.exe is recognized as a comment

Wscript.exe recognizes that it represents a branch in vbs syntax, and then On Error Resume Next, that is, asks WSH to ignore some errors.

Reference:

Start wscript-e:vbs "% ~ f0"

Cmd.exe is recognized as: start wscript.exe, and its parameters are:

①-e:vbs setting to parse the file itself in vbs

② "% ~ f0" refers to the batch itself.

Wscript.exe recognizes this as: call a function called start, whose argument is the variable wscript, and then subtract e from the result of this function. And then there's another: branch, and then another call to a function called vbs, with the argument to the character "% ~ f0".

This sentence is the most ingenious because it successfully got the vbs engine to interpret a batch without errors! Of course, these start () and vbs () functions do not exist, but they are executed by cmd.exe as commands. Why not use wscript / / e:vbs "% ~ f0" to execute? Vbs parsing will make mistakes, hehe

The core idea of this code has been introduced. Next, in order for the batch to exit immediately after calling itself with vbs, we need exit or goto: eof, but goto call exit is another keyword in vbs, so we can only use exit sub that conforms to vbs syntax, so we add one in the second sentence

Sub bat, in fact, cmd.exe is looking for a command called sub.exe, but this command does not exist, cmd.exe skips. Then add an exit sub and end sub to the 6 and 7 sentences to let the batch end, while conforming to the syntax of vbs

That echo off & cls batch means @ echo off, but vbs doesn't recognize the @ symbol, so change it to echo off & cls, and vbs can be parsed to call a function called echo () with the argument off & cls, which is the addition of the two strings off and cls.

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

The advantage of this code is that there is no need to generate temporary files. Its practical echo or more or find to generate temporary vbs is a waste of system resources, with this code I wrote, it is completely free of these troubles. Direct mixed programming, to start wscript-e:vbs "% ~ f0" as the limit, write batch above, write vbs below, parallel!

Thank you for reading! This is the end of this article on "how to execute vbs directly in batch processing". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report