Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding text with a batch file using Notepad++

I need to find text in a file when I open it with Notepad++.
For instance I need to find the text "ABC". However the true text I need are 2 numbers that follow "ABC". So I have a batch file that uses Notepad++ to open a particular file when I double click it. It needs to find ABC and then look at the 2 digits directly following it and populate a variable with them.

Some files might contain ABC12 while others might contain ABC21, etc.

All I have so far is the following:

%echo off
"C:\Program Files (x86)\Notepad++\notepad++.exe" %1 
FIND [/I] "ABC"

It opens the file fine, but does not search. Please help!
Thanks!

like image 923
jmitch977 Avatar asked Mar 22 '23 08:03

jmitch977


1 Answers

this is a really nice question! Try this:

@ECHO OFF &SETLOCAL
set "LineNr="
for /f "tokens=1*delims=[]" %%a in ('^<"%~1" find /i /n "%~2"') do if not defined LineNr (
    set "LineNr=%%a"
    SET "Line=%%b"
)
if not defined LineNr (
    set "LineNr=1"
    SET "Row=1"
    GOTO :launch
)
CALL SET "Right=%~2%%Line:*%~2=%%"
CALL SET "Line=%%Line:%Right%=%%"
FOR /f "delims=:" %%a in ('"(echo("%Line%"&@echo()|findstr /o $"') do SET /a Row=%%a-4
:launch
START /b CMD /c ""%ProgramFiles(x86)%\Notepad++\notepad++.exe" -n%LineNr% -c%Row% "%~1""

calling the batch:

script.bat "D:\PATH\FILE.TXT" "STRING TO SEARCH"

notepad++ command line switches

like image 127
Endoro Avatar answered Mar 31 '23 16:03

Endoro