How to Set a multiline string replacement in a batch file - batch-file

I was developing a bat file to run my experiment with two varying parameters for each alpha1 there is 10 ws value, i use the bat file to change the variable inside the experiment code and to make 20 files of alpha with 10 subfolder in each alpha folder , i can't see where is the problem
#Echo off
SET "file=synch-ring.f"
SET /a Line#ToSearch1=14
SET /a Line#ToSearch2=27
FOR /L %%I IN (1,1,20) do (
FOR /L %%J IN (1,1,10) do (
md %%I
md %%I\%%J
(FOR /f "tokens=1*delims=:" %%a IN ('findstr /n "^" "%file%"') DO (
SET "Line=%%a"
IF %%a equ %Line#ToSearch% SET "Line= Alpha1=%%I"
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO(!Line!
ENDLOCAL
IF %%a equ %Line#ToSearch2% SET "Line= ws=%%J"
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO(!Line!
ENDLOCAL
))"%%J.f"
Move %%J.f %%I\%%J
))
pause

Im not sure that I've properly understood your intent, but this may be helpful to you:
#Echo Off
Set "file=synch-ring.f"
Set "Line#ToSearch1=14"
Set "Line#ToSearch2=27"
If Not Exist "%file%" Exit /B
For /L %%I In (1,1,20) Do (
For /L %%J In (1,1,10) Do (
MD "%%I\%%J" 2>Nul
(
For /F "Tokens=1* Delims=:" %%A In ('FindStr /N "^" "%file%"'
) Do (
If %%A NEq %Line#ToSearch% (
If %%A NEq %Line#ToSearch2% (
Echo=%%B
) Else (
Echo= ws=%%J
)
) Else (
Echo= Alpha1=%%I"
)
)
)>"%%I\%%J\%%J.f"
)
)
Pause

Related

How to add a goto to a for loop

I am working on a script that edits files and I am building a choice goto menu.
The script itself works fine, it reads from a file name that has ( ) in it. So my files are saved like this (1) filename.txt, (2) filename.txt ....(100) filename.txt
the content of the file has "times": 125489, saved in it, the script removes the 100 from the file name and does this "times": 100,
This is a small part of my content for my txt files it has a 4 indent space per line I believe the format is Json
"aidr": 3.58,
"nlpr": 0.5,
"tafr": 0.5,
"titp": 0.5,
"trld": 0.0,
"tssp": 0.5,
"tssr": 0.5,
"ttup": 0.5,
"ttpp": 0.5,
"times": 125,
"Stamp": 125,
"ppiid": 649,
"otiid": 649,
"apcid": 9,
"orcid": 9,
"jpcns": 0,
"agpns": 0,
"opcns": 0,
"rppns": 0,
I recently found that when I merged all my single scripts together it slows down dramatically. Before the script processed 500 .txt files in 1 minute but now it takes 30 minutes.
My goal is to find a way to bring back the speed of this script.
I heard and read that adding a goto before and after the loop could make the script fast again.
#echo off
:Menu
ECHO ################################################################
echo.
ECHO 1 - Script 1
ECHO 2 - Script 2
ECHO 3 - Script 3
ECHO 4 - Script 4
ECHO 5 - Script 5
ECHO 100 - Script 100
echo.
set pass=
:: the choice command
set /p Mchoice=Make Your Choice:
::goto choices
goto=:%Mchoice%
goto %goto%
:1
#ECHO Off
setlocal ENABLEDELAYEDEXPANSION
set "ToReplace1="times": "
SET "sourcedir=New folder 1"
SET "destdir=New fodler 2"
FOR /f "delims=" %%q IN ('dir /b /a-d "%sourcedir%\(*)*.txt"') DO (
(
FOR /f "tokens=1 delims=()" %%j IN ("%%q") DO (
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "%sourcedir%\%%q"') do (
set "line=%%b"
if defined line IF "%%b" neq "!line:times=!" CALL :Nums1 %%j
echo(!line!
)
)
)>"%destdir%\%%q"
)
GOTO :Menu
:Nums1
SET "original=%line%"
FOR /L %%s IN (0,1,9) DO set "line=!line:%ToReplace1%%%s=%ToReplace1%!"
IF "%original%" neq "%line%" goto Nums1
set "line=!line:%ToReplace1%=%ToReplace1%%1!"
GOTO :eof
:2
#ECHO Off
setlocal ENABLEDELAYEDEXPANSION
set "ToReplace2="Stamp": "
SET "sourcedir=New folder 1"
SET "destdir=New fodler 2"
FOR /f "delims=" %%q IN ('dir /b /a-d "%sourcedir%\(*)*.txt"') DO (
(
FOR /f "tokens=1 delims=()" %%j IN ("%%q") DO (
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "%sourcedir%\%%q"') do (
set "line=%%b"
if defined line IF "%%b" neq "!line:Stamp=!" CALL :Nums2 %%j
echo(!line!
)
)
)>"%destdir%\%%q"
)
GOTO :Menu
:Nums2
SET "original=%line%"
FOR /L %%s IN (0,1,9) DO set "line=!line:%ToReplace2%%%s=%ToReplace2%!"
IF "%original%" neq "%line%" goto Nums1
set "line=!line:%ToReplace2%=%ToReplace2%%1!"
GOTO :eof
:15
#ECHO Off
setlocal ENABLEDELAYEDEXPANSION
cd /d "%~dp0"
set "ToReplace1="times": "
set "ToReplace2="Stamp": "
set "ToReplace3="jpcns": "
SET "sourcedir15=New folder 1"
SET "destdir15=New fodler 2"
FOR /f "delims=" %%q IN ('dir /b /s /a-d "%sourcedir15%\(*)*.txt"') DO (
SET "newdest=%%~dpq"
SET "newdest=!newdest:%sourcedir15%=%destdir15%!"
SET "newdest=!newdest:~0,-1!"
MD "!newdest!" 2>nul
(
FOR /f "tokens=1 delims=()" %%j IN ("%%~nxq") DO (
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "%%q"') do (
set "line=%%b"
if defined line IF "%%b" neq "!line:times=!" CALL :Nums15 %%j
if defined line IF "%%b" neq "!line:Stamp=!" CALL :Nums15 %%j
if defined line IF "%%b" neq "!line:jpcns=!" CALL :Nums15 %%j
echo(!line!
)
)
)>"!newdest!\%%~nxq"
)
GOTO :Menu
:Nums15
SET "original=%line%"
FOR /L %%s IN (0,1,9) DO set "line=!line:%ToReplace1%%%s=%ToReplace1%!"
FOR /L %%s IN (0,1,9) DO set "line=!line:%ToReplace2%%%s=%ToReplace2%!"
FOR /L %%s IN (0,1,9) DO set "line=!line:%ToReplace3%%%s=%ToReplace3%!"
IF "%original%" neq "%line%" goto Nums15
set "line=!line:%ToReplace1%=%ToReplace1%%1!"
set "line=!line:%ToReplace2%=%ToReplace2%%1!"
set "line=!line:%ToReplace3%=%ToReplace3%%1!"
GOTO :eof
I've done a few exercises on this problem, but I've been unable to locate anything that might result in a 30:1 speed difference.
This version
FOR /f "delims=" %%q IN ('dir /b /a-d "%sourcedir%\(*)*.txt"') DO (
(
FOR /f "tokens=1 delims=()" %%j IN ("%%q") DO (
for /F "tokens=1* delims=:" %%b in ('findstr /N "^" "%sourcedir%\%%q"') do (
set "line=%%c"
if defined line (
IF "%%c" equ "!line:times=!" (ECHO %%c) ELSE (
for /F "tokens=1* delims=:" %%v in ("%%c") do ECHO %%v: %%j,
)
) ELSE ECHO(
)
)
)>"%destdir%\%%q"
)
Seemed to be a little faster.
This:
FOR /f "delims=" %%q IN ('dir /b /a-d "%sourcedir%\(*)*.txt"') DO (
(
FOR /f "tokens=1 delims=()" %%j IN ("%%q") DO (
SET "skipover="
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "%sourcedir%\%%q"') do (
IF DEFINED skipover (ECHO(%%b
) ELSE (
set "line=%%b"
if defined line IF "%%b" neq "!line:times=!" SET "skipover=y"&CALL :Nums1 %%j
echo(!line!
)
)
)
)>"%destdir%\%%q"
)
uses a Boolean variable skipover to bypass some of the processing once the target line has been found and manipulated - but it actually seemed slower.
This
FOR /f "delims=" %%q IN ('dir /b /a-d "%sourcedir%\(*)*.txt"') DO (
(
FOR /f "tokens=1 delims=()" %%j IN ("%%q") DO (
for /F "tokens=1* delims=:" %%b in ('findstr /N "^" "%sourcedir%\%%q"') do (
set "line=%%c"
if defined line (
IF "%%c" equ "!line:times=!" (ECHO %%c) ELSE (
for /F "tokens=1* delims=:" %%v in ("%%c") do ECHO %%v: %%j,
)
) ELSE ECHO(
)
)
)>"%destdir%\%%q"
)
was consistently a little faster than the original - it avoids the subroutine call.
And this:
FOR /f "delims=" %%q IN ('dir /b /a-d "%sourcedir%\(*)*.txt"') DO (
(
FOR /f "tokens=1 delims=()" %%j IN ("%%q") DO (
SET "skipover="
for /F "usebackqdelims=" %%e in ("%sourcedir%\%%q") do (
IF DEFINED skipover (ECHO/%%e
) ELSE (
set "line=%%e"
if defined line (
IF "%%e" equ "!line:times=!" (ECHO %%e
) ELSE (
SET "skipover=Y"
for /F "tokens=1* delims=:" %%v in ("%%e") do ECHO %%v: %%j,
)
) ELSE ECHO/
)
)
)
)>"%destdir%\%%q"
)
was consistently nearly twice as fast, using the skip-if-it's been-changed technique and avoiding findstr. It won't reproduce empty lines, though.
I also found that the position of the target in the source was signiificant. Using a 700-line version of the sample data, placing the target at the end was half-speed of placing it at the start of the file, so I was able to get nearly a 4:1 speed difference between the original and this last technique if the target was placed early in the source data (running 1,000 700-line files).
I presume that sub appeared in the original post by way of editing-out the irrelevant portions of the code, but it seemed to be reversed in sense, the way it was written...
--- based on version 3,
:: remove variables starting #
FOR /F "delims==" %%e In ('set # 2^>Nul') DO SET "%%e="
:: Read controls from file "%1"
FOR /f "usebackqtokens=1*" %%b IN ("%~1") DO (
SET "#%%b=%%c"
FINDSTR /x /i /L /c:":#%%b" <"%~f0" >NUL
IF ERRORLEVEL 1 ECHO LABEL ":#%%b" NOT found&SET "destdir="
)
IF NOT DEFINED destdir ECHO Required LABEL(s) NOT found &GOTO :eof
FOR /f "delims=" %%q IN ('dir /b /a-d "%sourcedir%\(*)*.txt"') DO (
(
rem %%q contains the name of the file to be processed
rem remove variables starting $
FOR /F "delims==" %%e In ('set $ 2^>Nul') DO SET "%%e="
rem extract filenumber if sourcefilename is (number)something
SET "filenumber="
FOR /f "tokens=1 delims=()" %%j IN ("%%q") DO SET "filenumber=%%j"
rem
rem now process the file
for /F "tokens=1* delims=:" %%b in ('findstr /N "^" "%sourcedir%\%%q"') do (
set "line=%%c"
if defined line (
rem if any #name is detected, take action
SET "processline=Y"
FOR /f "tokens=1,2delims=#=" %%u IN ('set #') DO IF DEFINED processline IF "%%c" neq "!line:%%u=!" (
SET "processline="
CALL :#%%u %%v
)
IF DEFINED processline ECHO %%c
) ELSE ECHO/
)
)>"%destdir%\%%q"
)
GOTO :eof
:#times
rem replace times value with filenumber
ECHO "times": %filenumber%,
GOTO :eof
:#titp
rem replace titp value with value read from file (%1)
ECHO "titp": %1,
GOTO :eof
:#tssp
rem put newz value before and zwen value after tssp value
ECHO "newz": whatever,
ECHO %line%
ECHO "zwen": revetahw,
GOTO :eof
:#trld
rem delete all trld values
GOTO :eof
:#tssr
rem replace the third and the fifth tssr value (I have multiples)
rem note that all $ variables are deleted FOR each file
SET /a $#tssr+=1
IF %$#tssr%==3 ECHO "tssr": thirdtime,&GOTO :eof
IF %$#tssr%==5 ECHO "tssr": fifthtime,&GOTO :eof
ECHO %line%
GOTO :eof
Using a control file contents
times #
titp jello
trld #
tssp #
tssr #
So - this is an attempt to obviate the repeated cloning of the file-processing logic.
First, I'm setting all variables that start # to nothing, un-defining them. That way, the remainder of the code knows that any variable starting # has not been inherited from elsewhere.
Next step : read the file to set up
#times=#
#titp=jello
#trld=#
#tssp=#
#tssr=#
The code then examines itself to make sure that subroutines :#times etc all exist and complains if they don't. destdir is simply a convenient non-empty variable used as a flag - we'll abort if the subroutines are missing, so no problem there.
Next - read the filenames. Obviously, the filemask may change depending on the selection made from the menu.
Then, for each file, kill all $ variables for the same reason as # but this time for each file to be processed.
Then set up variables to contain whatever data about the file is appropriate. Could be size, date, whatever - extract-parenethesised-number is shown. No harm in doing such things for all files, even if the data is not used for this particular menu selection.
Then process the file, detecting the keystrings from the # array. Call :#keystring if found, regurgitate line if not.
Each :# routine does what it does. A variety of possibilities is shown. Naturally, any particular routine could be controlled further by testing the selection made.
Setting the value assigned to #? to a value other than # will deliver that value to the :#? subroutine as parameters. Use them as you will - constants, whatever - just interpret the parameters delivered.
Naturally, you could set the # variables manually for particular menu selections instead of reading them from a file.
But - this is drifting way away from the timing problem - just adding more facilities (I'm a chronic implementor of bells and whistles) so I'd suggest to end discussion here.

Add numbers with same text together

I would like to create a batch file in which I can see what I have collected in a game.
The game saves this information in a .txt file.
The output would look like this.
70x Silver.
Back Pearl.
41x Copper.
Amethyst.
Amethyst.
12x Silver.
Back Pearl.
21x Copper.
5x Silver.
Back Pearl.
Back Pearl.
Amethyst.
What I want to do now, is to add the items with the same name together, like this:
128x Silver.
4x Back Pearl.
62x Copper.
3x Amethyst.
There are hundreds of items with different names, not just these 4.
Would that be possible?
Any help would be appreciated. Thanks!
Another one!
#echo off
setlocal EnableDelayedExpansion
for /F "delims=" %%l in (test.txt) do for /F "tokens=1*" %%a in ("%%l") do (
set "first=%%a"
if "!first:~-1!" equ "x" (set /A "num=!first:~0,-1!") else set "num=0"
if !num! equ 0 (
set "rest=%%l"
set /A "count[!rest: =_!]+=1"
) else (
set "rest=%%b"
set /A "count[!rest: =_!]+=num"
)
)
(for /F "tokens=2* delims=[]=" %%a in ('set count[') do (
set "item=%%a"
if %%b equ 1 (
echo !item:_= !
) else (
echo %%bx !item:_= !
)
)) > summary.txt
#ECHO OFF
SETLOCAL
rem The following settings for the source directory and filename are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\q72672485.txt"
:: remove variables starting #
FOR /F "delims==" %%b In ('set # 2^>Nul') DO SET "%%b="
FOR /f "usebackqdelims=" %%b IN ("%filename1%") DO (
CALL :sub %%b
)
SETLOCAL ENABLEDELAYEDEXPANSION
(
FOR /F "tokens=1,2delims==" %%b In ('set # 2^>Nul') DO (
SET "line=%%cx%%b"
ECHO !line:#= !
)
)>summary.txt
endlocal
type summary.txt
GOTO :EOF
:sub
SET "quantity=%1"
SET "line=%*"
IF /i "%quantity:~-1%"=="x" (SET /a quantity=%quantity:~0,-1%&SET "line=%line:* =%") ELSE SET quantity=1
IF %quantity%==0 SET /a quantity=1&SET "line=%*"
SET /a #%line: =#%+=quantity
GOTO :eof
Different approach...
Would that be possible? - Yes.
#echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in (t.txt) do (
set " z=%%a"
set " z=!z:x =#!"
set " z=!z: =_!"
for /f "tokens=1,2 delims=#" %%b in ("!z!") do (
if "%%c" == "" (
set "x=1"
set "y=%%b
) else (
set "x=%%b"
set "y=%%c"
)
set /a #!y!+=!x!
)
)
(for /f "tokens=1,2 delims=#=" %%a in ('set #') do (
set "x=%%a"
set "y=%%bx "
echo !y:~0,4! !x:_= !
))>summary.txt
Output with your example data (I hope, alphabetic sorting is ok for you):
3x Amethyst.
4x Back Pearl.
62x Copper.
87x Silver.
(your calculation of 120 silver might be a bit optimistic with the given input data)
This is a different approach that use a "file merge" method. The overall code is somewhat simpler than other methods...
#echo off
setlocal EnableDelayedExpansion
set "lineNum=0"
(for /F "tokens=1,2* delims=:x " %%a in ('(type test.txt ^& echo 0x^) ^| findstr /N "[0-9][0-9]*x"') do (
if !lineNum! lss %%a call :locateLine %%a
set "line=%%c"
set /A "count[!line: =_!]+=%%b"
)) < test.txt
set "count[="
(for /F "tokens=2* delims=[]=" %%a in ('set count[') do (
set "item=%%a"
if %%b equ 1 (echo !item:_= !) else echo %%bx !item:_= !
)) > summary.txt
goto :EOF
:locateLine num
set /A "lineNum+=1" & set /P "line=" & if errorlevel 1 exit /B
if %lineNum% lss %1 set /A "count[%line: =_%]+=1" & goto locateLine
exit /B
Another approach (splitting the file into items with and without quantity) (also fixing the Pollux Infusion issue in my first answer):
#echo off
setlocal enabledelayedexpansion
REM process lines without quantity:
for /f "delims=" %%a in ('type test.txt^|findstr /vrc:"[0123456789][0123456789]*x "') do call :count 1 "%%a"
REM process lines with quantity:
for /f "tokens=1*delims=x " %%a in ('type test.txt^|findstr /rc:"[0123456789][0123456789]*x "') do call :count %%a "%%b"
REM reformat:
(for /f "tokens=1,2 delims=#=" %%a in ('set #') do (
set "count=%%bx "
set "line=!count:~0,5!%%a" &REM including '1x'
if %%b==1 set "line= %%a" &REM supressing '1x'
echo !line:_= !
))>summary.txt
type summary.txt
goto :eof
:count
set item=%~2
set "item=%item: =_%"
set /a #%item% +=%1
Included both with and without 1x. Remove the line, you don't want.

how to remove a Negative using a txt editing batch script

I need a little help with this
I recently asked a different question on this same script
Now I have my scripted working for me and found a new issue
Here is my script
#ECHO Off
setlocal ENABLEDELAYEDEXPANSION
cd /d "%~dp0"
:: Significant part of string
set "params1="okhg": "
set "params2="tfed": "
set "params3="pkna": "
set "params4=txt": ""
#For %%G In ("%~dp0Preparing") Do Set "sourcedir=%%~fG"
#For %%G In ("%~dp0Ready") Do Set "destdir=%%~fG"
FOR /f "delims=" %%q IN ('dir /b /s /a-d "%sourcedir%\(*)*.txt"') DO (
rem calculate new destination directory
SET "newdest=%%~dpq"
SET "newdest=!newdest:%sourcedir%=%destdir%!"
SET "newdest=!newdest:~0,-1!"
MD "!newdest!" 2>nul
(
FOR /f "tokens=1 delims=()" %%j IN ("%%~nxq") DO (
rem %%j now has sequence number
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "%%q"') do (
set "line=%%b"
for %%x in (%params1%) do if defined line IF "%%b" neq "!line:%%x=!" CALL :subs1 %%j
for %%x in (%params2%) do if defined line IF "%%b" neq "!line:%%x=!" CALL :subs2 %%j
for %%x in (%params3%) do if defined line IF "%%b" neq "!line:%%x=!" CALL :subs3 %%j
for %%x in (%params4%) do if defined line IF "%%b" neq "!line:%%x=!" CALL :subs4 %%j
echo(!line!
)
)
)>"!newdest!\%%~nxq"
)
GOTO :eof
:: substitute
:subs1
SET "original=%line%"
FOR /L %%s IN (0,1,9) DO set "line=!line:%params1%%%s=%params1%!"
IF "%original%" neq "%line%" goto subs1
set "line=!line:%params1%=%params1%%1!"
GOTO :eof
:subs2
SET "original=%line%"
FOR /L %%s IN (0,1,9) DO set "line=!line:%params2%%%s=%params2%!"
IF "%original%" neq "%line%" goto subs2
set "line=!line:%params2%=%params2%%1!"
GOTO :eof
:subs3
SET "original=%line%"
FOR /L %%s IN (0,1,9) DO set "line=!line:%params3%%%s=%params3%!"
IF "%original%" neq "%line%" goto subs3
set "line=!line:%params3%=%params3%%1!"
GOTO :eof
:subs4
SET "original=%line%"
FOR /L %%s IN (0,1,9) DO set "line=!line:%params4%%%s=%params4%!"
IF "%original%" neq "%line%" goto subs3
set "line=!line:%params4%=%params4%CITY%1!"
GOTO :eof
This is how it works
ORIGNAL FOLDER
+ ----- FOLDER 1
--------+ --(100) Filename.txt
-------------+ -- "okhg": 452587"
-------------+ -- "tfed": 6541"
-------------+ -- "pkna": -58452"
-------------+ -- "txt": ""
RESULTS FOLDER
+ ----- FOLDER 1
--------+ --(100) Filename.txt
-------------+ -- "okhg": 100"
-------------+ -- "tfed": 100"
-------------+ -- "pkna": 100-58452"
-------------+ -- "txt": "CITY100"
The issue I am coming across with is that this line has a negative
"pkna": -58452"
I have tested different ways to make the scripts remove the negative
if I leave it like this, I get this result
set "params3="pkna": "
"pkna": 100-58452"
if I do this, I get this result
set "params3="pkna": -"
"pkna": -100"
what I am looking for is this
"pkna": 100"
Thank you
Edit:
The question has been updated with an answer.
The code will "code" multiple row/lines in to a text file using its prefix name, or with a "more direct" coding.
If you do know a way to clean up the code feel free to post an answer to do so., I would like to leave a script that I can continue to add more codes if needed
Thank you for all the hard work you'll provide us with to make our scripts better
#ECHO Off
setlocal ENABLEDELAYEDEXPANSION
cd /d "%~dp0"
:: Significant part of string
set "params1="okhg": "
set "params2="tfed": "
set "params3="pkna": "
set "params4=txt": ""
#For %%G In ("%~dp0Preparing") Do Set "sourcedir=%%~fG"
#For %%G In ("%~dp0Ready") Do Set "destdir=%%~fG"
FOR /f "delims=" %%q IN ('dir /b /s /a-d "%sourcedir%\(*)*.txt"') DO (
rem calculate new destination directory
SET "newdest=%%~dpq"
SET "newdest=!newdest:%sourcedir%=%destdir%!"
SET "newdest=!newdest:~0,-1!"
MD "!newdest!" 2>nul
(
rem this will take the data in the filename enclosed in (!)
FOR /f "tokens=1 delims=()" %%j IN ("%%~nxq") DO (
rem this will replace any random codes found after : with 10
FOR /f "tokens=1 delims=()" %%k IN ("10") DO (
rem this will add any wording after this : and will take data enclosed in (!)
FOR /f "tokens=1 delims=()" %%l IN ("%%~nxq") DO (
rem %%j %%k %%l now has sequence number or pattern
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "%%q"') do (
set "line=%%b"
rem in order to make each code work I gave them there own ::Substitute
for %%x in (%params1%) do if defined line IF "%%b" neq "!line:%%x=!" CALL :subs1 %%j
for %%x in (%params2%) do if defined line IF "%%b" neq "!line:%%x=!" CALL :subs2 %%j
for %%x in (%params3%) do if defined line IF "%%b" neq "!line:%%x=!" CALL :subs3 %%k
for %%x in (%params4%) do if defined line IF "%%b" neq "!line:%%x=!" CALL :subs4 %%l
echo(!line!
)
)
)
)
)>"!newdest!\%%~nxq"
)
GOTO :eof
:: substitute
:subs1
SET "line=%line:-=%"
SET "original=%line%"
FOR /L %%s IN (0,1,9) DO set "line=!line:%params1%%%s=%params1%!"
IF "%original%" neq "%line%" goto subs1
set "line=!line:%params1%=%params1%%1!"
GOTO :eof
:subs2
SET "line=%line:-=%"
SET "original=%line%"
FOR /L %%s IN (0,1,9) DO set "line=!line:%params2%%%s=%params2%!"
IF "%original%" neq "%line%" goto subs2
set "line=!line:%params2%=%params2%%1!"
GOTO :eof
:subs3
SET "line=%line:-=%"
SET "original=%line%"
FOR /L %%s IN (0,1,9) DO set "line=!line:%params3%%%s=%params3%!"
IF "%original%" neq "%line%" goto subs3
set "line=!line:%params3%=%params3%%1!"
GOTO :eof
:subs4
SET "line=%line:-=%"
SET "original=%line%"
FOR /L %%s IN (0,1,9) DO set "line=!line:%params4%%%s=%params4%!"
IF "%original%" neq "%line%" goto subs3
set "line=!line:%params4%=%params4%City%1!"
GOTO :eof

select file with the largest number of lines

It count number of line in all file but i need select the file with the largest number of lines.
for %%a in (*.*) do (
for /f %%b in (' find "" /v /c ^< "%%a" ') do (
echo %%a=%%b
)
)
Output:
test.txt=10
asdasd.txt=15
asdasd.txt=20
I need output:
asdasd.txt=20
only one file with the largest number of lines
Pleas help. Thx
First option, calling find for each file and then parsing output
#echo off
setlocal enableextensions enabledelayedexpansion
set "maxLines=0"
set "maxFile="
for %%a in (*.txt) do (
for /f %%b in ('^<"%%a" find /c /v ""') do if %%b gtr !maxLines! (
set "maxLines=%%b"
set "maxFile=%%a"
)
)
echo %maxFile%=%maxLines%
Second option, calling find only one time and parsing output
#echo off
setlocal enableextensions enabledelayedexpansion
set "maxLines=0"
set "maxFile="
for /f "tokens=* delims=- " %%a in ('find /c /v "" *.txt '
) do for /f "tokens=1,2 delims=:" %%b in ("%%a"
) do if %%c gtr !maxLines! (
for %%d in (%%c) do set "maxLines=%%d"
set "maxFile=%%b"
)
echo %maxFile%=%maxLines%
you need to set additional vars:
echo off
setlocal enableextensions enabledelayedexpansion
set /A counter=0
for /f "tokens=1,2* delims=-:" %%a in ('find /c /v "" *.*') do (
if !counter! LSS %%b (
set /A counter=%%b
set bezieher=%%~nxa)
)
)
echo %bezieher%=%counter%
pause
Rem Respect to the scriptwriter "MC ND" above!!

Returning the surrounding lines to a string windows batch file

I'm using findstr to search for a string in a file in the following manner:
findstr "test" file.txt
This is returning the line where test is found, but I would like to return the 3 lines above and below the matching line. I've had a look and it doesn't seem like there are any in-built options to findstr to return surrounding lines.
Here you go
#echo off
setlocal enabledelayedexpansion
for /f "tokens=1 delims=:" %%a in ('findstr /n "hello" file.txt') do (
set /a line=%%a
)
set /a num=0
for /l %%b in (3,-1,1) do (
set /a lines[!num!]=!line!-%%b
set /a num+=1
)
for /l %%c in (1,1,3) do (
set /a lines[!num!]=!line!+%%c
set /a num+=1
)
set /a count=1
for /f "tokens=* delims=" %%d in (file.txt) do (
for /l %%e in (%lines[0]%,1,%lines[5]%) do (
if !count!==%%e if not %%e==!line! echo %%d
)
set /a count+=1
)
pause >nul

Resources