# How to make a VBS commmand to open a file



## simr (May 13, 2013)

Hi, I would like to create a *.vbs* file using *VBScript*, and the task should be that if a certain file/folder is selected then a specific file should be executed. For instance, if file/folder X is selected then a certain sound effect or message box or any other file that I put in to the code should be activated/executed/automatically opened.

Does anybody know how to create such a *VBScipt* code?

Is it also possible to create a batch file or *Command Prompt* - command that could do it?

Thank you in advance!


----------



## simr (May 13, 2013)

Thank you MelisaStewart!

If you can elaborate and explain a little more, I would appreciate it, since I don't fully understand what you mean.

Thanks again.


----------



## Rollin_Again (Sep 4, 2003)

So from what I understand you want to run the VBS script and have the user to be prompted with a dialogue to select a file/folder before taking the appropriate action? Is there any particular reason you want this to be done using a stand-alone VBS script instead of a VBA macro instead which is normally run from MS Office applications and is more versatile in my opinion 

If you can provide a few more details on how you plan on using the code in a real-world scenario it would make it easier to offer suggestions. Also please indicate what operating system you are using. 


Rollin


----------



## simr (May 13, 2013)

Thank you Rollin!

I have a Vista.

Let me give you an example of what I want to do with the *VBS* file. One of the things I'd like to do is create a *VBS* file, *Batch* file using a *Command Prompt* line or a *HTA* file (using *HTML application*) which will make that when someone opens folder X or file Y, a message box should appear, or a sound effect should happen (from a pre-arranged audio file) or a video/music file should start playing, or really any effect that I want to happen should occur as a result of said folder/file being double clicked on.

Another example of what I'd like to do is to create a file (again *VBS*, *Batch* or *HTA*) that will change the target of a shortcut when I click on the *VBS/Batch/HTA* file. An example of a shortcut target change would be from:
"*C:\Windows\nircmd.exe standby*"
to
"*C:\Windows\nircmd.exe hibernate*"
In other words, the shortcut uses the same program but the function changes. Or for that matter, to change the whole address to something else entirely.

One more example, I'd like to know what the *VBS/Batch/HTA* code is that causes a file/folder/program to close when another one is opened, i.e. opening file/folder/program A will close file/folder/program B.

Thank you again!


----------



## Triple6 (Dec 26, 2002)

simricht said:


> Thank you MelisaStewart!
> 
> If you can elaborate and explain a little more, I would appreciate it, since I don't fully understand what you mean.
> 
> Thanks again.


Ignore that post, it's just a brainless spammer.


----------



## simr (May 13, 2013)

Triple6 said:


> Ignore that post, it's just a brainless spammer.


Well I was indeed struggling to understand what *MelisaStewart* was saying in his (now deleted) post.
I see now that I wasn't the only one with difficulty understanding it.
(Perhaps *MelisaStewart* can write in now and clarify what he meant there.)


----------



## Triple6 (Dec 26, 2002)

simricht said:


> Well I was indeed struggling to understand what *MelisaStewart* was saying in his (now deleted) post.
> I see now that I wasn't the only one with difficulty understanding it.
> (Perhaps *MelisaStewart* can write in now and clarify what he meant there.)


Spammers are banned, they will not be returning to clarify anything.


----------



## TonyB25 (Jan 1, 1970)

In order for something to happen when a file or folder is selected, it would have to be running as a service and intercept calls to Explorer.exe. I don't think you can do that with a simple script file.


----------



## simr (May 13, 2013)

What about with HTA?
And what about changing the target of a shortcut?


----------



## TonyB25 (Jan 1, 1970)

I don't know what HTA is. Making a shortcut won't intercept Explorer calls or run as a service. What you want to do is probably more complicated than you're thinking.


----------



## simr (May 13, 2013)

HTA stands for HTML Application, it's how you make HTML pages for the web, but you could also make functions for your PC which doesn't involve the web using HTA.


----------



## simr (May 13, 2013)

TonyB25 said:


> In order for something to happen when a file or folder is selected, it would have to be running as a service and intercept calls to Explorer.exe. I don't think you can do that with a simple script file.


You can for instance change multiple file names with a simple Command line even though those files aren't running. Apparently there are things you can do to files that aren't running. I assume that the same is true for VBScript.


----------



## TonyB25 (Jan 1, 1970)

Changing file names is different from performing an action when a file is accessed. To do that, the program has to be running at all times so it can monitor what folders are being opened.


----------



## simr (May 13, 2013)

What about doing this? I assume that this is just like changing file names.


simricht said:


> ...I'd like to do is to create a file (again *VBS*, *Batch* or *HTA*) that will change the target of a shortcut when I click on the *VBS/Batch/HTA* file. An example of a shortcut target change would be from:
> "*C:\Windows\nircmd.exe standby*"
> to
> "*C:\Windows\nircmd.exe hibernate*"
> In other words, the shortcut uses the same program but the function changes. Or for that matter, to change the whole address to something else entirely.


Thank you TonyB25!


----------



## TonyB25 (Jan 1, 1970)

You change the shortcut path by right-clicking on a file, and clicking Properties, and then changing the path. At what point is your custom program supposed to take over? How is it monitoring Windows to know someone right-clicked on that shortcut?


----------



## simr (May 13, 2013)

No, what I'm saying is that by entering a Command line/Batch script or VBScript with the right code and the new target for the shortcut file (all of which is put in the script) you can change the target shortcut by clicking on the Batch file or VBS file.


----------



## TonyB25 (Jan 1, 1970)

Yes, you can change the target of a shortcut. How does that change what happens when someone opens a folder?


----------



## simr (May 13, 2013)

My first question was, is there a code to cause one file to be activated when another is opened. Since apparently there isn't, my next question is - what's the Command line or VBS code to change a shortcut target?

Thanks!


----------



## valis (Sep 24, 2004)

starting to wonder about the 'why' here.......


----------



## simr (May 13, 2013)

valis said:


> starting to wonder about the 'why' here.......


In post #14 you have your answer Valis.
And what do your 6 dots mean? What could the "why" be?


----------



## valis (Sep 24, 2004)

it just seems to me that this is a rather unwieldy way to go about this problem.....but if you have your reasons, you have your reasons. I don't like to see .hta files run rampant, as those are easily misused.

thanks, 

v


----------



## TonyB25 (Jan 1, 1970)

How about you create a shortcut the way you want and then just copy it to people's computers?


----------



## simr (May 13, 2013)

valis said:


> it just seems to me that this is a rather unwieldy way to go about this problem.....but if you have your reasons, you have your reasons. I don't like to see .hta files run rampant, as those are easily misused.
> 
> thanks,
> 
> v


I only brought up the HTA option as a last resort if there's no way to create a batch file with a command line that could change a shortcut target. I personally like Batch files with command lines better then HTA files.

During the day I have a shortcut desktop icon which has the target:
*"C:\Windows\nircmd.exe standby"*
Meaning when I double click it, my PC goes into standby (neat shortcut, no?).
At night I go into the shortcut and change its target to:
*"C:\Windows\nircmd.exe hibernate"*
so that when I click it - it'll hibernate over night.
To make things more convenient for me, I asked above if there's a way to make a Batch file or VBS file (if not than HTA as a last ditch attempt) so that instead of having to go and manually change the target every morning to "Sleep" and every evening to "Hibernate" I could just have 2 Batch files that by clicking on them I will accomplish the aforementioned tasks automatically.

I hope I clarified myself and the purpose of my quarry.

I thank everyone for your attempts at assisting me, though I guess I won't have such a Batch file, but that's okay, as the small things in life needn't bother anyone.


----------



## valis (Sep 24, 2004)

is there a reason you don't just have two shortcuts?


----------



## simr (May 13, 2013)

Good question valis.
Yes, the reason is since it's a computer that a few people use and not just me, and for some people it gets a little confusing when there's too much for them to remember, I keep it simple with one icon on the desktop that they click when they're done, and I change it from day to night.


----------



## valis (Sep 24, 2004)

trust me, I had a VERY similar problem, hence my questioning.......I literally had to relabel the shortcuts 1 and 2, as AM and PM wasn't getting it done.......


----------



## simr (May 13, 2013)

Right, so I have a shortcut icon on the desktop with a picture of a computer screen and a moon on it (thank windows for including many icons to choose from) to illustrate the computer sleeping, and whoever is done with the PC double clicks it. By day it makes the PC sleep, and at night - hibernate.
Because before I started with this, people would just shut the screen when they were done (I have a laptop) and from all the shutting and opening, the screen started flickering many times, so I had to institute the current way of shutting the computer so my screen shouldn't go totally down, though I don't know how to repair the current flickering of the screen which is very annoying, aside from paying big bucks (relatively speaking for such a problem) to a repair shop which I don't want to do, since my PC is old as it is, and has many other issues as well, hence my other question forums on this wonderful website.


----------



## valis (Sep 24, 2004)

so we can mark the software part _finit_ and focus on the hardware issue, e.g., the monitor flicker? If so, I'll just move the last post and create a new thread for you....

thanks,

v


----------



## simr (May 13, 2013)

Yes, screen flicker is a big problem, and very annoying...


----------

