Automatically open file with - executable with same name

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    Automatically open file with - executable with same name

    I am attempting to automatically open .xml files with a .bat script when I open them.

    Microsoft XP 2002 SP 2

    Ultimate goals:
    1. Figure out how Open With list is stored.
    2. Edit Open With list to clear old entries, and add new entries.

    Current:
    When I double click on xml files, they are automatically run with
    C:\project1\Pre process.bat

    Wanted:
    When I double click on xml files, they are automatically run with
    C:\project2\Pre process.bat

    Attempts to change:
    1. a. Right clicked on xml file and selected Open With... Choose Program
    b. Browsed to C:\project2\Pre process.bat
    c. selected Preprocess.bat
    d. selected "always open with this file."

    Result: when the file executes, or when I later double click on the file, it runs with C:\project1\Pre process.bat. The file listing in the screen of programs to select from has not updated.


    2. a. Renamed C:\project1\Pre process.bat to C:\project1\Pre process2.bat
    b. Right clicked on xml file and selected Open With... Choose Program
    c. Preprocess.bat can not be found.
    d. Browsed to C:\project2\Pre process.bat
    e. Preprocess.bat still can not be found.

    The only thing that works is renaming my current C:\project2\Pre process.bat.
    However, I don't want to have to version up my .bat files for every version of every project. Why does windows not update these entries??
  • Nilpo
    New Member
    • Mar 2009
    • 2

    #2
    As you probably well imagined, these settings are held in the registry. Specifically, file type handlers are housed in the HKEY_CLASSES_RO OT hive. If you navigate this hive in the Registry Editor, you'll notice a key dedicated to each registered file type on your system. So your .xml settings will be housed here:

    HKEY_CLASSES_RO OT\.xml

    This key is used to register the file type and its handler along with any other pertinent information about that file type. In this case, XML files are already recognized by Windows so this key should already be populated for you. It's default string value assigns its handler (xmlfile) and its Content Type string value assigns the default content type (text/xml). We're only concerned with the first at this point.

    As you continue navigating down the HKCR hive, you will find the handler entries. This is where you'll make your changes. The xmlfile handler is located at the following key:

    HKEY_CLASSES_RO OT\xmlfile

    This is the key that performs the magic. It houses all of the default actions for this file type. We're working with its default Open command so we can drill down a bit further into this key and edit its shell actions.

    HKEY_CLASSES_RO OT\xmlfile\shel l\open

    This key defines the shell open settings. Its default string value can be set to change the open text on the context menu in Windows Explorer. Leaving this value unset will display the default "Open" text when you right-click an XML file. Beneath the open key there is a command key that we need to edit.

    HKEY_CLASSES_RO OT\xmlfile\shel l\open\command

    This key's default string value assigns the command line that should be executed whenever this file type is opened through the Windows Shell (i.e. by being double-clicked). Your command line should look like this:

    C:\project2\Pre process.bat "%1"

    The %1 reference is used to pass the path and file name of the file being opened as an argument to the batch file. I've surrounded this in quotes in case the XML file name or path contains any spaces. Setting this value will make the changes you wish to make.

    Here's what the same thing looks like as a .reg file.
    Code:
    Windows Registry Editor Version 5.00
    
    [HKEY_CLASSES_ROOT\xmlfile\shell\open]
    
    [HKEY_CLASSES_ROOT\xmlfile\shell\open\command]
    @="C:\\project2\\Preprocess.bat \"%1\""
    You can also work with File Types through the Window GUI. In either My Computer or Windows Explorer, choose Folder Options... from the Tools menu to open the Folder Options dialog. You may add or edit registered file types on the File Types tab.

    Nilpo

    <links removed>
    Last edited by Nepomuk; Apr 19 '09, 09:56 AM. Reason: Linking to pages which compete with bytes is against our posting guidelines.

    Comment

    Working...