0

I want to count all the characters of a certain text. However in my code it only counts single string. But the requirement needs to count all characters including whites spaces and new line.. For example:

Hello World!
How are you today?
Hope you are okay

How am i going to do that in my code?Thanks. My code:

@ECHO OFF

for %%i in (y.txt) do @set count=%%~zi
REM Set "string" variable
SET string=(Hello World
            How are you today?
            Im fine..) // i want to read these string because my code only read single string
REM Set the value of temporary variable to the value of "string" variable
SET temp_str=%string%
REM Initialize counter
SET str_len=0

:loop
if defined temp_str (
REM Remove the first character from the temporary string variable and increment 
REM counter by 1. Countinue to loop until the value of temp_str is empty string.
SET temp_str=%temp_str:~1%
SET /A str_len += 1
GOTO loop
)

REM Echo the actual string value and its length.
ECHO %string% is %str_len% characters long!
Jay Gorio
  • 335
  • 2
  • 4
  • 22
  • Where does the string come from? Is it a text file? From the clipboard? From a web page? And how are the [tag:unix] and [tag:functional-programming] tags applicable to this problem? – rojo Mar 17 '15 at 16:08
  • @rojo thanks it come from the same batch file with my code at the top of it..Yeah a text file. – Jay Gorio Mar 17 '15 at 16:10
  • So you want to count characters including line breaks that occur after the final `exit /b` or `goto :EOF` of `%0`? By the way, use `exit /b` instead of `exit` to prevent your cmd console from closing if you run the script from the console. – rojo Mar 17 '15 at 16:19
  • 1
    If you can't keep your question straight and be specific you'll never get a worthwhile answer. What does "type a text in a notepad" have to do with "come from the same batch file with my code at the top of it"? You mean you'll edit your batch script every time you want a character count? If you're going to go through that kind of trouble, you might as well just put your text into Notepad++, select all, and look at the status bar for your character count (which does include CR and LF in the count). – rojo Mar 17 '15 at 16:36
  • @rojo okay i updated my code kindly check also the comment on the code thanks again.. – Jay Gorio Mar 17 '15 at 16:51
  • [This answer](http://stackoverflow.com/a/28992360/1683264) demonstrates how to include line breaks in batch script variable values. What it looks like you're attempting to do is create a [heredoc or nowdoc](http://stackoverflow.com/a/11153164/1683264), but batch scripting doesn't support such things. You'd be better off using Perl or PHP. Or PowerShell supports [something similar](http://tasteofpowershell.blogspot.com/2008/07/here-docs-in-powershell.html). – rojo Mar 17 '15 at 17:08
  • wait - you want to count all characters including whitespaces and `cr`, `lf` of a file? `for %%i in (y.txt) do @set count=%%~zi` – Stephan Mar 17 '15 at 17:14
  • @rojo i think it can support because someone already done that before in our class last year using batch file..Yes it will count all of what u have mentioned.Can you give sample code please I really need to finish this.. Thanks again. – Jay Gorio Mar 17 '15 at 17:21
  • Please help me on this Iam really having trouble.. – Jay Gorio Mar 17 '15 at 18:21
  • @stephan can u please give me sample code. Im having problem placing it – Jay Gorio Mar 17 '15 at 19:14
  • no placing at all. It's a one-liner: `for %%i in (y.txt) do @set count=%%~zi` (or directly on commandline: `for %i in (y.txt) do @set count=%~zi)` - of course followed by `echo %count%` – Stephan Mar 17 '15 at 19:51
  • Sorry but this may a stupid question. What will i do with the variable string?i will just leave it that way?..i updated the code is that what u are saying?please give me sample thnaks – Jay Gorio Mar 17 '15 at 19:59

2 Answers2

2

this is all, you need:

@ECHO OFF
set /p "file=enter filename: "
for %%i in (%file%) do @set count=%%~zi
echo this file has %count% characters including whitespaces and special chars like line-feed/carriage-return etc.
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • its not working..Also the requirement will not need user input it will just read the text file.. Like the code above it supposedly count all the text hello world until iam fine.. – Jay Gorio Mar 17 '15 at 20:17
  • oh sorry its working hehe thanks so much..But theres an extra 2 characters that has been counted. – Jay Gorio Mar 18 '15 at 02:00
  • it's exactly the count of all characters in a plain text file. Keep in mind, that "newLine" is indeed two characters ("CariageReturn"+"LineFeed") in windows-format (a text file in Unix-format has only "LineFeed"). If you `echo.>x.txt`, x.txt contains exactly two characters: CR and LF (making a "linefeed" together) – Stephan Mar 18 '15 at 06:40
0

With this function you can calculate the line number, the number of words and the number of characters in a text file

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "MOD=%1"
SET "ARC=%2"
IF %MOD%==/L GOTO :CONTLIN
IF %MOD%==/P GOTO :CONTPAL
IF %MOD%==/C GOTO :CONTCAR
:CONTLIN
FOR /f "TOKENS=*" %%z IN (%ARC%) DO SET /a LINEAS+=1
ECHO %LINEAS%
EXIT /b 0
:CONTPAL
FOR /f "TOKENS=*" %%x IN (%ARC%) DO FOR %%y IN (%%x) DO SET /a PALABRAS+=1
ECHO %PALABRAS%
EXIT /b 0
:CONTCAR
SETLOCAL
FOR /f "TOKENS=*" %%a IN (%ARC%) DO (FOR %%b IN (%%a) DO (FOR %%a IN (%%b) DO (SET 
A=%%a)&(CALL :CC !A!)))
ECHO %CARACTERES%
EXIT /b 0
:CC
SET B=!A!
:I
SET /a CARACTERES+=1
SET B=%B:~1%
IF DEFINED B GOTO :I
EXIT /b 0

And I could access it from a file like this:

@ECHO OFF
SET ARCHIVO=TEXTO.TXT
FOR /F %%a IN ('CONTTEXT /L %ARCHIVO%') DO SET /a LINEAS=%%a
FOR /F %%a IN ('CONTTEXT /P %ARCHIVO%') DO SET /a PALABRAS=%%a
FOR /F %%a IN ('CONTTEXT /C %ARCHIVO%') DO SET /a CARACTERES=%%a
ECHO %LINEAS% LINEAS
ECHO %PALABRAS% PALABRAS
ECHO %CARACTERES% CARACTERES
PAUSE
julio
  • 1