windows - How to associate a file extension to be opened with a batch script?

06
2014-04
  • Oriol

    I want to get rid of OSPPSVC (Office Software Protection Platform Service).

    Then, I have replaced the links to open Office programs with a link to launcher.bat:

    @echo off
    title Launching Office...
    Set target=%1
    
    sc config osppsvc start= demand
    net start osppsvc
    
    :check
    if exist "%target%.exe" goto launch
    Set/p target="Introduce the program to open:"
    goto check
    
    :launch
    start %target%.exe %2
    
    sleep 10
    
    sc config osppsvc start= disabled
    net stop osppsvc
    

    And I can open files if, in the console, I enter:

    "C:\Path\To\Office14\launcher.bat" winword "C:\Path\To\File\doc.docx"
    

    But, since I want explorer integration, I have tried associating .docx extension to be opened with

    "C:\Path\To\Office14\launcher.bat" winword "%1"
    

    But it doesn't work: the batch file runs but doesn't open Word (it asks "Introduce the program to open:").


    Edit: Fixed code:

    @echo off
    title Launching Office...
    Set name=%1
    
    sc config osppsvc start= demand
    net start osppsvc
    
    :check
    Set target="%~dp0%name%.exe"
    if exist %target% goto launch
    Set/p name="Introduce the program to open:"
    goto check
    
    :launch
    start "" %target% %2
    
    sleep 10
    
    sc config osppsvc start= disabled
    net stop osppsvc
    
  • Answers
  • Wally

    Actually it appears that you need to specify which folder %target% is in, unless somehow the Office path is in your %PATH%.


  • Related Question

    How to set the default program for opening files without an extension in Windows?
  • Piotr Dobrogost

    How to set the default program for opening files without an extension in Windows?


  • Related Answers
  • 8088

    From Windows IT pro:

    Normal files with an extension can have a program associated however this is not the case with files that don't have an extension. If you double click on one and select an application and check the box to always use that application it is ignored and every time you select a file you have to choose the application.

    You can force this using the registry:

    • Start the registry editor (regedit.exe)
    • Move to HKEY_CLASSES_ROOT
    • From the Edit menu select New - Key
    • Enter a name of '.' and press Enter (don't type the quotes)
    • Select the new '.' key
    • Double click the (Default) value
    • Change to the HKEY_CLASSES_ROOT used to open, e.g. NOTEPAD for the notepad.exe application NOTEPAD
    • Click OK

    alt text

    If you now double click on a file with no extension it will open with the application selected.

    To check what an existing application used look at its entry under HKEY_CLASSES_ROOT, e.g. HKEY_CLASSES_ROOT.doc uses Word.Document.8 so if you wanted this as your default editor you would change HKEY_CLASSES_ROOT.(Default) to Word.Document.8.

    Any method which avoids the need to edit the registry is as follows:

    • Open Explorer - View - Folder Options... - File Types - New Type:
    • Description of type: Text (any description, you can type in)
    • Associated extension: . (just a period)
    • Actions: - new...
    • Action: open
    • Application used to perform action: c:\winnt\notepad.exe
    • Click ok, then ok again
  • Kache

    With the command line:

    assoc .="No Extension"
    ftype "No Extension"="C:\path\to\my editor.exe" "%1"
    

    To give credit, I learned this from the vim wikia here and here

    Extra info:

    Instead of "C:\path\to\...", the following macros may be useful:

    • %SystemDrive% - drive windows is installed on, i.e. C:\
    • %ProgramFiles% - e.g. "C:\Program Files\"
    • %ProgramFiles(x86)% - e.g. "C:\Program Files (x86)\"

    You will need to properly escape them though:

    ftype "No Extension"=^"^%ProgramFiles(x86)^%\Notepad++\notepad++.exe^" "%1"
    

    To set the icon to be the same as .txt files (I didn't do this, since it automatically made the files' icons display as Notepad++ files):

    assoc "No Extension"\DefaultIcon=%SystemRoot%\System32\imageres.dll,-102
    

    To undo, you can read the assoc /? or ftype /? information, e.g.:

    ftype "No Extension"=
    assoc "No Extension"\DefaultIcon=
    assoc .=
    
  • Pauk

    This is an example of .reg file for getting Notepad to open unknown file extensions. Create a new text file with an extension of .reg, cut and paste this into it, then double click to run and it'll put it into the registry.

    Windows Registry Editor Version 5.00
    
    [HKEY_CLASSES_ROOT\Unknown\shell]
    @="notepad"
    
    [HKEY_CLASSES_ROOT\Unknown\shell\notepad]
    @="Open with Notepad"
    
    [HKEY_CLASSES_ROOT\Unknown\shell\notepad\command]
    @="C:\\WINDOWS\\NOTEPAD.EXE %1"
    
  • slhck
    • Start the registry editor (regedit.exe)
    • Move to HKEY_CLASSES_ROOT \ *\ . \
    • Then move to or create this path shell \open \command
    • double click Default when in the command directory and put in your path to your editor e.g.

      C:\PFE\pn.exe "%1"
      

    This enables the PFE editor to open files with no extension if you have PFE on your C drive.

  • slhck

    You can also use NirSoft FileTypesManager:

    FileTypesMan is an alternative to the 'File Types' tab in the 'Folder Options' of Windows.

    FileTypesMan also allows you to easily edit the properties and flags of each file type, as well as it allows you to add, edit, and remove actions in a file type.

  • 79E09796

    Pauk's answer didn't work for me, I had to use instead:

    Windows Registry Editor Version 5.00

    [HKEY_CLASSES_ROOT.] @="No Extension"

    [HKEY_CLASSES_ROOT\No Extension]

    [HKEY_CLASSES_ROOT\No Extension\Shell]

    [HKEY_CLASSES_ROOT\No Extension\Shell\Open]

    [HKEY_CLASSES_ROOT\No Extension\Shell\Open\Command] @="C:\\pathtoexe\\yourexe.exe %1"

    Note the double backslashes on the exe path.

  • slhck

    The other answers are a bit outdated. Here is a link to one that works for Windows 7:

    Shell Extensions for File Names with No File Extension

    Shell extensions for file names with no file extension can be registered under the following:

    [HKEY_CLASSES_ROOT\.]
               For example, to associate a program (for example, Notepad.exe) to open all files with no extension, use the following registry keys:
    
    [HKEY_CLASSES_ROOT\.]
    @=""
    
    [HKEY_CLASSES_ROOT\.\shell]
    
    [HKEY_CLASSES_ROOT\.\shell\open]
    
    [HKEY_CLASSES_ROOT\.\shell\open\command]
    @="<path to notepad.exe> %1"
               Here is an alternative method:
    
    [HKEY_CLASSES_ROOT\.]
    @="NoExtFile"
    
    [HKEY_CLASSES_ROOT\NoExtFile]
    
    [HKEY_CLASSES_ROOT\NoExtFile\shell]
    
    [HKEY_CLASSES_ROOT\NoExtFile\shell\open]
    
    [HKEY_CLASSES_ROOT\NoExtFile\shell\open\command]
    @="<path to notepad.exe> %1"