Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.CloseDim StoreNumber
Dim TransNumber
Dim TransDate
Dim TransCancel
Dim CoupCode
Dim PostVoidTransStoreNumber = 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

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

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?

If you are trying to extract data from the file you have just read in use:
StoreNumber = Mid(strContents, 6, 3)
Stuart

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 PostVoidTransSet objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\ArchivedSANEW\SAcombine.txt", ForReading)strContents = objFile.ReadAll
objFile.CloseStoreNumber = 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

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

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.

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

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

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.

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, strNewFileSet 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

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 & CoupCodeLoop
End WithEDIT: 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.

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!

![]() |
![]() |
![]() |

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