Computing.Net > Forums > Programming > VBScript - String bytes together

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

VBScript - String bytes together

Reply to Message Icon

Name: skisalomon77
Date: July 11, 2008 at 08:22:35 Pacific
OS: XP Pro
CPU/Ram: Intel/2GB
Product: Dell
Comment:

I'm reading a file and searching for data in specific bytes, then stringing all that data together and exporting it to a separate text file. I'm not receiving any errors in my script, so I'm not sure what I'm doing wrong.

I use VBScript every once-in-a-while for certain tasks, but I'm still a beginner. Any help will be appreciated.

There are really only 28 lines in my script.

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\ArchivedSANEW\SAcombine.txt", ForReading)

strContents = objFile.ReadAll
objFile.Close

Dim StoreNumber
Dim TransNumber
Dim TransDate
Dim TransCancel
Dim CoupCode
Dim PostVoidTrans

StoreNumber = Mid("001", 6, 3)
TransNumber = Mid("", 29, 6)
TransDate = Mid("", 35, 8)
TransCancel = Mid("", 52, 1)
CoupCode = Mid("", 176, 5)
PostVoidTrans = Mid("", 163, 3)
OriginalTransNumber = Mid("", 166, 6)

strNewFile = strNewFile & StoreNumber & TransNumber & TransDate & TransCancel & CouponCode & PostVoidTrans & OriginalTransNumber & vbCrLf

Set objFile = objFSO.CreateTextFile("C:\ArchivedSANEW\SAtest.txt")
objFile.Write strNewFile
objFile.Close



Sponsored Link
Ads by Google

Response Number 1
Name: StuartS
Date: July 11, 2008 at 13:08:10 Pacific
Reply:

I dont know what you are trying to do but it's never going to work.

StoreNumber = Mid("001", 6, 3) returns three characters starting at the six character from the beginning of the string. As there are only three characters in the string you are asking the impossible.

TransDate = Mid("", 35, 8) here you are asking for eight characters from a zero length string starting at character 35. Not possible. Every one of the Mid statements will return a zero length string.

You read in the contents of SAcombine text into strContents but nowhere to you use that string. You are just creating a new file which is a series of zero length strings.

Stuart


0

Response Number 2
Name: skisalomon77
Date: July 11, 2008 at 13:45:51 Pacific
Reply:

Thanks Stuart! I kind of thought that was why I was pull no date into my file.

The 001 was a typo. Is there a way for me to pull data between certain bytes, no matter what that data is?

For example, pull everything between bytes 6, 7 and 8, no matter if those 6, 7 and 8 bytes say 123, or ABC, or XYZ?


0

Response Number 3
Name: StuartS
Date: July 11, 2008 at 13:52:34 Pacific
Reply:

If you are trying to extract data from the file you have just read in use:

StoreNumber = Mid(strContents, 6, 3)

Stuart


0

Response Number 4
Name: skisalomon77
Date: July 11, 2008 at 16:43:31 Pacific
Reply:

As you expected, that worked...thank you! I modified the proper lines and the script is now extracting the correct bytes. The only problem is that the script is only pulling the first line from the SAcombine.txt file even though I am reading the entire file. Any ideas?

Const ForReading = 1

Dim StoreNumber
Dim TransNumber
Dim TransDate
Dim TransCancel
Dim CoupCode
Dim PostVoidTrans

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\ArchivedSANEW\SAcombine.txt", ForReading)

strContents = objFile.ReadAll
objFile.Close

StoreNumber = Mid(strContents, 6, 3)
TransNumber = Mid(strContents, 29, 6)
TransDate = Mid(strContents, 35, 8)
TransCancel = Mid(strContents, 52, 1)
PostVoidTrans = Mid(strLine, 163) = "077"
OriginalTransNumber = Mid(strContents, 166, 6)
CoupCode = Mid(strContents, 176, 5)

strNewFile = strNewFile & StoreNumber & TransNumber & TransDate & TransCancel & PostVoidTrans & OriginalTransNumber & CoupCode

Set objFile = objFSO.CreateTextFile("C:\ArchivedSANEW\SAtest.txt")
objFile.Write strNewFile
objFile.Close


0

Response Number 5
Name: StuartS
Date: July 12, 2008 at 07:24:07 Pacific
Reply:

Are you absolutley sure you are reading the entire file? Without seeing the original file it is difficult to say and I am not that familiar with the VBscript Readall. If it is expectging to read a text file and the lines are terminsted with a zero that will casue it to stop after the first line. There are other reasons as well. You need to examine the original file and read up on excatly what ReadAll does.

Stuart


0

Related Posts

See More



Response Number 6
Name: Razor2.3
Date: July 12, 2008 at 16:15:26 Pacific
Reply:

No, objFile.ReadAll does exactly what it sounds like. But he wants to do so on a line-by-line basis. He really should be using objFile.ReadLine and Do loops. I'd do the rewrite, but I don't have the time.

On the bright side, there shouldn't be any shortage of example VBScript code from me. Then again, I've admitted my code isn't the easiest to read. Or, at least, it seems to require more knowledge of basic VBScript syntax than most people want to learn.


0

Response Number 7
Name: Razor2.3
Date: July 13, 2008 at 01:05:38 Pacific
Reply:

I did the rewrite, but it's untested. Should work; I only took skisalomon77's code and wrapped up in a Do Until loop.

Option Explicit
Dim StoreNumber, TransNumber, TransDate, TransCancel
Dim CoupCode, PostVoidTrans, objFSO, objFile
'PROTIP: If you're not going to Dim, fine. It's your code.
' If you're going to Dim, great. Use Option Explicit.
' Whatever you do, DON'T Dim only SOME variables.

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("C:\ArchivedSANEW\SAtest.txt")

With objFSO.OpenTextFile("C:\ArchivedSANEW\SAcombine.txt", 1)
Do Until .AtEndOfStream
strContents = .ReadLine

StoreNumber = Mid(strContents, 6, 3)
TransNumber = Mid(strContents, 29, 6)
TransDate = Mid(strContents, 35, 8)
TransCancel = Mid(strContents, 52, 1)
PostVoidTrans = (Mid(strLine, 163) = "077")
OriginalTransNumber = Mid(strContents, 166, 6)
CoupCode = Mid(strContents, 176, 5)

strNewFile = strNewFile & StoreNumber & TransNumber & _
TransDate & TransCancel & PostVoidTrans & _
OriginalTransNumber & CoupCode
objFile.WriteLine strNewFile
Loop
End With



0

Response Number 8
Name: StuartS
Date: July 13, 2008 at 03:13:45 Pacific
Reply:

PostVoidTrans = Mid(strLine, 163) = "077"

That line look a bit odd. Where does strLine come from. However, regardless of what strLine is, PostVoidTans will always be "077".

Stuart


0

Response Number 9
Name: Razor2.3
Date: July 13, 2008 at 04:29:33 Pacific
Reply:

Ah, you see, in VB = pulls double duty as both assignment and equality. PostVoidTrans is just a Boolean. Normally, I'd use parentheses for clarity. In fact, I think I will. Excuse me while I edit my post...

And I have no idea where strLine comes from. He'll have to fix that himself. Thanks to Option Explicit, the script will refuse to run until he either Dims the variable, or he gets rid of it.


0

Response Number 10
Name: skisalomon77
Date: July 16, 2008 at 06:42:10 Pacific
Reply:

Thanks for both of your help! I have been offline for few days.

Razor,

I removed my strLine and changed it back to strContents. I also DIM'd a few more variables because I was popping variable errors. The script runs without error now, but the 900kb file that I'm extracting data from turns into a 304MB file. It seems to be continuously stringing data together.

Option Explicit

Dim StoreNumber, TransNumber, TransDate, TransCancel
Dim CoupCode, PostVoidTrans, objFSO, objFile, strContents, OriginalTransNumber, strNewFile

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("C:\ArchivedSANEW\SAtest.txt")
With objFSO.OpenTextFile("C:\ArchivedSANEW\SAcombine.txt", 1)
Do Until .AtEndOfStream
strContents = .ReadLine
StoreNumber = Mid(strContents, 6, 3)
TransNumber = Mid(strContents, 29, 6)
TransDate = Mid(strContents, 35, 8)
TransCancel = Mid(strContents, 52, 1)
PostVoidTrans = Mid(strContents, 163,3)
OriginalTransNumber = Mid(strContents, 166, 6)
CoupCode = Mid(strContents, 176, 5)

strNewFile = strNewFile & StoreNumber & TransNumber & _
TransDate & TransCancel & PostVoidTrans & _
OriginalTransNumber & CoupCode
objFile.WriteLine strNewFile
Loop
End With


0

Response Number 11
Name: Razor2.3
Date: July 16, 2008 at 07:21:45 Pacific
Reply:

I did say I didn't touch the code, aside from wrapping it in a loop. Still, I should have caught that. Sorry 'bout that.

Replace everything after strNewFile (including said line) with this:

    objFile.WriteLine StoreNumber & TransNumber & _
TransDate & TransCancel & PostVoidTrans & _
OriginalTransNumber & CoupCode

Loop
End With

EDIT: I've edited this message twice, and every single time it keeps adding strNewFile to the end of the line. If it's still there, it shouldn't be, and I give up.


0

Response Number 12
Name: skisalomon77
Date: July 17, 2008 at 14:02:06 Pacific
Reply:

Thank you very much Razor!

I'm pretty sure you got it working for me. I just had to remove strNewFile after CoupCode because the script was expecting and end of statement. After I removed that, the script ran fine and the extract seems to have everything I need.

Thanks again!


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: VBScript - String bytes together

VBS - String Bytes Together Part II www.computing.net/answers/programming/vbs-string-bytes-together-part-ii/16707.html

Razor...I figured out my VBScript www.computing.net/answers/programming/razori-figured-out-my-vbscript/16752.html

WSH VBScript String Manipulation www.computing.net/answers/programming/wsh-vbscript-string-manipulation/17017.html