Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: How to Count a character in a long string  (Read 3375 times)

0 Members and 1 Guest are viewing this topic.

inds7

  • Guest
How to Count a character in a long string
« on: December 08, 2006, 03:55:41 PM »
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
Bharat

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: How to Count a character in a long string
« Reply #1 on: December 08, 2006, 06:00:42 PM »
Not 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. ;D
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

inds7

  • Guest
Re: How to Count a character in a long string
« Reply #2 on: December 08, 2006, 06:12:06 PM »
Thanks for your reply. I will try this method as doing with dos commands look tough at present.

thanks
bharat

ghostdog74



    Specialist

    Thanked: 27
    Re: How to Count a character in a long string
    « Reply #3 on: December 08, 2006, 11:26:43 PM »
    If you have Python :

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