4

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!

4

1 回答 1

7

这是一个非常好的问题!尝试这个:

@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""

调用批处理:

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

notepad++ 命令行开关

于 2013-09-20T15:44:33.933 回答