All Forums >> [Scripting] >> Post a VBScript >> Launch vbs from the right-click menu (advanced) Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
INTRODUCTION: You can easily launch a script in a vbs file from the context menu (right click menu) of Windows Explorer if you know how to add the registry key, or by using simple softwares like Context Edit. Problems arise when you right-click on a selection of more than one file. Instead of one instance of your script that will process all the files you selected, each file selected will launch a separate instance which will process only this file and not the other and eventualy you will have dozen of message boxes poping up all around your screen. This problem doesn't happends when you drag and drop your selection on the script but that's not easy or fast. One solution is to put the script in the "Send To" folder. If you are satisfied with this solution you can stop reading here.
Unfortunately for my lazy mouse, it's still two clicks and one submenu too far . + The 'Send To" folder is supposed to send files to some place, not to process them with some programs and that could cause confusion. + Your "Send To" folder will be very quickely crowded with all sort of vbs files, mixed with various destination folders, all this not sorted.
So this the other solution I created: I will immediately say that's not a perfect solution! I'm not a programmer and this is more a round-about than anything else. It's based on a race against the clock rather than real system tweaks. KNOWN BUGS AND LIMITATIONS: -You will see the sandglass during a time that depends on the number of files selected. With one file you will not see it. With 50 it could take of to a couple of second. -The first time in the day you use this method to launch a script, it may cause an error and not process all files. That's because the files took more time to load, as the computer is still half asleep. Wake him up and he won't do the same mistake again. -There is a limitation on the number of files you can select. I have tested it succesfuly with a selcetion of 120 files. maybe it can take more or less on your PC, but don't expect much in the triple digits. -Your script can be named only with minimal names, like 2 or 3 letters, maybe 4. And the lenght of the path is also limited (see bellow).
FILES You will need to create these files: C:\x.nul C:\t.vbs C:\t.txt
and the main script for the example: C:\Program Files\vbs\tst.vbs
The folder "C:\Program Files\vbs\" will contain your scripts. The name of your scripts must be as short as possible (see bellow). You can put them in another folder but you will have to modify the following scripts accordingly (see bellow). ____________________________________ INSTALLATION SCRIPT: This script will create a menu called "test" that will work on txt files. Your script, and for the test, "tst.vbs" must be located in "C:\Progra~1\vbs\". As you can see the parameter contained in the reg key is a complete command line, transmitted directly to WSH. It will launch an instance of "t.vbs" for each selected file. "t.vbs" + the command line passed to it, will append the filepath represented by the "%1" parameter in the list in "t.txt". Then it will launch the main script if he founds "x.nul". Your script will then delete "x.nul" so that the next instances of "t.vbs" won't launch the main script again. When "t.vbs" doesn't find "x.nul", it only adds the filepath to the list. Because sometimes 2 or 3 instances of "t.vbs" find "x.nul" before it get renamed, there is a second barrier in the main script. Only after that, the main script will read the list and do what it has to do with it.
IMPORTANT If you don't want to put your scripts in "C:\Program Files\vbs\", you can put them somewhere else but bear in mind that the shortpath (here: "C:\Progra~1\vbs\") cannot be longer than the current one. For the same reason, the name of your script can contain only a maximum of 3, maybe 4 characters. That's because we will put the vbs filepath with many other things in the registry key variable, and it's limited to 256 characters. To use it with other files, you will have to know the file type name and location in the registry (here: "HKCR\txtfile").
___________________________________ EXPLANATION: C:\x.nul Is an empty file. Its role is only to exists and be there to say the main script can go on. C:\t.vbs Is a mini script to launch the main script C:\t.txt Is keeping temporarily the list of files to be processed
____________________________________ THE T SCRIPTS: This is the content of "t.vbs." As you can see it's very small but I wish I could make it even smaller because it must be processed by WSH as fast as possible to avoid errors.
____________________________________ THE MAIN SCRIPT This it the sample script "tst.vbs", refered above as the main script. It's maybe the simpliest text viewer on earth. Please remove the quotes once you don't need them because it's also important that the script be porcessed by WSH as fast as possible
< Message edited by Fredledingue -- 3/8/2006 10:36:59 AM >
Excellent information here. I doubt that I will personally use it since I do everything from the command line any way and I am too lazy to even set it up.
This is another version: -No limit for the number of file selected (it even beats the limit number in the Argument object!) -More reliable (with a lot of files, the 1st version missied sometimes one) -You can name your script with long names
but: -The "T" script, as described above now work for only one script. (in the previous version only one T script was needed for all the scripts) So you will need a T script (of course named differently) for each "main" script. In the T script you will have to write the path of the main script. -It may be a bit slower
The final variable in the reg key should read: C:\WINDOWS\WScript.exe "C:\PROGRA~1\VBS\T_VERS~1.VBS" "%1" ____________________________________ THE T SCRIPTS:
The main script remains unchanged (see above) I still didn't test extensively this method. I will post more info or update the above post later...