<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Count number of lines in a file using DOS</title>
	<atom:link href="http://brett.batie.com/dosshell/count-number-of-lines-in-a-file-using-dos/feed/" rel="self" type="application/rss+xml" />
	<link>http://brett.batie.com/scripting/count-number-of-lines-in-a-file-using-dos/</link>
	<description>Thoughts of a Software Engineer.</description>
	<lastBuildDate>Thu, 02 Feb 2012 22:08:57 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Dave Shibli</title>
		<link>http://brett.batie.com/scripting/count-number-of-lines-in-a-file-using-dos/comment-page-1/#comment-25530</link>
		<dc:creator>Dave Shibli</dc:creator>
		<pubDate>Wed, 25 Jan 2012 14:57:54 +0000</pubDate>
		<guid isPermaLink="false">http://brett.batie.com/?p=25#comment-25530</guid>
		<description>I had a need to number a text file before bulk inserting into a database so I could keep the lines in order.
findstr /R /N &quot;^&quot; file.txt &gt;&gt; newfile.txt saved my life...Thanks much.
I wrapped it in a command shell and loaded the file names from SQL and indexed nearly 5,000 text files for a forensic analysis.</description>
		<content:encoded><![CDATA[<p>I had a need to number a text file before bulk inserting into a database so I could keep the lines in order.<br />
findstr /R /N &#034;^&#034; file.txt &gt;&gt; newfile.txt saved my life&#8230;Thanks much.<br />
I wrapped it in a command shell and loaded the file names from SQL and indexed nearly 5,000 text files for a forensic analysis.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: john</title>
		<link>http://brett.batie.com/scripting/count-number-of-lines-in-a-file-using-dos/comment-page-1/#comment-19123</link>
		<dc:creator>john</dc:creator>
		<pubDate>Wed, 20 Jul 2011 02:49:37 +0000</pubDate>
		<guid isPermaLink="false">http://brett.batie.com/?p=25#comment-19123</guid>
		<description>Hello,

I have a batch file that counts the number of lines in each text file inside a directory, and then prepends the total number of lines in each file to the beginning of each file in the directory. It looks like this:

&lt;pre class=&quot;brush: bash;&quot;&gt;@echo off
setlocal enabledelayedexpansion
cd /d &quot;C:\Users\John\Desktop\Testing\AllFiles&quot;
for /f &quot;delims=&quot; %%F in (&#039;dir /b *.txt&#039;) do (
   for /f &quot;delims=:&quot; %%N in (&#039;findstr /N /R &quot;^&quot; &quot;%%~dpnxF&quot;&#039;) do set lines=%%N

      echo There are !lines! lines in this file &gt; &quot;%%~dpnxF.temp&quot;
      type &quot;%%~dpnxF&quot;&gt;&gt;&quot;%%~dpnxF.temp&quot;
      del &quot;%%~dpnxF&quot;
      echo &quot;%%~dpnxF.temp&quot; &quot;%%~nxF&quot;
      ren &quot;%%~dpnxF.temp&quot; &quot;%%~nxF&quot;

      )&lt;/pre&gt;

The batch file above works great for small text files in the directory, however, it takes a long time to read the large text files in the directory. 

As you stated in your blog, using the code

findstr /R /N &quot;^&quot; file.txt

Does not work well for counting the lines in large text files. So, i added the following code:

findstr /R /N &quot;^&quot; file.txt &#124; find /C &quot;:&quot;

from your blog into the batch file, and now it looks like this:

&lt;pre class=&quot;brush: bash;&quot;&gt;@echo off
setlocal enabledelayedexpansion
cd /d &quot;C:\Users\John\Desktop\Testing\AllFiles&quot;
for /f &quot;delims=&quot; %%F in (&#039;dir /b *.txt&#039;) do (
   for /f &quot;delims=:&quot; %%N in (&#039;findstr /N /R &quot;^&quot; &quot;%%~dpnxF&quot; &#124; find /C &quot;:&quot;&#039;) do set lines=%%N

      echo There are !lines! lines in this file &gt; &quot;%%~dpnxF.temp&quot;
      type &quot;%%~dpnxF&quot;&gt;&gt;&quot;%%~dpnxF.temp&quot;
      del &quot;%%~dpnxF&quot;
      echo &quot;%%~dpnxF.temp&quot; &quot;%%~nxF&quot;
      ren &quot;%%~dpnxF.temp&quot; &quot;%%~nxF&quot;

      )&lt;/pre&gt;


But, when i run the modified code, i get the following error:

&quot;: was unexpected at this time&quot;

Do you know what i am doing wrong?

Thanks in advance :)</description>
		<content:encoded><![CDATA[<p>Hello,</p>
<p>I have a batch file that counts the number of lines in each text file inside a directory, and then prepends the total number of lines in each file to the beginning of each file in the directory. It looks like this:</p>
<pre class="brush: bash;">@echo off
setlocal enabledelayedexpansion
cd /d "C:\Users\John\Desktop\Testing\AllFiles"
for /f "delims=" %%F in ('dir /b *.txt') do (
   for /f "delims=:" %%N in ('findstr /N /R "^" "%%~dpnxF"') do set lines=%%N

      echo There are !lines! lines in this file &gt; "%%~dpnxF.temp"
      type "%%~dpnxF"&gt;&gt;"%%~dpnxF.temp"
      del "%%~dpnxF"
      echo "%%~dpnxF.temp" "%%~nxF"
      ren "%%~dpnxF.temp" "%%~nxF"

      )</pre>
<p>The batch file above works great for small text files in the directory, however, it takes a long time to read the large text files in the directory. </p>
<p>As you stated in your blog, using the code</p>
<p>findstr /R /N &#034;^&#034; file.txt</p>
<p>Does not work well for counting the lines in large text files. So, i added the following code:</p>
<p>findstr /R /N &#034;^&#034; file.txt | find /C &#034;:&#034;</p>
<p>from your blog into the batch file, and now it looks like this:</p>
<pre class="brush: bash;">@echo off
setlocal enabledelayedexpansion
cd /d "C:\Users\John\Desktop\Testing\AllFiles"
for /f "delims=" %%F in ('dir /b *.txt') do (
   for /f "delims=:" %%N in ('findstr /N /R "^" "%%~dpnxF" | find /C ":"') do set lines=%%N

      echo There are !lines! lines in this file &gt; "%%~dpnxF.temp"
      type "%%~dpnxF"&gt;&gt;"%%~dpnxF.temp"
      del "%%~dpnxF"
      echo "%%~dpnxF.temp" "%%~nxF"
      ren "%%~dpnxF.temp" "%%~nxF"

      )</pre>
<p>But, when i run the modified code, i get the following error:</p>
<p>&#034;: was unexpected at this time&#034;</p>
<p>Do you know what i am doing wrong?</p>
<p>Thanks in advance :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: FractalSpace</title>
		<link>http://brett.batie.com/scripting/count-number-of-lines-in-a-file-using-dos/comment-page-1/#comment-15339</link>
		<dc:creator>FractalSpace</dc:creator>
		<pubDate>Wed, 06 Apr 2011 22:33:46 +0000</pubDate>
		<guid isPermaLink="false">http://brett.batie.com/?p=25#comment-15339</guid>
		<description>Nice though, but Google brought me here on a different search. How to display current line number in a batch file (for debugging) ?</description>
		<content:encoded><![CDATA[<p>Nice though, but Google brought me here on a different search. How to display current line number in a batch file (for debugging) ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mark</title>
		<link>http://brett.batie.com/scripting/count-number-of-lines-in-a-file-using-dos/comment-page-1/#comment-14874</link>
		<dc:creator>mark</dc:creator>
		<pubDate>Thu, 24 Mar 2011 17:17:35 +0000</pubDate>
		<guid isPermaLink="false">http://brett.batie.com/?p=25#comment-14874</guid>
		<description>awesome, helped on my uni tutorial, tears since I used dos as mature student
top stuff =)</description>
		<content:encoded><![CDATA[<p>awesome, helped on my uni tutorial, tears since I used dos as mature student<br />
top stuff =)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tom</title>
		<link>http://brett.batie.com/scripting/count-number-of-lines-in-a-file-using-dos/comment-page-1/#comment-14584</link>
		<dc:creator>Tom</dc:creator>
		<pubDate>Wed, 16 Mar 2011 16:47:24 +0000</pubDate>
		<guid isPermaLink="false">http://brett.batie.com/?p=25#comment-14584</guid>
		<description>Here&#039;s another example that i found useful:

type *.txt &#124; findstr /R /N &quot;^&quot; &#124; find /C &quot;:&quot;

good for use to count total lines of all the files.</description>
		<content:encoded><![CDATA[<p>Here&#039;s another example that i found useful:</p>
<p>type *.txt | findstr /R /N &#034;^&#034; | find /C &#034;:&#034;</p>
<p>good for use to count total lines of all the files.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Faraz</title>
		<link>http://brett.batie.com/scripting/count-number-of-lines-in-a-file-using-dos/comment-page-1/#comment-14223</link>
		<dc:creator>Faraz</dc:creator>
		<pubDate>Mon, 07 Mar 2011 10:38:41 +0000</pubDate>
		<guid isPermaLink="false">http://brett.batie.com/?p=25#comment-14223</guid>
		<description>Hi Brett,

This below dos code when I run in batch file it Rename all files in Numeric sequence like 1.jpg, 2.jpg, 3.jpg, 4.jpg....... one problem with this code. suppose we have 99 files, When it rename the last file 99.jpg  then at the end it again rename 9.jpg to last file like 100.jpg do u have a clue to fix it so it don&#039;t do that or have better dos batch file code to do this task.
===================================================

@echo off
set /a cnt=1
for %%a in (*.jpg) do call :PROCESS &quot;%%a&quot;
goto :EOF
:PROCESS
ren %1 %cnt%.jpg
set /a cnt+=1

===================================================

Thanks</description>
		<content:encoded><![CDATA[<p>Hi Brett,</p>
<p>This below dos code when I run in batch file it Rename all files in Numeric sequence like 1.jpg, 2.jpg, 3.jpg, 4.jpg&#8230;&#8230;. one problem with this code. suppose we have 99 files, When it rename the last file 99.jpg  then at the end it again rename 9.jpg to last file like 100.jpg do u have a clue to fix it so it don&#039;t do that or have better dos batch file code to do this task.<br />
===================================================</p>
<p>@echo off<br />
set /a cnt=1<br />
for %%a in (*.jpg) do call :PROCESS &#034;%%a&#034;<br />
goto :EOF<br />
:PROCESS<br />
ren %1 %cnt%.jpg<br />
set /a cnt+=1</p>
<p>===================================================</p>
<p>Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nogs</title>
		<link>http://brett.batie.com/scripting/count-number-of-lines-in-a-file-using-dos/comment-page-1/#comment-13119</link>
		<dc:creator>Nogs</dc:creator>
		<pubDate>Thu, 03 Feb 2011 08:42:36 +0000</pubDate>
		<guid isPermaLink="false">http://brett.batie.com/?p=25#comment-13119</guid>
		<description>sorry, I read more carefully and this post already have what I needed.
thanks it helped a lot!</description>
		<content:encoded><![CDATA[<p>sorry, I read more carefully and this post already have what I needed.<br />
thanks it helped a lot!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nogs</title>
		<link>http://brett.batie.com/scripting/count-number-of-lines-in-a-file-using-dos/comment-page-1/#comment-13118</link>
		<dc:creator>Nogs</dc:creator>
		<pubDate>Thu, 03 Feb 2011 08:32:21 +0000</pubDate>
		<guid isPermaLink="false">http://brett.batie.com/?p=25#comment-13118</guid>
		<description>Hi,
thanks a lot for your post, I still have a question:
I want to use this line and store the count result in a variable, can anyone help?

Thanks.</description>
		<content:encoded><![CDATA[<p>Hi,<br />
thanks a lot for your post, I still have a question:<br />
I want to use this line and store the count result in a variable, can anyone help?</p>
<p>Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Moesjamarra</title>
		<link>http://brett.batie.com/scripting/count-number-of-lines-in-a-file-using-dos/comment-page-1/#comment-13073</link>
		<dc:creator>Moesjamarra</dc:creator>
		<pubDate>Wed, 02 Feb 2011 12:54:21 +0000</pubDate>
		<guid isPermaLink="false">http://brett.batie.com/?p=25#comment-13073</guid>
		<description>Works perfect! Thx a lot!</description>
		<content:encoded><![CDATA[<p>Works perfect! Thx a lot!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Faraz</title>
		<link>http://brett.batie.com/scripting/count-number-of-lines-in-a-file-using-dos/comment-page-1/#comment-12228</link>
		<dc:creator>Faraz</dc:creator>
		<pubDate>Wed, 05 Jan 2011 05:00:52 +0000</pubDate>
		<guid isPermaLink="false">http://brett.batie.com/?p=25#comment-12228</guid>
		<description>Brett: Brilliant!!! It worked Great.  Thank you very much, God bless you &amp; your family.</description>
		<content:encoded><![CDATA[<p>Brett: Brilliant!!! It worked Great.  Thank you very much, God bless you &amp; your family.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

