count character before first space

January 4, 2010 at 01:10:24
Specs: Windows XP
I want to count the number of character before a first space occured in each line of a text file using batch.
As for example, i have my content in a file as
0001A 123 ert 234
00012A 345 fght 346
Now i want to count characters before first space occur such that in line1 i must get the count 5. similarly with lin2 i must get the count 6.

Is there any way?

thanky you

See More: count character before first space

Report •

January 4, 2010 at 02:19:02
you can use vbscript

Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\file.txt"
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfLine
	strLine = objFile.ReadLine
        s = Split(strLine," ")
	WScript.Echo len(s(0))

or if you can download stuff, use gawk for windows(see my sig)

c:\test> gawk.exe "{print length($1)}" file

GNU win32 packages | Gawk

Report •

January 4, 2010 at 02:24:56
Is the count needed to get rid of these whitespaces, or is there another purpose ?

Report •

January 4, 2010 at 02:25:27
@echo off & setLocal EnableDELAYedeXpansion

set N=

for /f "tokens=1 delims= " %%a in (myfile) do (
  set /a N+=1
  set C=
  set S=%%a
  call :sub1
  echo line !N! first string has !C! chars

goto :eof

  set /a C+=1
  if '!S:~1^,99!' neq '' (
    set S=!S:~1,99!
    goto :sub1

goto :eof

Helping others achieve escape felicity


Report •
Related Solutions

Ask Question