Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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, strNewFileSet 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 IfIf 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 IfIf AddInput = Mid(strContents, 3, 3) = "028" Then
CoupCode = Mid(strContents, 14, 6)
End IfstrNewFile = strNewFile & StoreNumber & TransNumber & _
TransDate & TransCancel & TransType & _
DiscountCode & CoupCode & AmountBeforeDiscount & DiscountAmount & AmountAfterDiscount & OriginalTransNumber
objFile.WriteLine StoreNumber & TransNumber & _
TransDate & TransCancel & TransType & _
DiscountCode & CoupCode & AmountBeforeDiscount & DiscountAmount & AmountAfterDiscount & OriginalTransNumberLoop
End With

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, strNewFileSet 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 IfIf Mid(strContents, 1, 3) = "028" Then
CoupCode = Mid(strContents, 14, 6)
End IfIf Mid(strContents, 1, 3) = "077" Then
OriginalTransNumber = Mid(strContents, 4, 6)
End IfstrNewFile = strNewFile & StoreNumber & TransNumber & _
TransDate & TransCancel & TransType & _
DiscountCode & CoupCode & AmountBeforeDiscount & DiscountAmount & AmountAfterDiscount & OriginalTransNumber
objFile.WriteLine StoreNumber & TransNumber & _
TransDate & TransCancel & TransType & _
DiscountCode & CoupCode & AmountBeforeDiscount & DiscountAmount & AmountAfterDiscount & OriginalTransNumberLoop
End With

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

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

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.

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.

![]() |
Backup using xcopy - date...
|
Help With Excel Formula
|

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