Custom Macro which searches and opens a file from project or given directories

Problem statement:

If we want to open a file included in a visual studio project we need  to open the “Solution explorer” and navigate to that particular file. If the project has many files included in it and the files are not organized with proper filters/groups then finding the file may take considerable time.This tip consists of a visual studio macro which by taking the file name as input opens any file for you.The macro also searches for given file inside predefined directories.
Before going into details of the script here is some basic intoduction to visual studio macros.

Visual studio macros are Visual Basic programmes intended to automate repetetive/common tasks inside  visual studio.Visual studio comes with number of built in macros which can be found in “Macro Explorer” (Tools->Macros->Macro Explorer).You can run any  macro in the following ways.

By double clicking on the macro in Macro explorer. By a keyboard short cut

In the Macros IDE.

Visual studio also provides support for creating our own macros. The Macros IDE is the tool used to accomplish this task.For opening Macro IDE press Alt+ F11.

And here is the VB script for opening a file.

Sub openFile()

Dim foldersToSearchForGivenFile AsString() = {"D:\iDevelop\SDKs\Any SDK\source", "D:\TEST_DIRECTORY"}  ' Enter here the directories to search for given file  . you can enter any number of directories

Dim fileToOpen AsString = InputBox("Enter the name of the file(with extension) to open", "Open File ", ".cpp")

IfString.IsNullOrEmpty(fileToOpen) Then

Return

EndIf

 

' Searches given file in current project/solution

Dim projItem As EnvDTE.ProjectItem = DTE.Solution.FindProjectItem(fileToOpen)

If not projItem IsNothingThen

          projItem.Open()

      Return

EndIf

 

'If the given file is not found in current project/solution it is searched inside given directories

Dim fileExt AsString = fileToOpen.Substring(fileToOpen.LastIndexOf(".") + 1)

Dim directory AsString

For Each directory In foldersToSearchForGivenFile

   Dim listofFiles AsString() = System.IO.Directory.GetFiles(directory, "*." + fileExt, IO.SearchOption.AllDirectories)

  Dim filePath AsString

  Dim fileName AsString

  For Each filePath In listofFiles

      fileName = filePath.Substring(filePath.LastIndexOf("\") + 1)

    If (fileName.Equals(fileToOpen)) Then

              ExecuteCommand("open", """" + filePath + """")

           Return

    EndIf
  Next
Next

MsgBox("The specified file - " + fileToOpen + " - could not be found")

EndSub

To run this script : –

Create a new Module/select an existing module in Macro Explorer. Add a new macro “openFile” inside the selected module. Right click on the new macro and click on “Edit”.Macro IDE will be opened Copy paste the above script inside the macro.

Save it.

To assign a Key board Short cut: –

Go to Tools->Options->Environment->Keyboard Find your macro.(You can use the field “Show commands containing”)

Set up a shortcut key (such as Ctrl + f, Ctrl + o) and press assign.

When you run this script it opens an input dialog where you can enter the filename and the macro searches the file and opens it in IDE

150 150 Burnignorance | Where Minds Meet And Sparks Fly!