1.

Solve : How to Count a character in a long string?

Answer»

Hi,

I have an text file seperated by tilde "~". I want to COUNT how many Tilde's are in the File and store in the variable. That file is having only ONE large string as only seperator between the lines is "~". If I use COMP or Findstr commands, it gives me number of lines and that comes out to be 1 or it returns me the complete file itself.

I want to use the batch file to return me the count.

Please guide.

THANKS
BharatNot sure you can do this in batch, but sometimes you have to think outside the BOX:

Code: [Select]Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("[highlight]filenamegoeshere[/highlight]", ForReading)

strText = objFile.ReadAll
objFile.Close

arrTilde = Split(strText, "~")
Wscript.Echo Ubound(arrTilde)

After saving the script with a vbs extension, run as cscript scriptname.vbs

The irony is that the solution is in the box. Thanks for your reply. I will try this method as doing with dos commands look tough at present.

thanks
bharatIf you have Python :

Code: [Select]data = open("your_file_with_tilde.txt").read()
print data.count("~")



Discussion

No Comment Found