How to associate file extensions with application on Windows by VBScript

Introduction

This sample provides a script for IT pro or windows customers to quickly batch the associated file extensions with application on Windows by VBScript.

For PowerShell version, please read this post: How to associate file extensions with application on Windows by PowerShell

 

Note

If users manually link an app with a specified program by Windows Explorer UI, for example, he might use this app for all .txt files in the right-click pop-up menu, then he cannot run the script to change the linkage. Unfortunately, we haven't found any cause for now.

Scenarios

The most efficient way to change file associations is by selecting a default program. This tells Windows that you want a certain program to be associated with all the file types it can handle. IT pro or windows customers want to have some kinds of batch method to automatically associate a selection of file extensions. This sample script will help to quickly batch the associated file extensions with application on Windows.

Script

VB Script
Edit|Remove
call AssociateFileExtensions(Array(".txt",".sql"), "C:\Windows\System32\notepad.exe")


Here are some code snippets for your reference.

VB Script
Edit|Remove
Set fso = CreateObject("Scripting.FileSystemObject") 
        If (fso.FileExists(openAppPath)) Then  
            Set WshShell = CreateObject("WScript.Shell") 
            echoMsg = "" 
            For i=0 To ubound(arrFileExtensions)  
                  Set oExec = WshShell.exec("cmd.exe /c assoc " &  arrFileExtensions(i)) 
                 fileType = oExec.Stdout.ReadAll 
                 fileType = Split(fileType, "=")(1)  
                 Set oExec = WshShell.exec("cmd.exe /c ftype " &  fileType & "=""" & openAppPath & """ ""%1""") 
                 echoMsg = echoMsg & oExec.Stdout.ReadAll & vbCr 
                 Set oExec = Nothing 
             Next  
            Set WshShell = Nothing 
            WScript.Echo echoMsg 
        Else  
            WScript.Echo openAppPath & " does not exist." 
        End If         
    Set fso = Nothing
 

 

 

 

Microsoft All-In-One Script Framework is an automation script sample library for IT Professionals. The key value that All-In-One Script Framework is trying to deliver is Scenario-Focused Script Samples driven by IT Pros' real-world pains and needs. The team is monitoring all TechNet forums, IT Pros' support calls to Microsoft, and script requests submitted to TechNet Script Repository. We collect frequently asked IT scenarios, and create script samples to automate the tasks and save some time for IT Pros. The team of All-In-One Script Framework sincerely hope that these customer-driven automation script samples can help our IT community in this script-centric move.