1

Don't laugh.

I'm in a corporate environment with a locked down desktop. Installing third party apps is not an option.

Notepad is mostly okay, the biggest pain though is that it doesn't detect if a file is already open. I wind up with edits to files in two places.

So, I thought I would write a little vbs to check to see if the file might be open and instead of opening it twice, just raise the suspect window.

Dim Arg, var1
Set Arg = WScript.Arguments

'Parameter1, begin with index0
var1 = Arg(0)

set fso = CreateObject("Scripting.FileSystemObject")

Set oShell = CreateObject("WScript.Shell") 
If Not oShell.AppActivate( fso.GetFileName(var1) + " - Notepad") Then
   Dim objShell
   Set objShell = WScript.CreateObject( "WScript.Shell" )
   objShell.Exec("notepad.exe " + var1 )
   Set objShell = Nothing
End If

it works great. But...

I can't set a vbs to the target of an "Open With..." menu. It looks like I can do a .bat, .cmd, .exe, .pif. Manually selecting ".vbs" doesn't work. It will say that the .txt file "is not a valid win32 application"

How can I get a file's default action to open with a vbs script?

2
  • n.b., there are problems with this method, e.g, opening readme.txt in two directories would make it appear as though the second file contained the content from the first. I'll touch it up with a better method later, just worried about getting it working for now.
    – mgjk
    Jul 29, 2014 at 15:17
  • Are you able to utilize software that doesn't require installation, such as a portable version of Notepad++ or Atom? If not, I'd recommend suggesting to the IT department to add Atom, or a similar coding text editor to the company PCs (I'd recommend Atom due to it's cross platform support and customization ability by the end user), such as Microsoft's cross platform text editor VScode.
    – JW0914
    Sep 1, 2018 at 15:01

3 Answers 3

1

Call the vbscript from a batch file (.cmd) with a single line 'wscript.exe %1'. Put the cmd file in the send to list.

3
  • Thanks, it does work... the only thing that sucks is the black window which appears for a few seconds. Any thoughts about preventing it or reducing its visibility?
    – mgjk
    Jul 29, 2014 at 15:12
  • Use either start /B wscript.exe %1 or start /min wscript.exe %1 in your cmd file
    – uSlackr
    Jul 30, 2014 at 21:44
  • The cmd window appears because cmd.exe is launched when the .cmd or .bat file is started. start /min won't help.
    – mgjk
    Jul 31, 2014 at 12:14
1

Solution

Windows file association can be either system-wide or per-user, and the latter takes precedence. You can therefore override the default behavior for .txt files by creating a specific registry key:

  1. Open a command prompt.

  2. Type or paste the following command, and press Enter after replacing X:\Path\to\myscript.vbs with the actual path:

    reg add "HKCU\Software\Classes\txtfile\shell\open\command" /ve /t REG_SZ /d "\"%windir%\System32\wscript.exe\" \"X:\Path\to\myscript.vbs\" \"%1\"" /f
    

    Changes are applied immediately.

Further reading

0

create file 1.cmd at user temp directory).

1.cmd:

cmd

open file and do what you want)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .