String Parsing

Microsoft Microsoft xp professional
August 3, 2009 at 04:25:22
Specs: Windows XP
Hi ,

I have a string as WindowsXP-KB959542-Today.exe which need to be parsed to get only KB959542 . Can any one please help me in parsing this string in a batch file..Its Urgent plz


See More: String Parsing

Report •


#1
August 3, 2009 at 04:41:42
for /f "tokens=2 delims=-" %%a in ("WindowsXP-KB959542-Today.exe") do echo %%a


Report •

#2
August 3, 2009 at 04:47:58
Thanks a bunch...

Can you suggest me what i need to change in the script.. if such strings( WindowsXP-KB959542-Today.exe) are there in a .txt file in each line...

Thanks...


Report •

#3
August 3, 2009 at 05:47:08
vbscript
Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\file.txt"
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfStream
	strLine = objFile.ReadLine
	s = Split(strLine,"-")
	WScript.Echo s(1)
Loop
objFile.Close

save as myscript.vbs and on command line

c:\test> cscript /nologo myscript.vbs

GNU win32 packages | Gawk


Report •

Related Solutions

#4
August 3, 2009 at 05:53:55
For the batch version:

for /f "usebackq tokens=2 delims=-" %%a in ("your text file") do echo %%a


Report •

#5
August 3, 2009 at 22:43:05
Thanks!!

Can you please also help me in storing the for loop value each time in a variable ..


Report •

#6
August 4, 2009 at 02:47:02
"Can you please also help me in storing the for loop value each time in a variable .."

Perhaps this is what your after:

setlocal enabledelayedexpansion
for /f "usebackq tokens=2 delims=-" %%a in ("your text file") do (
    set /a cnt+=1
    set var!cnt!=%%a
)


Report •

#7
August 10, 2009 at 03:50:59
Thanks for your timely help!!

I need one more suggestion in following problem..plz help me out!!

There are two text files with five lines each. I want to read both text files and have first line from one file and first line from second test file ..do some operation...then again second line from one file and second line form second file. and do operattion...so on uptill 5 lines...


Please help me in this....as i am new to batch file creation...

Thanks in advance..


Report •

#8
August 10, 2009 at 23:53:23
Please give me answer for this....

Report •

#9
August 11, 2009 at 00:43:03
Reading multiple files concurrently is problematic in batch files, it is possible but takes a lot of mucking around. Blank lines can also be a problem..

Here's a quick job, it's quite inefficient(and a bit of an abuse of find and findstr..) but seems to work......

for /f %%a in ('type "file1"^|find /c /v ""') do set /a size=%%a
for /l %%a in (1,1, %size%) do (
	for /f "tokens=2 delims=-" %%b in ('type "file1"^|find /n /v "" ^|findstr /b /l "[%%a]"') do (
		for /f "tokens=2 delims=-" %%c in ('type "file2"^|find /n /v "" ^|findstr /b /l "[%%a]"') do (
			echo %%b --- %%c
		)
	)
)

This code Assumes:
1. That both files have the same number of lines.
2. Both files contain the same type of data.
3. That there is no blank lines.


On a side note, you should show a little more initiative than just ask for someone to code it for you, the net is jam pack with examples and reference material.....


Report •

#10
August 11, 2009 at 02:33:59
Thanks for suggestion....But I am really new to the batch scripting....

Anyways..I have checked for my data..it is not working as it has data in each line and two text files are seperate

Atlast help me with one thing...can we delete a line in a text file thru for loop..i searched..but no where i found the help...

Hoping you will help me...Judago!!


Report •

#11
August 11, 2009 at 05:50:17
Anyways..I have checked for my data..it is not working as it has data in each line and two text files are seperate

Atlast help me with one thing...can we delete a line in a text file thru for loop..i searched..but no where i found the help...

The Code I posted in #9 does run over two files("file1" and "file2"), albeit inefficiently and in very limited scope.

To delete a line you have to run a for loop over the file, exclude the unwanted line, output to a new file the delete the old file and rename the new file or move the new file over the top of it.

This should work to exclude a line by line number(blank lines will cause an issue).

set cnt=
set exclude=8
for /f "usebackq delims=" %%a in ("your file") do (
    set /a line+=1
    for /f %%b in ('call echo %%line%%') do (
        if not %%b==%exclude% (
            >> newfile echo %%a
        )
    )
)
move "%cd%\newfile" "%cd%\yourfile"


Report •

#12
August 11, 2009 at 19:05:32
the alternative way to exclude a line is using find or findstr with /v option.

GNU win32 packages | Gawk


Report •


Ask Question