Page 1 of 2
findstr in variable
Posted: 11 Feb 2013 16:02
by geraldjr30
Hi. I have the following which counts the number of lines in the file:
Findstr /R /N "^" "C:\test\test.csv" | find /c ":"
I am trying to figure out how to get the resulting number into a variable so i can output:
there are VARIABLE lines.
Please help. Thanks
Re: findstr in variable
Posted: 11 Feb 2013 16:38
by shirulkar
Following work
Code: Select all
Findstr /R /N "^" "C:\test\test.csv" | find /c ":" >>a.txt
set /p "abc="< a.txt
del a.txt
Re: findstr in variable
Posted: 11 Feb 2013 20:39
by Squashman
Code: Select all
for /f "tokens=2 delims=:" %%G in ('find /c /v "" temp.csv') do set lines=%%G
echo %lines%
Re: findstr in variable
Posted: 11 Feb 2013 20:40
by geraldjr30
hi. that tip helped me get the number of lines into a file called a.txt. but i need to know how to get it into a variable on the screen and output it like:
"there are n lines"
Re: findstr in variable
Posted: 11 Feb 2013 20:55
by geraldjr30
hi squashman. i tried your suggestion but its not working.
Re: findstr in variable
Posted: 11 Feb 2013 20:57
by Squashman
geraldjr30 wrote:hi. that tip helped me get the number of lines into a file called a.txt. but i need to know how to get it into a variable on the screen and output it like:
"there are n lines"
a.txt is being redirected into the variable abc.
If you don't like the script creating a temp file then you can use my code.
Re: findstr in variable
Posted: 11 Feb 2013 21:00
by Squashman
Works just fine.
Code: Select all
C:\Users\Squashman>type temp.csv
line1
line2
line3
line5
C:\Users\Squashman>type lines.bat
@echo off
for /f "tokens=2 delims=:" %%G in ('find /c /v "" temp.csv') do set lines=%%G
echo %lines%
C:\Users\Squashman>lines.bat
5
C:\Users\Squashman>
Re: findstr in variable
Posted: 12 Feb 2013 00:53
by foxidrive
Here is a modification on Squashman's code.
Using the < redirection makes find return only the number required.
Use it like this:
countlines.bat "file full of lines.txt"
Code: Select all
@echo off
set "file=%~1"
for /f %%G in ('find /c /v "" ^< "%file%"') do set lines=%%G
echo %lines%
pause
Re: findstr in variable
Posted: 13 Feb 2013 10:43
by booga73
maybe this will work .. . . here, you may modify it as you wish .. .
:: code start to identify number of lines in a text
Set FileName=sample.txt
Set /a LineNumb=0
for /f "tokens=*" %%a in ('find /c /v "" %FileName%') do set /a LineNumb=%%a
:: code end to identify number of lines in a text
This will help find the number of lines in your text file, which is set to a variable LineNumb.
Set FileName=sample.txt ---> this is the file that you want to read the number of lines
Set /a LineNumb=0 ----> this is the variable which the code can store the number of lines found.
After creating a for loop, the code that resides in the loop, 'find /c /v "" %FileName%' will take the file name specified in the variable FileName and will count the number of lines in the text file.
the /c ---> Displays only the count of lines containing the string.
the /v ---> Displays all lines NOT containing the specified string.
so the code should find a line that has string or characters, etc and should count the empty lines too in the text file.
Hope that helps! best regards, Boog73
Re: findstr in variable
Posted: 13 Feb 2013 11:01
by Squashman
booga73 wrote:for /f "tokens=*" %%a in ('find /c /v "" %FileName%') do set /a LineNumb=%%a
That don't look right.
Re: findstr in variable
Posted: 13 Feb 2013 12:48
by booga73
I've actually used this code to pass the value of !LineNumb! to another subroutine; hadn't had issues.
My previous post to use get assistance with html table inquiry, I actually use that code at the beginning to find how many times my for loop at to run before reaching the value stored in !LineNumb!
thank you for that help too!
do this though, create a folder, temp1 on root of drive c:. Next, place sample.txt into temp1, fill the text file with "X" amount of lines. also,copy/paste the batch code below into temp1 folder.
the value which resides in LineNumb will be copied into value MaxLines. The for loop in routine2 will loop approximately "X" amount of times the value of MaxLines then for loop will exit at that max value of MaxLines. MyResults.txt will have "X" amount of lines from the value of MaxLines which was given by LineNumb value.
if it can be coded better and easier, I'm game .. best regards, Booga73
:: beginning routine1
Set FileName=sample.txt
Set /a LineNumb=0
for /f "tokens=*" %%a in ('find /c /v "" %FileName%') do set /a LineNumb=%%a
:: end routine1
:: beginning routine2
SET /A maxlines=!LineNumb!
SET /A linecount=0
FOR /F "tokens=*" %%b IN (c:\temp\userFileNames.txt) DO (
IF !linecount! EQU %maxlines% GOTO ExitLoop
echo %%b >> c:\temp\MyResults.txt
SET /A linecount+=1
)
:ExitLoop
start c:\temp\MyResults.txt
exit
:: end routine2
Re: findstr in variable
Posted: 13 Feb 2013 13:12
by Squashman
booga73 wrote:I've actually used this code to pass the value of !LineNumb! to another subroutine; hadn't had issues.
Run the code with the FOR loop alone. The code I quoted from your post and tell me what your output is.
Re: findstr in variable
Posted: 13 Feb 2013 14:25
by booga73
hmmm... is this my 2nd for loop? which you are referring about?
Re: findstr in variable
Posted: 13 Feb 2013 14:46
by booga73
:: ------------------beginning of batch:
:: beginning routine1
Set FileName=sample.txt
:: Set /a LineNumb=0
for /f %%a in ('find /c /v "" %FileName%') do set /a LineNumb=%%a
:: end routine1
:: beginning routine2
SET /A maxlines=!LineNumb!
SET /A linecount=0
FOR /F "tokens=1" %%b IN (sample.txt) DO (
IF !linecount! EQU %maxlines% GOTO ExitLoop
echo %%b >> c:\temp1\MyResults.txt
SET /A linecount+=1
)
:ExitLoop
start MyResults.txt
:: end routine2
pause
exit
:: ------------------ end of batch:
original code: for /f "tokens=*" %%a in ('find /c /v "" %FileName%') do set /a LineNumb=%%a
I removed the tokens statement but when I run the batch file, I still get MyResults.txt but during the running of my batch file I get .. .
.. . Missing operand.
I get that error shortly after this initial for loop is ran.
not sure exactly why that is occuring. r/ Booga73
Re: findstr in variable
Posted: 13 Feb 2013 15:04
by Squashman
Run this code in a batch file or from the command line with any text file and look at what the output is and you will see why your code fails.
find /c /v "" somfile.txt