findstr in variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
geraldjr30
Posts: 3
Joined: 11 Feb 2013 15:57

findstr in variable

#1 Post by geraldjr30 » 11 Feb 2013 16:02

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

shirulkar
Posts: 40
Joined: 15 Jan 2013 23:53

Re: findstr in variable

#2 Post by shirulkar » 11 Feb 2013 16:38

Following work

Code: Select all

Findstr /R /N "^" "C:\test\test.csv" | find /c ":" >>a.txt
set /p "abc="< a.txt
del a.txt

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: findstr in variable

#3 Post by Squashman » 11 Feb 2013 20:39

Code: Select all

for /f "tokens=2 delims=:" %%G in ('find /c /v "" temp.csv') do set lines=%%G
echo %lines%

geraldjr30
Posts: 3
Joined: 11 Feb 2013 15:57

Re: findstr in variable

#4 Post by geraldjr30 » 11 Feb 2013 20:40

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"

geraldjr30
Posts: 3
Joined: 11 Feb 2013 15:57

Re: findstr in variable

#5 Post by geraldjr30 » 11 Feb 2013 20:55

hi squashman. i tried your suggestion but its not working.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: findstr in variable

#6 Post by Squashman » 11 Feb 2013 20:57

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.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: findstr in variable

#7 Post by Squashman » 11 Feb 2013 21:00

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>

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: findstr in variable

#8 Post by foxidrive » 12 Feb 2013 00:53

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

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: findstr in variable

#9 Post by booga73 » 13 Feb 2013 10:43

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 :roll:

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: findstr in variable

#10 Post by Squashman » 13 Feb 2013 11:01

booga73 wrote:for /f "tokens=*" %%a in ('find /c /v "" %FileName%') do set /a LineNumb=%%a

That don't look right. :?:

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: findstr in variable

#11 Post by booga73 » 13 Feb 2013 12:48

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

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: findstr in variable

#12 Post by Squashman » 13 Feb 2013 13:12

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.

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: findstr in variable

#13 Post by booga73 » 13 Feb 2013 14:25

hmmm... is this my 2nd for loop? which you are referring about? :?:

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: findstr in variable

#14 Post by booga73 » 13 Feb 2013 14:46

:: ------------------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

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: findstr in variable

#15 Post by Squashman » 13 Feb 2013 15:04

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

Post Reply