4.6.16. Open a file basing on the text selected in the editor

I was wondering whether NppExec allows to open a file, say, "C:\Program Files (x86)\Notepad++\change.log" when a text "Notepad++\change.log" is selected in the editor.

NppExec does allow this!

Here is NppExec's script required for this (please see the embedded comments for clarifications):

npp_console ?

// process the selected text...
sci_sendmsg SCI_GETSELTEXT 0 @""
set local F = $(MSG_LPARAM) // the selected text
if "$(F)" != "" then // something selected
  // check current file name...
  set local n ~ strfind "$(FILE_NAME)" "$(F)"
  if $(n) == -1 then // current file name does not contain the selected text
    // check already opened files...
    set local prev_fpath = $(FULL_CURRENT_PATH)
    npp_console local -
    npp_switch $(F) // try to switch to another file
    npp_console local +
    if "$(FULL_CURRENT_PATH)" == "$(prev_fpath)" then // did not switch to another file
      // check predefined directories...
      npe_console local v+ --
      C:\tools\findfile.bat "$(F)"
      if "$(OUTPUT1)" != "" then // found in a predefined dir
        set local F = $(OUTPUT1)
      endif
      npp_open $(F)
    endif
  endif
endif

This NppExec's script uses external file "C:\tools\findfile.bat" which describes the set of predefined directories to be checked.

Here is a possible content of this "findfile.bat":

@echo off

for /D %%d in (
               "C:",
               "C:\Program Files",
               "C:\Program Files (x86)"
              ) do (
    if exist "%%~d\%~1" (
        call :EchoUnquotedString "%%~d\%~1"
    )
)
goto End

:EchoUnquotedString
    echo %~1
goto End

:End

The predefined directories in this example are: "C:", C:\Program Files" and "C:\Program Files (x86)". You can define your own set of directories here.

Here is how it works:

Basing on your needs, the batch file "findfile.bat" can be modified. For example, it can use the output of git ls-files :

@echo off

set WorkDir=C:\Projects\notepad-plus-plus
cd /D "%WorkDir%"

for /F %%f in ('git ls-files -- *%1*') do (
  call :EchoUnquotedString "%WorkDir%\%%~f"
)
goto End

:EchoUnquotedString
    echo %~1
goto End

:End