Home Count total words from text file serperated by "," (comma) in batch script
Reply: 2

Count total words from text file serperated by "," (comma) in batch script

Kaushal Dave
Kaushal Dave Published in 2017-09-12 13:02:44Z

I have a comma d eliminated text file (names.txt) in which there is a string for ex Mike,Joe,Kelly.

I need to get total count of strings like : total name : 3 and store this result in a variable and call it further in my batch script. I googled a lot but not getting proper code.

It would be really great if I can get an answer from this expert group. Thanks a lot in advance.

Quanmao Reply to 2017-09-12 15:24:49Z

A simple way, just 1 command line: ( name count is just the return value %ERRORLEVEL% )

msr -p your-file.txt -t "," -o "\n" -PAC | msr -t "\w+"

Store to a variable:

  • Name count ? set /a NameCount=%ERRORLEVEL%

  • Full string ? set "ResultText=Total name : %ERRORLEVEL%"

Additionally, if you want to hide output of the command, redirect output to nothing by >nul :

msr -p your-file.txt -t "," -o "\n" -PAC | msr -t "\w+" >nul

msr.exe / msr-Win32.exe is a single portable exe tool for file processing with cross platform versions. see tools directory in my open project https://github.com/qualiu/msr . You can also see: https://qualiu.github.io/msr/usage-by-running/msr-Windows.html

Stephan Reply to 2017-09-12 15:38:59Z

When I understand correctly, your textfile has only one line.

@echo off
<t.txt set /p line=
set a=0
for %%a in (%line%) do set /a a+=1
echo %a%
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.303589 second(s) , Gzip On .

© 2016 Powered by cudou.com design MATCHINFO