2

i m trying to compile batch file to search a string on a txt file and when it finds give the line number and place it to a variable so i can use it on a statement

example

name.txt

carolina
rita
sara
andre

in the example above i whant to find %username% = Andre and then return to a variable the number 4 if the %username% is not on the list i whant it to atribut the name "Hello" because the "andre" is in line 4

i found a code but icant adatp it to function

    @echo off &setlocal
set "search=%username%"
set "replace=kordo anstataui"
set "textfile=name.txt"
set "newfile=new.txt"

(for /f "delims=" %%i in ('findstr /n "^" "%textfile%"') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:%search%=%replace%!"
    echo(!line!
    endlocal
))>"%newfile%"

type "%newfile%"

i hope it help the code helps

Ps:sorry for my bad writing

user3121120
  • 21
  • 1
  • 2

3 Answers3

4

You found the FINDSTR command. Hmmm, I wonder what it might do...

for /f "delims=:" %%N in ('findstr /i /x /n andre "%textfile%"') do set line=%%N

Use help findstr or findstr /? from a command prompt to get help. Refer to What are the undocumented features and limitations of the Windows FINDSTR command? for additional info not available in the standard help.

Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • hello after a lot o seaching i found what i whant but i cant put it to work i found another code for my problem and it works i tried to change the "Installer ID:" for a variable but it wont work i need to search %username% instead of "installer id:"if i can do that my problem is solved @echo off set "file=build_info.txt" set "installer_id=" for /f "tokens=2* delims=: " %%A in ( 'findstr /bc:"Installer ID:" "%file%"' ) do set "installer_id=%%B" echo %installer_id% pause>null – user3121120 Dec 21 '13 at 23:50
  • @user3121120 then replace "Installer ID:" with "%username%". This will now search for the username and store that in the INstaller ID variable – Luigi Aug 04 '15 at 17:45
  • @dbenham: When I `echo !line!` from `for /f "delims=:" %N in ('findstr /i /x /n andre "%textfile%"') do set line=%N` I get a response `!line!` instead of the number 4. Do you know why? – Zimba Apr 16 '20 at 17:43
  • @Zimba - Probably because delayed expansion has not enabled. - `setlocal enableDelayedExpansion` – dbenham Apr 16 '20 at 18:07
  • @dbenham: it is enabled. Here's what I had: `for /f "delims=:" %N in ('findstr /i /x /n andre "name.txt"') do set line=%N echo %line% %line% echo !line! !line!` It's working now, after I pasted the code from my comment. Didn't work at first when I edited it in Notepad to remove the extra %, and still doesn't. – Zimba Apr 16 '20 at 18:28
0

This code gives all lines containing andre and stores the last line containing the match in a variable:

for /f "tokens=1* delims=][" %i in ('find /n "andre" ^< name.txt') do echo Hello %j, you are in line %i & set line=%i
Zimba
  • 2,854
  • 18
  • 26
-1

I use this one as is very simple.

FOR /F "delims=: tokens=1*" %i in ('findstr /N /I "String2Find" "file2read.log"') do ( set VARIABLE=%i )
echo %VARIABLE%
Chop Labalagun
  • 592
  • 1
  • 6
  • 19