[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Script or run small program to edit .txt file?

Posted on 2012-08-30
34
Medium Priority
?
578 Views
Last Modified: 2012-09-04
Running WinXP

I have a formatted text file that needs to retain its formatting/spacing and be presented to a few users to be easily manipulated.  

The only values I need changed by the user are:
12-34-56-7 (Good ID)
DOE^JOHN^D (last name, first name, MI)
19110202 (DOB)
F (sex)
99-99-99-9 (Bad ID)

When need to modify this file on a daily basis and it would be much easier to have maybe a VB program or a small JAVA app to show only those fields and then out the to display exactly as the attached file displays.

Any ideas on how easily this could be done?  easy job or a difficult job?
0
Comment
Question by:doc_jay
  • 14
  • 12
  • 7
  • +1
34 Comments
 
LVL 50

Expert Comment

by:Martin Liss
ID: 38352705
Can you post a sample file?
0
 

Author Comment

by:doc_jay
ID: 38352710
..opps, forgot to press 'attach'
a34-TEMPLATE.txt
0
 
LVL 46

Expert Comment

by:aikimark
ID: 38352749
Here is a sample VBScript
Option Explicit


    Dim oFS
    Dim oTS
    Dim strFile
    Set oFS = CreateObject("Scripting.FileSystemObject")
    Set oTS = oFS.GetFile("C:\users\aikimark\downloads\a34-TEMPLATE.txt").OpenAsTextStream(1)   'ForReading
    strFile = oTS.ReadAll
    oTS.Close
    strFile = Replace(strFile, "|12-34-56-7^", "|97-65-43-2^")
    strFile = Replace(strFile, "|DOE^JOHN^D^", "|SMITH^FRED^L^")
    strFile = Replace(strFile, "|19110202|", "|19280401|")
    strFile = Replace(strFile, "|F|", "|M|")
    strFile = Replace(strFile, "|99-99-99-9^", "|88-77-66-5^")
    Set oTS = oFS.GetFile("C:\users\aikimark\downloads\a34-TEMPLATE.txt").OpenAsTextStream(2)   'ForWriting
    oTS.Write strFile
    oTS.Close

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:doc_jay
ID: 38352767
Hey,

Thanks, your script changed the file, but is there a way to display a small GUI to a user and let them type into fields and then let it generate a file?

thank you!
0
 
LVL 46

Expert Comment

by:aikimark
ID: 38352793
You can use an InputBox() function to prompt the user for data
http://www.w3schools.com/vbscript/func_inputbox.asp
0
 

Author Comment

by:doc_jay
ID: 38352832
Thanks for the link.  Man I wish I was a coder/programmer, then maybe I could bang this out in 2-3 minutes like your gurus!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38353342
You need to rely on the format (rather opaque-looking imo) being fixed or you're down to using regular expressions. IOW it cannot be done using string find and replace as in the example above.

IS the format fixed? A couple of other examples as attachments would be nice
0
 

Author Comment

by:doc_jay
ID: 38354013
yes, the format is fixed, we would never want it to change.  If the app could generate the 'attached' file and only change the fields listed above, that would work just great.  Just present to the user the field above to change with descriptions for each and then an 'okay' button maybe?

attaching other examples would just be the same, only the fields above would be different.

thanks
0
 
LVL 46

Expert Comment

by:aikimark
ID: 38354257
I would suggest you create and HTA (HTML Application) with some VBScript to create and output the string with data entered into controls on the window.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38354292
yes, the format is fixed, we would never want it to change.

That's not what i mean by 'fixed'. Being fixed refers to the delimitation, configuration, length, etc. of the fields.

attaching other examples would just be the same
Nonetheless, it would be helpful
0
 
LVL 46

Expert Comment

by:aikimark
ID: 38354372
@doc_jay

You have shown us a single file that has a mixture of delimiters; some pipe and some caret.

Although you have identified the names of the fields to be changed, we do not know any of the rules for these fields, such as validation.  We do not know anything about the rest of the file.
0
 

Author Comment

by:doc_jay
ID: 38354465
Sorry, I have attached a few more examples.  

This file is actually a HL7 ADT^A34 message.  I need these values changed to merge patients together manually on more than one system.  Its very tedious for us to log into multiple systems to make the same change more than three times.  If we could edit this 'template' then I could feed the output file to our Mirth connect server (www.mirthcorp.com) and it could route the newly created ADT^A34 message to multiple systems at once.  This also helps us merge reports as well as they do NOT get merged with the patient when a 'manual' merge is done while logged into the system.

Trying to answer your question:

12-34-56-7 (Good ID)
--this value in the original attachment will always contain numbers and or dashes and sometimes contain two letters.
DOE^JOHN^D (last name, first name, MI)
--these three fields will only contain letters
19110202 (DOB)
--this field needs to be formatted exactly above for the Date of birth
F (sex)
--this field right after the DOB is the SEX and will either be a 'M' or 'F'
99-99-99-9 (Bad ID)
--this field on the last line after 'MRG|' will always contain numbers and or dashes and sometimes contain two letters

the rest of the file can remain the same, as there is no big need to change any other values.

I hope this helps, and that  I'm not being too vague here with your question?

Thank you
EXAMPLE1.txt
EXAMPLE2.txt
EXAMPLE3.txt
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38354539
I hope this helps, and that  I'm not being too vague here with your question?

No. Regrettably this is too complex an issue for me, outside a professional assignment, to offer anything other than a broad outline approach. But when i've looked at it, i can possibly give you some pointers
0
 
LVL 46

Expert Comment

by:aikimark
ID: 38354601
>>sometimes contain two letters
Is there a rule about where the letters may occur?
Are the letters mixed case, upper case, or lower case?

>>--these three fields will only contain letters
What about Irish names?
What about hyphenated names?

What fields are mandatory?
What are default values for these fields?

========
Do you have any experience with HTML?
Does this process need to be single instance or does this process need to create multiple sets of change records in the output file or create multiple output files in succession?
0
 

Author Comment

by:doc_jay
ID: 38354649
Answers to your question:

1)  The letters will be uppercase and will always be the 1st two characters before the number, but there will be occurrences where there will NO letters in this field at all.
2)  You are right, there might be the occasional " ' " needed for Irish names or a " - " for hyphenated names.
3)  the mandatory fields are the ones listed above in my post, but they need to remain in the same area as they do in the example.  The rest of the example needs to remain the same.  I hope that is clear enough and that has answered your question?

==============
I have no experience with HTML
The process needs to create a single file, each time a user users it to change the values.

thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38354716
OK, by the looks, the records are far too complex to attempt a naive parser/message editor/creator. You need software that supports HL7 standards
0
 

Author Comment

by:doc_jay
ID: 38354731
really, okay..I was afraid of that.  thanks anyways and i will award points across the board.  :-/
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38354925
No problem. Feel free to contact me via my profile if you want to take it further
0
 
LVL 46

Expert Comment

by:aikimark
ID: 38355061
stop.  do not close this question.

Just because one expert has opined "Regrettably this is too complex an issue for me", it does not mean that the problem is unsolvable by other experts.
0
 
LVL 46

Expert Comment

by:aikimark
ID: 38355642
I've created a new template file.  Please place the following code into a text file with an .HTA file extension.  You will need to change the input and output folders in the code to suit your run-time environment.  Play with it.  Add your own validations.
<html>
<head>
<title>Q_27848845</title>

<HTA:APPLICATION 
     ID="Q_27848845"
     APPLICATIONNAME="Q_27848845"
     SCROLL="yes"
     SINGLEINSTANCE="yes"
>
</head>

<SCRIPT Language="VBScript">
Option Explicit

sub UcaseMe(parmCtl)
  parmctl.value = ucase(parmctl.value)
end sub 

sub CreateFile

    Dim oFS
    Dim oTS
    Dim strFile
    Dim strOutFileName, strErrMsgs
    If DataNotValid(strErrMsgs) Then
       msgbox "There are validation errors in your data. Please correct and try again" & vbcr & strErrMsgs
       exit sub
    End If
    Set oFS = CreateObject("Scripting.FileSystemObject")
    Set oTS = oFS.GetFile("C:\users\aikimark\downloads\Q_27848845_TEMPLATE.txt").OpenAsTextStream(1)   'ForReading
    strFile = oTS.ReadAll
    oTS.Close
    strFile = Replace(strFile, "$[GoodID]$", txtG1.Value & "-" & txtG2.Value & "-" & txtG3.Value & "-" & txtG4.Value)
    strFile = Replace(strFile, "$[Lname]$", txtLname.value)
    strFile = Replace(strFile, "$[Fname]$", txtFname.value)
    strFile = Replace(strFile, "$[MI]$", txtMI.value)
    strFile = Replace(strFile, "$[DOB]$", txtDOB.value)
    strFile = Replace(strFile, "$[Sex]$", txtSex.value)
    strFile = Replace(strFile, "$[BadID]$", txtB1.Value & "-" & txtB2.Value & "-" & txtB3.Value & "-" & txtB4.Value)
    strOutFileName = "C:\users\aikimark\downloads\" & Year(now) & Month(Now) & Day(Now) & "_" & Right("0" & Hour(Now), 2) & Right("0" & Minute(Now), 2) & Right("0" & Second(Now), 2) & ".txt"
    oFS.CreateTextFile strOutFileName
    Set oTS = oFS.GetFile(strOutFileName).OpenAsTextStream(2)   'ForWriting
    oTS.Write strFile
    oTS.Close
    msgbox "File created: " & strOutFilename
end sub

Function DataNotValid(parmErrorMsgs)
    DataNotValid = False

    If (Len(txtSex.Value)=0) or (InStr("MF", txtSex.Value) = 0) Then
        DataNotValid = True
        txtSex.style.background = "Yellow"
        txtSex.Focus
        parmErrorMsgs = parmErrorMsgs & "* Sex not equal to M or F" & vbcr
    Else
        txtSex.style.background = "White"
    End If
    If IsNumeric(txtDOB.Value) And Len(txtDOB.Value) = 8 Then
        txtDOB.style.background = "White"
    Else
        DataNotValid = True
        txtDOB.style.background = "Yellow"
        txtDOB.Focus
        parmErrorMsgs = parmErrorMsgs & "* DOB not numeric or less than 8 digits" & vbcr
    End If

End Function

</SCRIPT>
<body>
<b>Good ID: </b>
<input type="text" name="txtG1" size="2" maxlength="2" onblur="UcaseMe(txtG1)">-
<input type="text" name="txtG2" size="2" maxlength="2" onblur="UcaseMe(txtG2)">-
<input type="text" name="txtG3" size="2" maxlength="2" onblur="UcaseMe(txtG3)">-
<input type="text" name="txtG4" size="1" maxlength="1" onblur="UcaseMe(txtG4)"><BR>
<b>Last Name: </b><input type="text" name="txtLname" size="50" onblur="UcaseMe(txtLname)"> <b>First Name: </b><input type="text" name="txtFname" size="50" onblur="UcaseMe(txtFname)"> <b>MI: </b><input type="text" name="txtMI" size="1" onblur="UcaseMe(txtMI)"><BR>
<b>DOB (yyyymmdd): </b><input type="text" name="txtDOB" size="8" maxlength="8"><BR>
<b>Sex (M|F): </b><input type="text" name="txtSex" size="1" maxlength="1" onblur="UcaseMe(txtSex)"><BR>
<b>Bad ID: </b>
<input type="text" name="txtB1" size="2" maxlength="2" onblur="UcaseMe(txtB1)">-
<input type="text" name="txtB2" size="2" maxlength="2" onblur="UcaseMe(txtB2)">-
<input type="text" name="txtB3" size="2" maxlength="2" onblur="UcaseMe(txtB3)">-
<input type="text" name="txtB4" size="1" maxlength="1" onblur="UcaseMe(txtB4)"><BR>
<BR>
<input id=cmdCreateFile  class="button" type="button" value="Create File" name="cmdCreateFile"  onClick="CreateFile">
</body>
</html>

Open in new window

Q-27848845-TEMPLATE.txt
0
 

Author Comment

by:doc_jay
ID: 38363517
aikimark,

   Wow!  very impressive!  I hope you could look at this code once more and make some modifications for me?  For the good ID & bad ID, I actually need these fields to be one field each.  Our IDs are actually a mix of hypenated IDs and non-hyphenated IDs and the non hypenated ones begin with 'SJ'.  So, could you make each of these two fields display as 'one' field each with a max of 10 characters?  Instead of the 4 fields split by hyphens for the good & bad IDs?

Also, for DOB, could that be setup so that it always spits out the DOB as year, month, day no matter how the user inputs the DOB?

thank you so much for all of your help!  I'm very happy!
0
 
LVL 46

Expert Comment

by:aikimark
ID: 38363532
would the goodid/badid field length include internal hyphens?

>>spits out the DOB
does that mean that the DOB needs to be three separate textboxes?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38363533
As a matter of interest, is this a UK thing you're engaged in?
0
 

Author Comment

by:doc_jay
ID: 38363572
as far as the IDs go, we have two format that we are using:

7-digits & 8-digits.  We ran out of 7-digit IDs at the beginning of the year so we moved to 8-digit IDs and we also put a 'SJ' at the beginning of the 8-digit IDs.

Only the 7-digit ID are hypenated.

ex.   (7-digit)  12-34-56-7
ex.  (8-digit)  SJ12345678

>>does that mean that the DOB needs to be three separate text boxes?
Lets make it three text boxes please, but the output file always needs to have that field formatted as 'yyyymmdd' just as you had it already.

thank you
0
 

Author Comment

by:doc_jay
ID: 38363574
CEHJ,

   No, I'm not in the UK, I'm in the US.  Dealing with multiple IDs in my hosp.
0
 

Author Comment

by:doc_jay
ID: 38363592
aikimark,

   Can I contact you via your profile?

thanks
0
 
LVL 46

Accepted Solution

by:
aikimark earned 2000 total points
ID: 38363595
<html>
<head>
<title>Q_27848845</title>

<HTA:APPLICATION 
     ID="Q_27848845"
     APPLICATIONNAME="Q_27848845"
     SCROLL="yes"
     SINGLEINSTANCE="yes"
>
</head>

<SCRIPT Language="VBScript">
Option Explicit

sub UcaseMe(parmCtl)
  parmctl.value = ucase(parmctl.value)
end sub 

sub CreateFile

    Dim oFS
    Dim oTS
    Dim strFile
    Dim strOutFileName, strErrMsgs
    If DataNotValid(strErrMsgs) Then
       msgbox "There are validation errors in your data. Please correct and try again" & vbcr & strErrMsgs
       exit sub
    End If
    Set oFS = CreateObject("Scripting.FileSystemObject")
    Set oTS = oFS.GetFile("C:\users\mark\downloads\Q_27848845_TEMPLATE.txt").OpenAsTextStream(1)   'ForReading
    strFile = oTS.ReadAll
    oTS.Close
    strFile = Replace(strFile, "$[GoodID]$", txtGood.Value)
    strFile = Replace(strFile, "$[Lname]$", txtLname.value)
    strFile = Replace(strFile, "$[Fname]$", txtFname.value)
    strFile = Replace(strFile, "$[MI]$", txtMI.value)
    strFile = Replace(strFile, "$[DOB]$", txtDOByear.value & txtDOBmonth.value & txtDOBday.value)
    strFile = Replace(strFile, "$[Sex]$", txtSex.value)
    strFile = Replace(strFile, "$[BadID]$", txtBad.Value)
    strOutFileName = "C:\users\mark\downloads\" & Year(now) & Month(Now) & Day(Now) & "_" & Right("0" & Hour(Now), 

2) & Right("0" & Minute(Now), 2) & Right("0" & Second(Now), 2) & ".txt"
    oFS.CreateTextFile strOutFileName
    Set oTS = oFS.GetFile(strOutFileName).OpenAsTextStream(2)   'ForWriting
    oTS.Write strFile
    oTS.Close
    msgbox "File created: " & strOutFilename
end sub

Function DataNotValid(parmErrorMsgs)
    DataNotValid = False

    If (Len(txtSex.Value)=0) or (InStr("MF", txtSex.Value) = 0) Then
        DataNotValid = True
        txtSex.style.background = "Yellow"
        txtSex.Focus
        parmErrorMsgs = parmErrorMsgs & "* Sex not equal to M or F" & vbcr
    Else
        txtSex.style.background = "White"
    End If
    If IsDate(txtDOByear.Value & "-" & txtDOBmonth.Value & "-" & txtDOBday.Value) Then
        txtDOByear.style.background = "White"
        txtDOBmonth.style.background = "White"
        txtDOBday.style.background = "White"
    Else
        DataNotValid = True
        txtDOByear.style.background = "Yellow"
        txtDOBmonth.style.background = "Yellow"
        txtDOBday.style.background = "Yellow"
        txtDOByear.Focus
        parmErrorMsgs = parmErrorMsgs & "* DOB not a valid date" & vbcr
    End If

End Function

</SCRIPT>
<body>
<b>Good ID: </b>
<input type="text" name="txtGood" size="10" maxlength="10" onblur="UcaseMe(txtGood)"><BR>
<b>Last Name: </b><input type="text" name="txtLname" size="50" onblur="UcaseMe(txtLname)"> <b>First Name: </b><input 

type="text" name="txtFname" size="50" onblur="UcaseMe(txtFname)"> <b>MI: </b><input type="text" name="txtMI" 

size="1" onblur="UcaseMe(txtMI)"><BR>
<b>DOB: </b>Year: <input type="text" name="txtDOByear" size="4" maxlength="4"> 
Month: <input type="text" name="txtDOBmonth" size="2" maxlength="2"> 
Day: <input type="text" name="txtDOBday" size="2" maxlength="2"><BR>
<b>Sex (M|F): </b><input type="text" name="txtSex" size="1" maxlength="1" onblur="UcaseMe(txtSex)"><BR>
<b>Bad ID: </b>
<input type="text" name="txtBad" size="10" maxlength="10" onblur="UcaseMe(txtBad)"><BR>
<BR>
<input id=cmdCreateFile  class="button" type="button" value="Create File" name="cmdCreateFile"  

onClick="CreateFile">
</body>
</html>

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38363717
  No, I'm not in the UK, I'm in the US.

Phew! This is what you should use most probably

http://hl7api.sourceforge.net/
0
 

Author Comment

by:doc_jay
ID: 38363729
Wow, looks like a great project.  Thank you very much for the link and I really think this tool will come in handy for me!
0
 
LVL 46

Expert Comment

by:aikimark
ID: 38364580
@doc_jay

Did you receive my member comment notification?
0
 

Author Comment

by:doc_jay
ID: 38364616
sorry, I'm not seeing it.  Where should I look?
0
 
LVL 46

Expert Comment

by:aikimark
ID: 38364632
you should have received an email from EE.
There is a member comment at the bottom of your profile page.
0
 

Author Closing Comment

by:doc_jay
ID: 38364667
Great job!  thank you everyone for your time and help with this question.
0
 
LVL 46

Expert Comment

by:aikimark
ID: 38364701
@doc_jay

If you want more consistency in the output file name, you can try this.  It will force the month and day part of the file name to be two characters just like I did with the time component.
strOutFileName = "C:\users\mark\downloads\" & Year(now) & Right("0" & Month(Now), 2) & Right("0" & Day(Now), 2) & "_" & Right("0" & Hour(Now), 2) & Right("0" & Minute(Now), 2) & Right("0" & Second(Now), 2) & ".txt"

Open in new window


Thanks for the points.

I know you were wondering...and the answer is "yes."  This can be turned into a web page with by renaming the file extension to HTML
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to demonstrate how we can use conditional statements using Python.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question