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!
12
  • Where does the string come from? Is it a text file? From the clipboard? From a web page? And how are the unix and functional-programming tags applicable to this problem? Commented Mar 17, 2015 at 16:08
  • @rojo thanks it come from the same batch file with my code at the top of it..Yeah a text file. Commented Mar 17, 2015 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. Commented Mar 17, 2015 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). Commented Mar 17, 2015 at 16:36
  • @rojo okay i updated my code kindly check also the comment on the code thanks again.. Commented Mar 17, 2015 at 16:51

2 Answers 2

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.
Sign up to request clarification or add additional context in comments.

3 Comments

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..
oh sorry its working hehe thanks so much..But theres an extra 2 characters that has been counted.
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)
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.