如何在没有 Cscript/Wscript 的情况下从命令行运行 VBScript [英] How to run VBScript from command line without Cscript/Wscript

查看:43
本文介绍了如何在没有 Cscript/Wscript 的情况下从命令行运行 VBScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 VBScript 的初学者.我用谷歌搜索了一下&知道我们可以通过执行以下命令从命令行运行 VBScript:

I am a beginner in VBScript. I googled it & got to know that we can run VBScript from command line by executing below command:

例如我的 vbscript 名称是 Converter.vbs &它存在于文件夹 D:\VBS 中.

For Example my vbscript name is Converter.vbs & it's present in folder D:\VBS.

我可以通过以下方法运行它:

I can run it through following methods:

CScript "D:\VBS\Converter.vbs"

WScript "D:\VBS\Converter.vbs"

现在我想执行上面的 VBScript without CscriptWscript 命令,只需输入VBscript 名称,即转换器.

Now I would like to execute above VBScript without Cscript or Wscript command by simply typing the name of VBscript name i.e. Converter.

我不想每次都指定 VBSCRIPT 的完整路径.

I DON'T WANT TO SPECIFY THE FULL PATH OF VBSCRIPT EVERYTIME.

谁能指导我如何做到这一点?

Can anyone please guide me on how to do that ?

推荐答案

我将把它分解成几个不同的部分,因为每个部分都可以单独完成.(我看到了类似的答案,但我将在这里给出更详细的解释..)

I'll break this down in to several distinct parts, as each part can be done individually. (I see the similar answer, but I'm going to give a more detailed explanation here..)

第一部分,为了避免键入CScript"(或WScript"),您需要告诉Windows 如何启动* .vbs 脚本文件.在我的 Windows 8 中(我无法确定所有这些命令在旧版 Windows 中的工作方式完全相同,但过程是相同的,即使您必须稍微更改命令),启动控制台窗口(又名命令提示符",或又名[错误地]dos prompt")并输入assoc .vbs".这应该会导致如下响应:

First part, in order to avoid typing "CScript" (or "WScript"), you need to tell Windows how to launch a * .vbs script file. In My Windows 8 (I cannot be sure all these commands work exactly as shown here in older Windows, but the process is the same, even if you have to change the commands slightly), launch a console window (aka "command prompt", or aka [incorrectly] "dos prompt") and type "assoc .vbs". That should result in a response such as:

C:\Windows\System32>assoc .vbs
.vbs=VBSFile

使用它,然后键入ftype VBSFile",这将导致响应:

Using that, you then type "ftype VBSFile", which should result in a response of:

C:\Windows\System32>ftype VBSFile
vbsfile="%SystemRoot%\System32\WScript.exe" "%1" %*

-或-

C:\Windows\System32>ftype VBSFile
vbsfile="%SystemRoot%\System32\CScript.exe" "%1" %*

如果这两个已经如上定义,则您的 Windows 已经设置为知道如何启动 * .vbs 文件.(顺便说一句,WScript 和 CScript 是同一个程序,使用不同的名称.WScript 像 GUI 程序一样启动脚本,CScript 像命令行程序一样启动它.有关这些详细信息,请参阅其他站点和/或文档和警告.)

If these two are already defined as above, your Windows' is already set up to know how to launch a * .vbs file. (BTW, WScript and CScript are the same program, using different names. WScript launches the script as if it were a GUI program, and CScript launches it as if it were a command line program. See other sites and/or documentation for these details and caveats.)

如果其中一个命令没有如上响应(或类似响应,如果assoc报告的文件类型和/或ftype报告的执行命令具有不同的名称或位置),您可以自己输入:

If either of the commands did not respond as above (or similar responses, if the file type reported by assoc and/or the command executed as reported by ftype have different names or locations), you can enter them yourself:

C:\Windows\System32>assoc .vbs=VBSFile

-和/或-

C:\Windows\System32>ftype vbsfile="%SystemRoot%\System32\WScript.exe" "%1" %*

您还可以键入help assoc"或help ftype"以获取有关这些命令的更多信息,当您想通过以下方式自动运行某些程序时,这通常很方便只需键入具有特定扩展名的文件名.(不过要小心,因为某些文件扩展名是由 Windows 或您可能安装的程序专门设置的,因此它们可以正常运行.始终检查由 assoc/ftype 报告的当前分配的值,并将它们保存在某个文本文件中,以防万一恢复它们.)

You can also type "help assoc" or "help ftype" for additional information on these commands, which are often handy when you want to automatically run certain programs by simply typing a filename with a specific extension. (Be careful though, as some file extensions are specially set up by Windows or programs you may have installed so they operate correctly. Always check the currently assigned values reported by assoc/ftype and save them in a text file somewhere in case you have to restore them.)

第二部分,在从控制台窗口输入命令时避免输入文件扩展名.下)部分.当您键入命令时,让我们使用querty"作为示例命令,系统将首先尝试在其内部命令列表中查找该命令(通过系统本身的 Windows 注册表中的设置,或者在这种情况下编程CMD.EXE).由于没有这样的命令,它会尝试在当前的 %PATH% 环境变量中查找该命令.在旧版本的 DOS/Windows 中,CMD.EXE(和/或 COMMAND.COM)会自动将文件扩展名.bat"、.exe"、.com"和可能的.cmd"添加到您的命令名称中.键入,除非您明确键入扩展名(例如querty.bat"以避免错误地运行querty.exe").在更现代的 Windows 中,它将尝试 %PATHEXT% 环境变量中列出的扩展.所以你所要做的就是将 .vbs 添加到 %PATHEXT%.例如,这是我的 %PATHEXT%:

Second part, avoiding typing the file extension when typing the command from the console window.. Understanding how Windows (and the CMD.EXE program) finds commands you type is useful for this (and the next) part. When you type a command, let's use "querty" as an example command, the system will first try to find the command in it's internal list of commands (via settings in the Windows' registry for the system itself, or programmed in in the case of CMD.EXE). Since there is no such command, it will then try to find the command in the current %PATH% environment variable. In older versions of DOS/Windows, CMD.EXE (and/or COMMAND.COM) would automatically add the file extensions ".bat", ".exe", ".com" and possibly ".cmd" to the command name you typed, unless you explicitly typed an extension (such as "querty.bat" to avoid running "querty.exe" by mistake). In more modern Windows, it will try the extensions listed in the %PATHEXT% environment variable. So all you have to do is add .vbs to %PATHEXT%. For example, here's my %PATHEXT%:

C:\Windows\System32>set pathext
PATHEXT=.PLX;.PLW;.PL;.BAT;.CMD;.VBS;.COM;.EXE;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY

请注意,扩展名必须包含.",并以;"分隔,并且 .VBS 列在 .CMD 之后,但在 .COM 之前.这意味着如果命令处理器 (CMD.EXE) 找到多个匹配项,它将使用列出的第一个匹配项.也就是说,如果我有 query.cmd、querty.vbs 和 querty.com,它将使用 querty.cmd.

Notice that the extensions MUST include the ".", are separated by ";", and that .VBS is listed AFTER .CMD, but BEFORE .COM. This means that if the command processor (CMD.EXE) finds more than one match, it'll use the first one listed. That is, if I have query.cmd, querty.vbs and querty.com, it'll use querty.cmd.

现在,如果您想一直这样做而不必一直设置 %PATHEXT%,则必须修改系统环境.在控制台窗口中键入它只会在该控制台窗口会话中更改它.我将把这个过程留给读者作为练习.:-P

Now, if you want to do this all the time without having to keep setting %PATHEXT%, you'll have to modify the system environment. Typing it in a console window only changes it for that console window session. I'll leave this process as an exercise for the reader. :-P

第三部分,无需总是输入完整路径即可运行脚本.与第二部分相关的这一部分自 DOS 时代就已存在.只需确保该文件位于 %PATH% 环境变量中列出的目录之一(文件夹,对于您的 Windows 用户!).我的建议是创建自己的目录来存储您经常创建或使用的各种文件和程序从控制台窗口/命令提示符(也就是说,不要担心对从开始菜单或任何其他方法运行的程序执行此操作.仅控制台窗口.除非您知道自己在做什么,否则不要乱用 Windows 或自动安装程序安装的程序).

Third part, getting the script to run without always typing the full path. This part, in relation to the second part, has been around since the days of DOS. Simply make sure the file is in one of the directories (folders, for you Windows' folk!) listed in the %PATH% environment variable. My suggestion is to make your own directory to store various files and programs you create or use often from the console window/command prompt (that is, don't worry about doing this for programs you run from the start menu or any other method.. only the console window. Don't mess with programs that are installed by Windows or an automated installer unless you know what you're doing).

就我个人而言,我总是为批处理文件创建一个C:\sys\bat"目录,为 *.exe 和 *.com 文件创建一个C:\sys\bin"目录(例如,如果你下载类似md5sum",一个 MD5 校验和实用程序),一个用于 VBScripts 的C:\sys\wsh"目录(和 JScripts,命名为wsh",因为两者都是使用Windows Scripting Host"或wsh"程序执行的),以及很快.然后我将这些添加到我的系统 %PATH% 变量(控制面板 -> 高级系统设置 -> 高级选项卡 -> 环境变量按钮),这样当我输入它们时,Windows 总能找到它们.

Personally, I always create a "C:\sys\bat" directory for batch files, a "C:\sys\bin" directory for * .exe and * .com files (for example, if you download something like "md5sum", a MD5 checksum utility), a "C:\sys\wsh" directory for VBScripts (and JScripts, named "wsh" because both are executed using the "Windows Scripting Host", or "wsh" program), and so on. I then add these to my system %PATH% variable (Control Panel -> Advanced System Settings -> Advanced tab -> Environment Variables button), so Windows can always find them when I type them.

将所有三个部分结合起来 将导致配置您的 Windows 系统,以便您可以在任何可以键入命令行命令的地方,只需键入基本文件名即可启动 VBScript.您几乎可以对任何文件类型/扩展名执行相同的操作;正如您可能在我的 %PATHEXT% 输出中看到的那样,我的系统也设置为运行 Perl 脚本 (.PLX;.PLW;.PL) 和 Python (.PY) 脚本.(我也把C:\sys\bat;C:\sys\scripts;C:\sys\wsh;C:\sys\bin"放在我的%PATH%前面,把各种批处理文件,脚本文件, 等等, 在这些目录中,所以 Windows 总是可以找到它们.如果你想覆盖"一些命令,这也很方便:将 * .bat 文件放在路径中的第一个让系统在 * .exe 文件之前找到它们, 例如, 然后 * .bat 文件可以通过提供实际 *.exe 文件的完整路径来启动实际程序.查看批处理文件编程"的各个站点以获取详细信息和其他示例命令行..它还没有死!)

Combining all three parts will result in configuring your Windows system so that anywhere you can type in a command-line command, you can launch your VBScript by just typing it's base file name. You can do the same for just about any file type/extension; As you probably saw in my %PATHEXT% output, my system is set up to run Perl scripts (.PLX;.PLW;.PL) and Python (.PY) scripts as well. (I also put "C:\sys\bat;C:\sys\scripts;C:\sys\wsh;C:\sys\bin" at the front of my %PATH%, and put various batch files, script files, et cetera, in these directories, so Windows can always find them. This is also handy if you want to "override" some commands: Putting the * .bat files first in the path makes the system find them before the * .exe files, for example, and then the * .bat file can launch the actual program by giving the full path to the actual *. exe file. Check out the various sites on "batch file programming" for details and other examples of the power of the command line.. It isn't dead yet!)

最后一点:一定要查看其他一些网站,了解各种警告和注意事项.这个问题提出了一个名为converter.vbs"的脚本,它非常接近命令convert.exe",这是一个 Windows 程序,用于将您的硬盘驱动器从 FAT 文件系统转换为 NTFS 文件系统.如果您打字错误,请破坏您的硬盘!

One final note: DO check out some of the other sites for various warnings and caveats. This question posed a script named "converter.vbs", which is dangerously close to the command "convert.exe", which is a Windows program to convert your hard drive from a FAT file system to a NTFS file system.. Something that can clobber your hard drive if you make a typing mistake!

另一方面,使用上述技术,您也可以避免此类错误.以 CONVERT.EXE 为例.. 将其重命名为REAL_CONVERT.EXE"之类的内容,然后创建一个类似C:\sys\bat\convert.bat"的文件,其中包含:

On the other hand, using the above techniques you can insulate yourself from such mistakes, too. Using CONVERT.EXE as an example.. Rename it to something like "REAL_CONVERT.EXE", then create a file like "C:\sys\bat\convert.bat" which contains:

@ECHO OFF
ECHO !DANGER! !DANGER! !DANGER! !DANGER, WILL ROBINSON!

ECHO This command will convert your hard drive to NTFS! DO YOU REALLY WANT TO DO THIS?!
ECHO PRESS CONTROL-C TO ABORT, otherwise..

REM "PAUSE" will pause the batch file with the message "Press any key to continue...",
REM and also allow the user to press CONTROL-C which will prompt the user to abort or
REM continue running the batch file.
PAUSE

ECHO Okay, if you're really determined to do this, type this command:
ECHO.    %SystemRoot%\SYSTEM32\REAL_CONVERT.EXE
ECHO to run the real CONVERT.EXE program. Have a nice day!

您还可以在现代 Windows 中使用 CHOICE.EXE 来让用户输入y"或n",如果他们真的想继续的话,等等..再次,批处理(和脚本)文件的力量!

You can also use CHOICE.EXE in modern Windows to make the user type "y" or "n" if they really want to continue, and so on.. Again, the power of batch (and scripting) files!

这里有一些关于如何使用所有这些能力的优秀资源的链接:

Here's some links to some good resources on how to use all this power:

http://ss64.com/

http://www.computerhope.com/batch.htm

http://commandwindows.com/batch.htm

http://www.robvanderwoude.com/batchfiles.php

这些站点中的大多数都面向批处理文件,但其中的大部分信息适用于运行任何类型的批处理 (* .bat) 文件、命令 (* .cmd) 文件和脚本 (* .vbs, *.js、* .pl、* .py 等)文件.

Most of these sites are geared towards batch files, but most of the information in them applies to running any kind of batch (* .bat) file, command (* .cmd) file, and scripting (* .vbs, * .js, * .pl, * .py, and so on) files.

这篇关于如何在没有 Cscript/Wscript 的情况下从命令行运行 VBScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆