Replace text in a file - batch script
Moderator: DosItHelp
Replace text in a file - batch script
Hi
Im new to windows batch scripting so need help urgently
I have a "fileA.txt" with the following contents
---------------------
line1
line2
:: Block A Start
set variable1="C:\ABCD\1234"
set variable2="C:\XYZ\6789"
set variable3="C:\FFF\5555"
SET DB_SERVER=10.111.34.443
SET DB_USERID=db_user
SET DB_PASSWORD=Password
:: Block A End
line10
line11
----------------------
Now, what I want is:
A script that I can run from command prompt:
for ex: replace.bat <param1> <param2> <paramN> .....
Where:
- param1 is a keyword to use within the file to find from which keyword
onwards in the file you have to make change (in this ex: param1 would
be ":: Block A Start")
- param2 is at what keyword within the file, replace should not proceed.
i.e. param1 and 2 are defining the boundary within a file to replace text.
in this ex: param2 would be ":: Block A End"
- param 3 is "Keyword to replace". In this example, let say I want to replace
keyword "DB_USERID=sa" to "DB_USERID=valid_db_user"
then param 3 would be "DB_USERID=sa"
- param4 is the value with which you'll replace keyword 3 with.
in this example, param4 would be: "DB_USERID=valid_db_user"
and that's it.
I want the file to be editted in place (i.e. instead of creating a new temporary file and then moving it back to original file name as: "fileA.txt"), this replace.bat script should work on fileA.txt directly and update it.
Any help is greatly appreciated.
Please consider the following situations:
- What if param3 keyword occur more than once in the file. I think our
boundary param1,2 parameters will take care of that part as we are
updating within that block.
- What if I dont use the boundary block parameters (param1,2) and I want
to replace param3 with param4 at param3's Nth occurrence.
i.e. if param3 exists in the file at 5 different locations, then I want to
replace only the first one, or Nth one or the last one.
Thank you.
Im new to windows batch scripting so need help urgently
I have a "fileA.txt" with the following contents
---------------------
line1
line2
:: Block A Start
set variable1="C:\ABCD\1234"
set variable2="C:\XYZ\6789"
set variable3="C:\FFF\5555"
SET DB_SERVER=10.111.34.443
SET DB_USERID=db_user
SET DB_PASSWORD=Password
:: Block A End
line10
line11
----------------------
Now, what I want is:
A script that I can run from command prompt:
for ex: replace.bat <param1> <param2> <paramN> .....
Where:
- param1 is a keyword to use within the file to find from which keyword
onwards in the file you have to make change (in this ex: param1 would
be ":: Block A Start")
- param2 is at what keyword within the file, replace should not proceed.
i.e. param1 and 2 are defining the boundary within a file to replace text.
in this ex: param2 would be ":: Block A End"
- param 3 is "Keyword to replace". In this example, let say I want to replace
keyword "DB_USERID=sa" to "DB_USERID=valid_db_user"
then param 3 would be "DB_USERID=sa"
- param4 is the value with which you'll replace keyword 3 with.
in this example, param4 would be: "DB_USERID=valid_db_user"
and that's it.
I want the file to be editted in place (i.e. instead of creating a new temporary file and then moving it back to original file name as: "fileA.txt"), this replace.bat script should work on fileA.txt directly and update it.
Any help is greatly appreciated.
Please consider the following situations:
- What if param3 keyword occur more than once in the file. I think our
boundary param1,2 parameters will take care of that part as we are
updating within that block.
- What if I dont use the boundary block parameters (param1,2) and I want
to replace param3 with param4 at param3's Nth occurrence.
i.e. if param3 exists in the file at 5 different locations, then I want to
replace only the first one, or Nth one or the last one.
Thank you.
Re: Replace text in a file - batch script
That is going to be a bit hard, but for a start,
wouldn't be easier if you define the start line and end line instead of keywords that define the start and end of the part you are going to do replace in?
wouldn't be easier if you define the start line and end line instead of keywords that define the start and end of the part you are going to do replace in?
Re: Replace text in a file - batch script
Thanks for replying abc.
Well, specifying line# for start n end lines is not a good idea as the file changes frequently, so you can tell whether "Block A start" is now at line# 3 or at 58 as developers can add lines at the top / middle of the file.
I can make this ? more easy though.
So, instead of us ... searching for keyword and replacing them within that, block Start and end, what if I want to do sometihng like this.
========================
I have a "fileA.txt" with the following contents
---------------------
line1
line2
:: Block A Start
set variable1="C:\ABCD\1234"
set variable2="C:\XYZ\6789"
set variable3="C:\FFF\5555"
SET DB_SERVER=10.111.34.443
SET DB_USERID=db_user
SET DB_PASSWORD=Password
:: Block A End
line10
line11
----------------------
now, I want to replace the whole BLOCK.. from a different fileB.txt
which has the following lines:
I have a "fileB.txt" with the following contents
---------------------
lineA
lineB
:: Block A Start
set variable1="C:\ABCD\1234"
set variable2="C:\XYZ\6789"
set variable3="C:\FFF\5555"
SET DB_SERVER=10.111.34.443
SET DB_USERID=correct_db_user
SET DB_PASSWORD=Correct_password
:: Block A End
lineY
lineZ
----------------------
Now, the only thing we need to do is to replace all the lines starting from keyword (param1) to param2... (inclusive the keyword lines)..... and replace it with the same block from fileB.txt.
Is that possible easily?
therefore, the final file fileA.txt after replacement of the block will look like:
I have a "fileB.txt" with the following contents
---------------------
line1
line2
:: Block A Start
set variable1="C:\ABCD\1234"
set variable2="C:\XYZ\6789"
set variable3="C:\FFF\5555"
SET DB_SERVER=10.111.34.443
SET DB_USERID=correct_db_user
SET DB_PASSWORD=Correct_password
:: Block A Endline10
line11
----------------------
Well, specifying line# for start n end lines is not a good idea as the file changes frequently, so you can tell whether "Block A start" is now at line# 3 or at 58 as developers can add lines at the top / middle of the file.
I can make this ? more easy though.
So, instead of us ... searching for keyword and replacing them within that, block Start and end, what if I want to do sometihng like this.
========================
I have a "fileA.txt" with the following contents
---------------------
line1
line2
:: Block A Start
set variable1="C:\ABCD\1234"
set variable2="C:\XYZ\6789"
set variable3="C:\FFF\5555"
SET DB_SERVER=10.111.34.443
SET DB_USERID=db_user
SET DB_PASSWORD=Password
:: Block A End
line10
line11
----------------------
now, I want to replace the whole BLOCK.. from a different fileB.txt
which has the following lines:
I have a "fileB.txt" with the following contents
---------------------
lineA
lineB
:: Block A Start
set variable1="C:\ABCD\1234"
set variable2="C:\XYZ\6789"
set variable3="C:\FFF\5555"
SET DB_SERVER=10.111.34.443
SET DB_USERID=correct_db_user
SET DB_PASSWORD=Correct_password
:: Block A End
lineY
lineZ
----------------------
Now, the only thing we need to do is to replace all the lines starting from keyword (param1) to param2... (inclusive the keyword lines)..... and replace it with the same block from fileB.txt.
Is that possible easily?
therefore, the final file fileA.txt after replacement of the block will look like:
I have a "fileB.txt" with the following contents
---------------------
line1
line2
:: Block A Start
set variable1="C:\ABCD\1234"
set variable2="C:\XYZ\6789"
set variable3="C:\FFF\5555"
SET DB_SERVER=10.111.34.443
SET DB_USERID=correct_db_user
SET DB_PASSWORD=Correct_password
:: Block A Endline10
line11
----------------------
Re: Replace text in a file - batch script
well that will be easier, wait for the batch 

Re: Replace text in a file - batch script
Quick question, the word or words that will be the start and end keywords for the block, will be the same in both files, file 1 and file2?
Re: Replace text in a file - batch script
Yes. That's correct.
Can you consider the fact that the keyword can contain ", ' , or any characters in it?
Can you consider the fact that the keyword can contain ", ' , or any characters in it?
Re: Replace text in a file - batch script
I finished the code, test it for now till i try to do the modifications
EDITED
Fixed the Error and it now can handle line numbers from 1 digit till 5 digits "ex: 1 to 99999"
Edit 1/10/2012
changed the function at the end of the batch now it can handle from 1 digit to .......
to use it in cmd
if start and end keyword 1 is the same as keywords 2, just input them once and it will be considered as they are the same
Edit: 2/10/2012
This batch will replace/remove a word from a file in a particular range of lines or in all file,
it will replace all words that match in the range the user set
used like this
the "start_from_words" & "stop_till_words" are optional but put empty "" in there place instead.
EDITED
Fixed the Error and it now can handle line numbers from 1 digit till 5 digits "ex: 1 to 99999"
Edit 1/10/2012
changed the function at the end of the batch now it can handle from 1 digit to .......
Code: Select all
@echo off & cls
:: Credit goes to aGerman for the Extract Functions
:: user should provide:
:: > the source that need modification,
:: > the second file that has the modification
:: > the keyword for the start line for file 1
:: > the keyword for the end line for file 1
:: > the keyword for the start line for file 2
:: > the keyword for the end line for file 2
:: if start and end keywords or one of them is missing, the start and end keywords
:: for file 1 is used instead.
:: Set variable here
set "file1=%~1"
set "file2=%~2"
set "word1=%~3"
set "word2=%~4"
set "word3=%~5"
set "word4=%~6"
:: set word 3 and 4 to word 1 and 2 if the user didn't input them.
if not exist "%word3%" set "word3=%word1%"
if not exist "%word4%" set "word4=%word2%"
setlocal enabledelayedexpansion
:: get start & end kewword's line numbers from file (1)
Call :line_number "%word1%" "%file1%" "Sline"
Call :line_number "%word2%" "%file1%" "Eline"
:: get start & end kewword's line numbers from file (2)
Call :line_number "%word3%" "%file2%" "Sline_2"
Call :line_number "%word4%" "%file2%" "Eline_2"
:: take lines from file (1) till the start keyword's line number (line that hold keyword not included)
:: and to make it included, replace LSS with LEQ Then add these lines to new file.
set l_num1=0
For /F "tokens=* delims=" %%A in ('Type "%file1%"') Do (
set /a l_num1 +=1
if !l_num1! LSS !Sline! Echo %%A>>"temp.txt"
)
:: IMPORTANT NOTE: >>> IF you Replaced LSS to LEQ in Previous Step, Replace the GEQ to GTR and LEQ to LSS in This Step.
:: take lines from file (2) starting from the start keyword's line number (line that hold keyword is included)
:: and ending with the end keyword's line number (also end keyword line included) and add all that to new file.
set l_num2=0
For /F "tokens=* delims=" %%A in ('Type "%file2%"') Do (
set /a l_num2 +=1
if !l_num2! GEQ !Sline_2! (
if !l_num2! LEQ !Eline_2! (
Echo %%A>>"temp.txt" ) )
)
:: IMPORTANT NOTE: >>> IF you Replaced GEQ to GTR and LEQ to LSS in Previous Step, Replace GTR to GEQ.
:: take lines from file (1) starting from the end keyword's line number (line that hold keyword not included)
:: and to make it included, replace GTR with GEQ Then add these lines to new file.
set l_num3=0
For /F "tokens=* delims=" %%A in ('Type "%file1%"') Do (
set /a l_num3 +=1
if !l_num3! GTR !Eline! Echo %%A>>"temp.txt"
)
:: backin-up file 1 and file 2, then rename the file temp to the file1 original name
Ren "%file1%" "%file1%.bak"
::Ren "%file2%" "%file2%.bak"
Ren "temp.txt" "%file1%"
pause
exit /b
:: Function to get line numbers
:line_number
:: word is first parameter and will hold the word to search for
:: file is the file where to search in
:: var is the variable name that will hold the line number
set "word=%~1"
set "file=%~2"
set "var=%~3"
for /f "skip=2 tokens=1 delims=[]" %%A in ('find /N "%word%" "%file%"') Do set "%var%=%%A"
to use it in cmd
Code: Select all
C:\batch_file.bat "file1.txt" "file2.txt" "start_keyword1" "end_keyword1" "start_keyword2" "end_keyword2"
if start and end keyword 1 is the same as keywords 2, just input them once and it will be considered as they are the same
Edit: 2/10/2012
This batch will replace/remove a word from a file in a particular range of lines or in all file,
Code: Select all
@Echo Off & Cls & Color 0E
:: user should provide:
:: > source file.extension
:: > word to replace
:: > word to replace with
:: > start Keyword (start from) [optional]
:: > end keyword (end at) [optional]
:: <<< Notes >>>
:: > (source), (word to replace) & (word to replace with) MUST be set.
:: > (Start & End Keywords) are OPTIONAL, but if you don't want to set them, must put
:: {empty double quotes} in there position, you can set one of them and place "" for the other one.
:: > you can remove a word by setting the (word to replace with) to < "" > [empty double quotes].
:: > word and word to replace can't contain <double qoute " >
:: set variables
set "source=%~1"
set "Rword=%~2"
set "RWword=%~3"
set "Sword=%~4"
set "Eword=%~5"
Setlocal EnableDelayedExpansion
:: Get start and end line numbers
::Check Incase both was set to {empty double quotes}
IF "%Sword%" == "" (
IF "%Eword%" == "" (
set "Sline=1"
for /f "skip=2 tokens=1 delims=[]" %%A in ('find /N /V "" "1.txt"') Do set "Eline=%%A"
)
)
::Check Incase one of them or both was set to {empty double quotes}
IF "%Sword%" == "" ( set "Sline=1"
) Else ( Call :line_number "%Sword%" "%source%" "Sline" )
IF "%Eword%" == "" ( for /f "skip=2 tokens=1 delims=[]" %%A in ('find /N /V "" "1.txt"') Do set "Eline=%%A"
) Else ( Call :line_number "%Eword%" "%source%" "Eline" )
:: Get total line number of the file
for /f "skip=2 tokens=1 delims=[]" %%A in ('find /N /V "" "%source%"') Do set "Lline=%%A"
:: Take all lines including <Sline> & copy to temp file.
Call :ExtractOnly "%source%" "1" "%Sline%" "temp.txt"
:: Take line by line from the blcok and replace all occurance of
:: the <Rword> with <RWword>
set /a NSline = Sline + 1
Call :Extract2 "%source%" "%NSline%" "%NEline%" "temp.txt"
:: Take all lines including <Eline> line that has the <Eword>
:: and till the end and copy it to the temp file.
Call :ExtractOnly "%source%" "%Eline%" "%Lline%" "temp.txt"
:: now we rename the original <source> file and add .bak extention and
:: rename the <temp.tmp> file to the <source> file name.
Ren "%source%" "%source%.bak" >nul
Ren "temp.tmp" "%source%" >nul
:: End
pause
Exit /B
:: Function to get line numbers
:line_number
:: Call :line_number <word> <file> <variable_name>
set "word=%~1"
set "file=%~2"
set "var=%~3"
for /f "skip=2 tokens=1 delims=[]" %%A in ('find /N "%word%" "%file%"') Do set "%var%=%%A"
goto :eof
:ExtractOnly
:: >>> Credits Goes to: aGerman @ Dostips.com <<<
:: Call :Extract "<source_file>" "<start_line>" "<end_line>" "<out_file>"
set "file=%~1"
set "from=%~2
set "till=%~3
set "out=%~4"
set /a skip=from-1
Setlocal EnableDelayedExpansion
<"!file!" (
for /l %%i in (1 1 %skip%) do set /p "="
for /l %%i in (%from% 1 %till%) do (
set "ln="
set /p "ln="
echo(!ln!
)
)>>"%out%"
endlocal
goto :eof
:Extract2
:: Call :Extract "<source_file>" "<start_line>" "<end_line>" "<out_file>"
:: for more info about this function: http://www.dostips.com/forum/viewtopic.php?f=3&t=3829
set "file=%~1"
set "from=%~2"
set "till=%~3"
set "out=%~4"
set /a skip=from-1
set /a till = till - from
set jump = %till%
Setlocal EnableDelayedExpansion
<"%file%" (
for /l %%a in (1 1 %skip%) do set /p "="
for /f "tokens=1,2 delims=:" %%A in ('findstr /n /r /v /c:"[^ ]" "%file%"') Do (
for /l %%i in (1 %jump% %till%) Do (
set "ln="
set /p "ln="
If "%%A:!ln!" EQU "%%A:%%B" ( echo(!ln!
) else ( echo(!ln:%Rword%=%RWword%! )
)
)
)>>"%out%"
endlocal
goto :eof
it will replace all words that match in the range the user set
used like this
batch.bat "file.txt" "word_to_replace" "word_to_replace_with" "start_from_words" "stop_till_words"
the "start_from_words" & "stop_till_words" are optional but put empty "" in there place instead.
Last edited by abc0502 on 02 Oct 2012 04:21, edited 7 times in total.
Re: Replace text in a file - batch script
This is what I ran after saving the code to "replace.bat" file.
F:\TEST_JIM_AKS>replace.bat fileA.txt fileB.txt ":: Block A Start" ":: Block A End"
=========================
FileA.txt was:
---------------------
line1
line2
:: Block A Start
set variable1="C:\ABCD\1234"
set variable2="C:\XYZ\6789"
set variable3="C:\FFF\5555"
SET DB_SERVER=10.111.34.443
SET DB_USERID=db_user
SET DB_PASSWORD=Password
:: Block A End
line10
line11
---------------------
FileB.txt was:
---------------------
lineAAAA
lineBBBB
:: Block A Start
set variable1="C:\ABCD\1234"
set variable2="C:\XYZ\6789"
set variable3="C:\FFF\5555"
SET DB_SERVER=10.111.34.443
SET DB_USERID=actual_db_user
SET DB_PASSWORD=actual_db_Password
:: Block A End
lineYYYY
lineZZZZ
---------------------
After running the bat file, fileA.txt is looking like this now:
---------------------
line1
line2
:: Block A Start
:: Block A End
line10
line11
set variable1="C:\ABCD\1234"
set variable2="C:\XYZ\6789"
set variable3="C:\FFF\5555"
SET DB_SERVER=10.111.34.443
SET DB_USERID=actual_db_user
SET DB_PASSWORD=actual_db_Password
line10
line11
---------------------
Now, you here are my findings:
1. First, we should not touch/update fileB.txt (from where we are reading).
But I found that after running the .bat file, fileB.txt was gone and now
there's fileB.txt.bak. It should let the fileB.txt as fileB.txt as it's.
2. Secondly, the expected output should be (after getting the whole block from fileB.txt to fileA.txt and leaving line1,2,10 and 11 as it's:
fileA.txt
---------------------
line1
line2
:: Block A Start
set variable1="C:\ABCD\1234"
set variable2="C:\XYZ\6789"
set variable3="C:\FFF\5555"
SET DB_SERVER=10.111.34.443
SET DB_USERID=correct_db_user
SET DB_PASSWORD=Correct_password
:: Block A End
line10
line11
---------------------
Thanks for helping me out, can you correct the issue above.
F:\TEST_JIM_AKS>replace.bat fileA.txt fileB.txt ":: Block A Start" ":: Block A End"
=========================
FileA.txt was:
---------------------
line1
line2
:: Block A Start
set variable1="C:\ABCD\1234"
set variable2="C:\XYZ\6789"
set variable3="C:\FFF\5555"
SET DB_SERVER=10.111.34.443
SET DB_USERID=db_user
SET DB_PASSWORD=Password
:: Block A End
line10
line11
---------------------
FileB.txt was:
---------------------
lineAAAA
lineBBBB
:: Block A Start
set variable1="C:\ABCD\1234"
set variable2="C:\XYZ\6789"
set variable3="C:\FFF\5555"
SET DB_SERVER=10.111.34.443
SET DB_USERID=actual_db_user
SET DB_PASSWORD=actual_db_Password
:: Block A End
lineYYYY
lineZZZZ
---------------------
After running the bat file, fileA.txt is looking like this now:
---------------------
line1
line2
:: Block A Start
:: Block A End
line10
line11
set variable1="C:\ABCD\1234"
set variable2="C:\XYZ\6789"
set variable3="C:\FFF\5555"
SET DB_SERVER=10.111.34.443
SET DB_USERID=actual_db_user
SET DB_PASSWORD=actual_db_Password
line10
line11
---------------------
Now, you here are my findings:
1. First, we should not touch/update fileB.txt (from where we are reading).
But I found that after running the .bat file, fileB.txt was gone and now
there's fileB.txt.bak. It should let the fileB.txt as fileB.txt as it's.
2. Secondly, the expected output should be (after getting the whole block from fileB.txt to fileA.txt and leaving line1,2,10 and 11 as it's:
fileA.txt
---------------------
line1
line2
:: Block A Start
set variable1="C:\ABCD\1234"
set variable2="C:\XYZ\6789"
set variable3="C:\FFF\5555"
SET DB_SERVER=10.111.34.443
SET DB_USERID=correct_db_user
SET DB_PASSWORD=Correct_password
:: Block A End
line10
line11
---------------------
Thanks for helping me out, can you correct the issue above.
Re: Replace text in a file - batch script
hi, the code is fine , the problem is in the function, when extracting the line number, i will fix that.
and about the renaming, i changed the batch up.
and about the renaming, i changed the batch up.
Re: Replace text in a file - batch script
OK this solution is what I did:
1. Install Cygwin on your machine. To get Linux/Unix command
funcationality from Windows command prompt.
i.e. if you do "ls -ltra", it'll list all the directories/files (including hidden).
2. Create fileA.txt, fileB.txt as mentioned in my post above.
2. Create a file "replace.bat" and put the following lines:
4. Run the following command from Windows command prompt:
replace.bat ":: Block A Start" ":: Block A End" fileA.txt fileB.txt
Where, param1: Start Keyword,
param2: End Keyword,
param3: The file in which you want to change from param4 block file
data.
param4: from where you pick the Start/End block data and put it
inside param3 file.
PS:
- Param order changed per my initial post / request.
- This is not a good solution but works and gives more power as you get
Linux power in Windows.
easy, peasy !!!!
1. Install Cygwin on your machine. To get Linux/Unix command
funcationality from Windows command prompt.
i.e. if you do "ls -ltra", it'll list all the directories/files (including hidden).
2. Create fileA.txt, fileB.txt as mentioned in my post above.
2. Create a file "replace.bat" and put the following lines:
Code: Select all
[color=#0040BF]@set sbk=%~1%
@set ebk=%~2%
@set fileA=%~3%
@set fileB=%~4%
@echo Start Keyword: %sbk%
@echo End Keyword: %ebk%
@echo
@echo Source File : %fileB%
@echo Destination : %fileA%
@echo -------------------------------------------
@echo Replace block - Start
@echo -------------------------------------------
@sed -n "1,/%sbk%/p" %fileA% > %fileA%.new
@sed -n "/%sbk%/,/%ebk%/p" %fileB% >> %fileA%.new
@sed -n "/%ebk%/,$p" %fileA% >> %fileA%.new
@cat -n %fileA%.new
@echo -------------------------------------------
@echo Replace block - Giga
@echo -------------------------------------------
@uniq %fileA%.new > %fileA%
@del %fileA%.new[/color]
4. Run the following command from Windows command prompt:
replace.bat ":: Block A Start" ":: Block A End" fileA.txt fileB.txt
Where, param1: Start Keyword,
param2: End Keyword,
param3: The file in which you want to change from param4 block file
data.
param4: from where you pick the Start/End block data and put it
inside param3 file.
PS:
- Param order changed per my initial post / request.
- This is not a good solution but works and gives more power as you get
Linux power in Windows.
easy, peasy !!!!
Re: Replace text in a file - batch script
I use Cygwin too sometimes, or a python script. sed is also very powerfull, you can try it
Re: Replace text in a file - batch script
I Fixed the error, it was because of an extra space after the line numbers
I Edited the Code above and also fixed a problem that could appear if the keywords is in lines greater than the 9th line it now can handle from line 1 till 99999
I Edited the Code above and also fixed a problem that could appear if the keywords is in lines greater than the 9th line it now can handle from line 1 till 99999
Re: Replace text in a file - batch script
Thanks, actually your solution is good in the sense that it takes care of keywords (can be different) from both files. THanks abc.
Re: Replace text in a file - batch script
gigaaks wrote:Thanks, actually your solution is good in the sense that it takes care of keywords (can be different) from both files. THanks abc.
I'm now trying to make the original batch file you requested but modifying the filen in plcae without using
temporary file is difficult.
IMPORTANT NOTE:
while i was testing, if thee was empty lines in the block before the start keyword, the SLine
will get wrong line number because the empty lines dosn't count in the for loop, so the Sline number will be equal to Sline number - (how many empty lines exist before it)
and that applay to the rest of the blocks too
Re: Replace text in a file - batch script
I Changed the Function up in the code, it is the same but more efficient
From:
To:
From:
Code: Select all
:line_number
:: word is first parameter and will hold the word to search for
:: file is the file where to search in
:: var is the variable name that will hold the line number
set "word=%~1"
set "file=%~2"
set "var=%~3"
for /f "skip=2 tokens=* delims=" %%A in ('find /N "%word%" "%file%"') Do (
set line=%%A
IF "!line:~2,1!" == "]" set "%var%=!line:~1,1!"
IF "!line:~3,1!" == "]" set "%var%=!line:~1,2!"
IF "!line:~4,1!" == "]" set "%var%=!line:~1,3!"
IF "!line:~5,1!" == "]" set "%var%=!line:~1,4!"
IF "!line:~6,1!" == "]" set "%var%=!line:~1,5!"
)
To:
Code: Select all
:line_number
:: word is first parameter and will hold the word to search for
:: file is the file where to search in
:: var is the variable name that will hold the line number
set "word=%~1"
set "file=%~2"
set "var=%~3"
for /f "skip=2 tokens=2 delims=[]" %%A in ('find /N "%word%" "%file%"') Do set "%var%=%%A"