Computing.Net > Forums > Programming > VBS - String Bytes Together Part II

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.

VBS - String Bytes Together Part II

Reply to Message Icon

Name: skisalomon77
Date: July 19, 2008 at 21:09:11 Pacific
OS: XP Pro
CPU/Ram: Intel/2GB
Product: Dell
Comment:

This is an addition to a post I made last week. My original script was reading a file, searching for specific bytes, then stringing them together in a new file. Stuart got me started and Razor helped me out considerably.

Anyway, I decided to put a new twist on the script to cut out some steps I was previously taking to get the results I need. Now I'm using 4 IF THEN statements. All of which are reading the first 3 bytes of each line. If the first 3 bytes = a certain value, then the statements within the IF block will be executed. The end of the script will then string all the statements together into a new file.

Without the IF THEN statements, the script works fine. Once I add the IF THEN statements, the script runs without error, but my file contains 44,000 blank lines. It's a completely blank file.

I've been working on/researching this here and there for a couple of days, but am still getting the same results. So here I am once again.

Option Explicit

Dim strContents, TransHeader, StoreNumber, TransNumber, TransDate, TransCancel, TransType, PostVoid, OriginalTransNumber
Dim Discount, DiscountCode, CoupCode, AmountBeforeDiscount, DiscountAmount, AmountAfterDiscount, AddInput, objFSO, objFile, strNewFile

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("C:\ArchivedSANEW\SAfilter.txt")
With objFSO.OpenTextFile("C:\ArchivedSANEW\SAextract.txt", 1)
Do Until .AtEndOfStream
strContents = .ReadLine

If TransHeader = Mid(strContents, 3, 3) = "000" Then
StoreNumber = Mid(strContents, 6, 3)
TransNumber = Mid(strContents, 29, 6)
TransDate = Mid(strContents, 35, 8)
TransCancel = Mid(strContents, 52, 1)
TransType = Mid(strContents, 47, 3)
End If

If PostVoid = Mid(strContents, 3, 3) = "077" Then
OriginalTransNumber = Mid(strContents, 4, 6)
End If

If Discount = Mid(strContents, 3, 3) = "021" Then
DiscountCode = Mid(strContents, 4, 8)
AmountBeforeDiscount = Mid(strContents, 17, 5)
DiscountAmount = Mid(strContents, 27, 5)
AmountAfterDiscount = Mid(strContents, 37, 5)
End If

If AddInput = Mid(strContents, 3, 3) = "028" Then
CoupCode = Mid(strContents, 14, 6)
End If

strNewFile = strNewFile & StoreNumber & TransNumber & _
TransDate & TransCancel & TransType & _
DiscountCode & CoupCode & AmountBeforeDiscount & DiscountAmount & AmountAfterDiscount & OriginalTransNumber

objFile.WriteLine StoreNumber & TransNumber & _
TransDate & TransCancel & TransType & _
DiscountCode & CoupCode & AmountBeforeDiscount & DiscountAmount & AmountAfterDiscount & OriginalTransNumber

Loop
End With



Sponsored Link
Ads by Google

Response Number 1
Name: skisalomon77
Date: July 21, 2008 at 17:44:06 Pacific
Reply:

I'm getting closer. My IF THEN statements are now working because I removed the variables from the IF lines, but the script isn't stringing the data together correctly. Once the second IF THEN statement locates the data, the strNewFile will write this data to every line, even though it doesn't belong there, until one of the IF THEN statement finds a new piece of data. Basically, I'm now pulling the correct data, but the script is stringing the data incorrectly.

Option Explicit

Dim strContents, StoreNumber, TransNumber, TransDate, TransCancel, TransType, OriginalTransNumber
Dim DiscountCode, CoupCode, AmountBeforeDiscount, DiscountAmount, AmountAfterDiscount, objFSO, objFile, strNewFile

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("C:\ArchivedSANEW\SAfilter.txt")
With objFSO.OpenTextFile("C:\ArchivedSANEW\SAextract.txt", 1)
Do Until .AtEndOfStream
strContents = .ReadLine

If Mid(strContents, 1, 3) = "000" Then
StoreNumber = Mid(strContents, 6, 3)
TransNumber = Mid(strContents, 29, 6)
TransDate = Mid(strContents, 35, 8)
TransCancel = Mid(strContents, 52, 1)
TransType = Mid(strContents, 47, 3)
End If

If Mid(strContents, 1, 3) = "021" Then
DiscountCode = Mid(strContents, 4, 8)
AmountBeforeDiscount = Mid(strContents, 17, 5)
DiscountAmount = Mid(strContents, 27, 5)
AmountAfterDiscount = Mid(strContents, 37, 5)
End If

If Mid(strContents, 1, 3) = "028" Then
CoupCode = Mid(strContents, 14, 6)
End If

If Mid(strContents, 1, 3) = "077" Then
OriginalTransNumber = Mid(strContents, 4, 6)
End If

strNewFile = strNewFile & StoreNumber & TransNumber & _
TransDate & TransCancel & TransType & _
DiscountCode & CoupCode & AmountBeforeDiscount & DiscountAmount & AmountAfterDiscount & OriginalTransNumber

objFile.WriteLine StoreNumber & TransNumber & _
TransDate & TransCancel & TransType & _
DiscountCode & CoupCode & AmountBeforeDiscount & DiscountAmount & AmountAfterDiscount & OriginalTransNumber

Loop
End With


0

Response Number 2
Name: Razor2.3
Date: July 21, 2008 at 18:19:10 Pacific
Reply:

Once the second IF THEN statement locates the data, the strNewFile will write this data to every line, even though it doesn't belong there, until one of the IF THEN statement finds a new piece of data.
Then you need to clear out the old data. Like so:

    If Mid(strContents, 1, 3) = "021" Then
DiscountCode = Mid(strContents, 4, 8)
AmountBeforeDiscount = Mid(strContents, 17, 5)
DiscountAmount = Mid(strContents, 27, 5)
AmountAfterDiscount = Mid(strContents, 37, 5)
Else
DiscountCode = ""
AmountBeforeDiscount = ""
DiscountAmount = ""
Amount AfterDiscount = ""
End If


0

Response Number 3
Name: skisalomon77
Date: July 21, 2008 at 19:05:53 Pacific
Reply:

Thanks! The data is now writing to the correct lines, except for one problem.

The first 21 bytes in the below three lines are identical, which is totally fine. I was expecting everything to be written on a single line, instead of three separate lines. The first line contains the first IF THEN statement. The second line contains the first and second IF THEN statement. The third line contains the first and third IF THEN statements.

Can I get everything to write on a single line?

001042710062520080200
00104271006252008020012 169000500011900
001042710062520080200ss608


0

Response Number 4
Name: Razor2.3
Date: July 22, 2008 at 01:25:40 Pacific
Reply:

You keep changing your requirements from post to post. This is a sign of a beginning programmer/scripter. It seems to result in the coder having a "big picture" idea of what he wants, but not the lower level steps.

I suggest you sit down and write, step by logical step, exactly what you need to happen. After you do that, you can think about how to accomplish your steps.


0

Response Number 5
Name: skisalomon77
Date: July 22, 2008 at 08:14:08 Pacific
Reply:

Thanks Razor! I am absolutely a beginner at this. My last post of having everything on the same line is how I want the script. I'll run through everything and see what I can come up with.


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon

Backup using xcopy - date... Help With Excel Formula



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: VBS - String Bytes Together Part II

VBScript - String bytes together www.computing.net/answers/programming/vbscript-string-bytes-together/16668.html

VB -- Strings www.computing.net/answers/programming/vb-strings/5214.html

::~~ VB String Search ::~~ www.computing.net/answers/programming/-vb-string-search-/8273.html