1.

Solve : VBS - Replace String where not 0 or 1?

Answer»

I have a value "00000000" without quotes where I am adding a 1 to any of the positions if a value has changed in an input file but in some cases I will end up with a value of "00130012" but what I would like my end result to be is only 0(unchanged) or 1(changed) in any position ... in other works if the value is greater then 1 I want to change it back to a 1. I'm trying to figure out if the easiest way to do this would be a replace function of some SORT? I'm thinking there would have to be something simpler then a doing a split and checking each value. Or instead of the above is there a way to check the position of a digit within the value to see if ALREADY 1, if it is don't add again. I COULD do 8 counters for each position that if greater then 0 add 1 to the position but keeping track of a bunch of counters seems like a lot of work. why don't you show us your code so far? That would save one *censored* of a lot of time asking questions. Why don't you, instead of adding 1 to a position, check if the digit in that position is already changed to 1 and if it is, leave it alone?

Ended up using counters to track if I had already added a 1 to that position. Not sure how to go about checking if the position already has a 1 within a number.

Didn't post the code as it is quite messy and very much spaghetti code. Not really sure yet how to clean it up into tidy functions but at least it works and does what I want for now. I'll still posted if you CARE to see as I'm always interested in learning a cleaner meaner approach. Quote from: nothlit on October 29, 2010, 01:30:52 PM

Ended up using counters to track if I had already added a 1 to that position. Not sure how to go about checking if the position already has a 1 within a number.

I am wondering... what are you going to do when you get to more than 9 changes for any particular position? Whne the number of changes is no longer a single digit?

But assuming you never reach that state, you could

create an empty string
go through your string e.g. "00130012" character by character
for each character of your string, if it is an 0 add a 0 to your new string otherwise add a 1 to your new string.

e.g.
Code: [Select]MyString="00130012"
NuString=""
For i=1 To 8
    If Mid(MyString,i,1)="0" Then
       NuString=NuString & "0"
    Else
       NuString=NuString & "1"
    End If
Next
REM NuString now reads 00110011
But why don't you just have an array with 8 elements, set them all to 0 initially and then it's easy to check if an element is 0 and if it is, change it to 1, and if it isn't 0, leave it alone. At the end read out the elements into a string like above.





My messy code ...
Code: [Select]Option Explicit
'''''''''''''''''
'''''''''''''''''This script creates flat file for ORC research.
'''''''''''''''''
Dim objOutputFile, s, objFS, objArgs, strInputFile, objFile, strOutputfile
Dim objFSO, boolMatchCaseSensitive, objInputFile, intArgCount, s1
Dim Count, dater, Branchn, a
Dim StrDate, StrTime

Dim origBatch, origTrans, origOLD, origISN, origAUX, origRT
Dim origwAUX, origACCT, origTC, origAMT

Dim storBatch, storTrans, storOLD, storISN, storAUX, storRT
Dim storwAUX, storACCT, storTC, storAMT

Dim storAUXn, storRTn, storwAUXn, storACCTn, storTCn

Dim strOutputFilePath

Dim editFlds, stroutput

Call intialize()


Set objFS=CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
Const intForWriting = 2
Const intForAppending = 8

Set objArgs = WScript.Arguments
'strInputFile = objArgs(0)
intArgCount = WSCript.Arguments.Count
'Check for at least first two arugments. First arugment is input, second is outputfile(DAT format)
if intArgCount < 2 then
MsgBox ("Need input file as first argument")
MsgBox ("Need ouput file as second argument")
WScript.Quit(99)
Else
strInputFile = objArgs(0)
'strOutputfile = "???.???"
strOutputFile = objArgs(1)
End If
'Check if Inputfile (argument1) exist, if not then exit with Error
If objFS.fileExists(strInputFile) Then
ELSE
MsgBox (strInputfile & " does not exist")
WScript.Quit(99)
End If
'MsgBox strOutputFile
'Set objFile = objFS.OpenTextFile(strInputFile)
If objFS.fileExists(strOutputfile) Then objFS.DeleteFile(StrOutputfile)
strOutputFilePath = objFS.GetParentFolderName(strOutputfile)
'If Not exists(strOutputFilePath) then
'CreateFolder(strOutputFilePath)
'End If
Set objInputFile = objFS.OpenTextFile(strInputFile, intForReading, False) 'Open Inputfile to read line by line (s variable in do loop)
Set objOutputFile = objFS.OpenTextFile(strOutputFile,intForAppending, True) 'Open Outputfile and set append mode for writing to file

Do While objInputFile.AtEndOfStream <> True
s = objInputFile.ReadLine()

If InStr (s,"Original") > 0 Then
If Count <> 0 Or editFlds <> 00000000 Then
If Count = 0 Then
objOutputFile.WriteLine origISN & " " & "01" & " " & editFlds
Else
objOutputFile.WriteLine stroutput & " " & editFlds
End If
End If
'stroutput = ""
'Count = 0
'editFlds = 00000000
Call intialize()

'origBatch = Trim(Mid(s,1,7))
'origBatch = Replace(origBatch, " ", "")
'origBatch = Replace(origBatch, " ", "")
'origTrans = Trim(Mid(s,7,8))
'origTrans = Replace(origTrans, " ", "")
'origTrans = Replace(origTrans, " ", "")
'origOLD   = Trim(Mid(s,15,9))
'origOLD = Replace(origOLD, " ", "")
'origOLD = Replace(origOLD, " ", "")
origISN   = Trim(Mid(s,25,10))
origISN = Replace(origISN, " ", "")
origISN = Replace(origISN, " ", "")
origAUX   = Trim(Mid(s,35,9))
origAUX = Replace(origAUX, " ", "")
origAUX = Replace(origAUX, " ", "")
origRT    = Trim(Mid(s,44,11))
origRT = Replace(origRT, " ", "")
origRT = Replace(origRT, " ", "")
origwAUX  = Trim(Mid(s,54,9))
origwAUX = Replace(origwAUX, " ", "")
origwAUX = Replace(origwAUX, " ", "")
origACCT  = Trim(Mid(s,63,18))
origACCT = Replace(origACCT, " ", "")
origACCT = Replace(origACCT, " ", "")
origTC    = Trim(Mid(s,81,7))
origTC = Replace(origTC, " ", "")
origTC = Replace(origTC, " ", "")
'Not using DESCRIPTION at this time
origAMT   = Trim(Mid(s,99,14))
origAMT = Replace(origAMT, " ", "")
origAMT = Replace(origAMT, " ", "")

End If

If InStr (s,"ChangedTo") > 0 Then
'storBatch = Trim(Mid(s,1,7))
'storBatch = Replace(storBatch, " ", "")
'storBatch = Replace(storBatch, " ", "")
'storTrans = Trim(Mid(s,7,8))
'storTrans = Replace(storTrans, " ", "")
'storTrans = Replace(storTrans, " ", "")
'storOLD   = Trim(Mid(s,15,10))
'storOLD = Replace(storOLD, " ", "")
'storOLD = Replace(storOLD, " ", "")
storISN   = Trim(Mid(s,25,11))
storISN = Replace(storISN, " ", "")
storISN = Replace(storISN, " ", "")
storAUX   = Trim(Mid(s,36,10))
storAUX = Replace(storAUX, " ", "")
storAUX = Replace(storAUX, " ", "")
storRT    = Trim(Mid(s,45,10))
storRT = Replace(storRT, " ", "")
storRT = Replace(storRT, " ", "")
storwAUX  = Trim(Mid(s,55,9))
storwAUX = Replace(storwAUX, " ", "")
storwAUX = Replace(storwAUX, " ", "")
storACCT  = Trim(Mid(s,63,19))
storACCT = Replace(storACCT, " ", "")
storACCT = Replace(storACCT, " ", "")
storTC    = Trim(Mid(s,81,7))
storTC = Replace(storTC, " ", "")
storTC = Replace(storTC, " ", "")
'Not using DESCRIPTION at this time
storAMT   = Trim(Mid(s,100,14))
storAMT = Replace(storAMT, " ", "")
storAMT = Replace(storAMT, " ", "")
'MsgBox "origAMT:"& origAMT &"storAMT:"& storAMT
If Count = 0  Then
If origAMT = 0.00 And storAMT > origAMT Then
'objOutputFile.WriteLine origISN & " " & "00"
stroutput = origISN & " " & "00"
Count = Count + 1
ElseIf origAMT <> storAMT Then
'objOutputFile.WriteLine origISN & " " & "02"
stroutput = origISN & " " & "02"
Count = Count + 1
End If
End If

If origAUX <> storAUX And storAUXn = 0 Then
editFlds = editFlds + 01000000
storAUXn = storAUXn + 1
End If
'If origRIC <> storRIC Then
'editFlds = editFlds + 00100000
'End If
If origRT <> storRT And storRTn = 0 Then
editFlds = editFlds + 00010000
storRTn = storRTn + 1
End If
If origwAUX <> storwAUX And storwAUXn = 0 Then
editFlds = editFlds + 00001000
storwAUXn = storwAUXn + 1
End If
If origACCT <> storACCT And storACCTn = 0 Then
editFlds = editFlds + 00000100
storACCTn = storACCTn + 1
End If
If origTC <> storTC And storTCn = 0 Then
editFlds = editFlds + 00000010
storTCn = storTCn + 1
End If
If origAMT <> storAMT Then
If Count = 1 Then
editFlds = editFlds + 00000001
Count = Count + 1
End If
End If
editFlds = Right("00000000" & editFlds, 8)

End If
Loop
objOutputFile.Close
        Set objOutputFile = Nothing
        Set objFS = Nothing
'WScript.Echo "Completed"
WScript.Quit()

Function intialize()

editFlds = 00000000
' ^Empty SERIAL RIC(Pos44) TR FLD4 AMT TC AMT
Count = 0

storAUXn = 0
storRTn = 0
storwAUXn = 0
storACCTn = 0
storTCn = 0

End Function

An array isn't a bad idea, considered it but as you can see above still very messy.


Discussion

No Comment Found