VBScript to read specific line and extract charact

2019-01-15 11:13发布

I have a VB script that reads the 11th line from a text file. However from that line I need to extract characters 48 through 53 and save it as a variable. After this is accomplished I would like to use that variable and use it in a web url. Example below:

Contents of the szCPUSer.dat file look like this: szCPUSer.dat

The script I have reads the 10th line

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("szCPUSer.dat", ForReading)

For i = 1 to 10
    objTextFile.ReadLine
Next

strLine = objTextFile.ReadLine
Wscript.Echo strLine

objTextFile.Close

I need the script to extract 03187 from the 11th line than store it as a variable SerNum. after this I would like to use that number extracted in a url for example:

http://seriallookup.com/serial=SerNum

2条回答
\"骚年 ilove
2楼-- · 2019-01-15 11:39

The following works!

Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("szCPUSer.dat", ForReading)
For i = 1 to 10
    objTextFile.ReadLine
Next
strLine = objTextFile.ReadLine
Wscript.Echo strLine
objTextFile.Close

'Gets 6 chars starting from Right side
SerNum = Right(strLine, 6)
'Gets 6 chars starting from Left side
SerNum = Left(SerNum, 5)
'Wscript.Echo SerNum
url = "http://seriallookup.com/serial=" & SerNum
Wscript.Echo url
查看更多
▲ chillily
3楼-- · 2019-01-15 11:51

Take a look at InStr function. It lets you search for a substring.

http://www.w3schools.com/vbscript/func_instr.asp

Then you can use the Right function to parse out the end bit of the line.

You can also look at the Split function so you can parse the lines into arrays and deal with it that way which would be best.

http://www.w3schools.com/vbscript/func_split.asp

查看更多
登录 后发表回答