Jump to content

Text File help


Recommended Posts

First text file

Isodraft
options default
$M "%MECISO%/STD/D_sheet"
add PIPE /MEC001SLP1001A06
detail all
monitor
$.

Second text file

Isodraft
options default
$M "%MECISO%/STD/D_sheet"
/MEC001SLP1001A06
$M "%MECISO%/STD/D_sheet"
detail /MEC001SLP1001A06
$.

Hello,

Can someone please help me out? I need a fast automated way to change the format from the first text file to the second text file. can someone please point me in the right direction?

Link to comment
Share on other sites

I suggest reading them line by line and doing string manipulation. You can read in a line with:

#include <file.au3>

$file = FileOpen("yourfile.txt", 0)

While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    MsgBox(0,'',$line)
WEnd
FileClose($file)

and then do a StringReplace() on the lines that you want to change.

UHJvZmVzc2lvbmFsIENvbXB1dGVyZXI=

Link to comment
Share on other sites

  • Moderators

wklw2010,

Read the file into an array using _FileReadToArray, adjust the content (see example below) and then rewrite it using _FileWriteFromArray. The "adjusting" can be done like this:

#include <StringConstants.au3>

#include <Array.au3>

; Simulate reading file into an array

$sOrg = "Isodraft" & @CRLF & _
    "options default" & @CRLF & _
    '$M "%MECISO%/STD/D_sheet"' & @CRLF & _
    "add PIPE /MEC001SLP1001A06" & @CRLF & _
    "detail all" & @CRLF & _
    "monitor" & @CRLF & _
    "$."
$aLines = StringSplit($sOrg, @CRLF, $STR_ENTIRESPLIT)

; And here it is
_ArrayDisplay($aLines, "", Default, 8) ; All these _ArrayDisplay lines are just to show what is happening

; Split the "add PIPE /" line to get the value after the slash
$aSplit = StringSplit($aLines[4], "/")

; Which we add to the array
$aLines[4] = "/" & $aSplit[2]
_ArrayDisplay($aLines, "", Default, 8)

; Rewrite this line to match the earlier one
$aLines[5] = $aLines[3]
_ArrayDisplay($aLines, "", Default, 8)

; And finally rewrite this one
$aLines[6] = "detail /" & $aSplit[2]
_ArrayDisplay($aLines, "", Default, 8)

; And now rewrite the file

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

wklw2010,

Which is exactly what my suggested solution will give you - have you tried it?

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Yes but I got confused... sometimes the text files will be different. so, this will load the text file, read it and output the new format... I've wrote way hard stuff then this, not sure way I am so confused on this... sorry for my confusion... ha ha  thank you very much.

Link to comment
Share on other sites

This example divides the entire first file into variables (actually back-references) .  Then, the first file can be transformed (re-formatted)  into the second file by re-arranging the back-references.

;Local $sFirst_text_file = FileRead("FirstFile.txt") ; Uncomment this line if a file is to be used

; Or

;#cs  ; Uncomment this line if a file is to be used
Local $sFirst_text_file = _
        "Isodraft" & @CRLF & _
        "options default" & @CRLF & _
        '$M "%MECISO%/STD/D_sheet"' & @CRLF & _
        "add PIPE /MEC001SLP1001A06" & @CRLF & _
        "detail all" & @CRLF & _
        "monitor" & @CRLF & _
        "$."
;#ce ; Uncomment this line if a file is to be used

Local $sSecond_text_file = StringRegExpReplace($sFirst_text_file, "" & _
        '(\V+\v+)' & _ ;                               1st line, BR 1  (BR = Back Reference)
        '(options )(\V+\v+)' & _ ;                     2nd line, BR 2 & 3
        '(\V+\v+)' & _ ;                               3rd line, BR 4
        '(add PIPE )(\V+\v+)' & _ ;                    4th line, BR 5 & 6
        '(detail )(\V+\v+)' & _ ;                      5th line, BR 7 & 8
        '(\V+\v+)' & _ ;                               6th line, BR 9
        '(\V+)', _ ;                                   7th line, BR 10
        '${1}${2}${3}${4}${6}${4}${7}${6}${10}') ;     Format (re-arrange Back References (BR))

;FileWrite("SecondFile.txt", $sSecond_text_file)  ; Uncomment this line to write to a file.
MsgBox(0, "Re-formated", $sSecond_text_file)

#cs ; Returns:-
Isodraft
options default
$M "%MECISO%/STD/D_sheet"
/MEC001SLP1001A06
$M "%MECISO%/STD/D_sheet"
detail /MEC001SLP1001A06
$.
#ce

 

Link to comment
Share on other sites

  • Moderators

wklw2010,

Quote

 sometimes the text files will be different

Can you provide a range of pre/post change files so that we can see if there is a pattern to follow. If there is no pattern, then it is very unlikely that an automated solution can help, but let us try and find one forst.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

New File...

Isodraft
options default
$M "%MECISO%/STD/D_sheet"
add PIPE /MEC001ANS1901A01
add PIPE /MEC001BFW1001A06
add PIPE /MEC001BFW1002A06
add PIPE /MEC001BFW1003A06

$.

What needs to be outputed from the text file.

Isodraft
options default
$M "%MECISO%/STD/D_sheet"
/MMEC001ANS1901A01
$M "%MECISO%/STD/D_sheet"
detail /MEC001ANS1901A01

$M "%MECISO%/STD/D_sheet"
/MEC001BFW1001A06
$M "%MECISO%/STD/D_sheet"
detail /MEC001BFW1002A06

$M "%MECISO%/STD/D_sheet"
/MEC001BFW1002A06
$M "%MECISO%/STD/D_sheet"
detail /MEC001BFW1002A06
$.

Here is another example, I was thinking of some type of array to read the MEC number. thanks you very much for the help. I also attached another example of the text file that needs to be read and converted to the new format.

Batch

Edited by wklw2010
Link to comment
Share on other sites

  • Moderators

wklw2010,

So what is the pattern?

Original:

add PIPE /MEC001ANS1901A01   
add PIPE /MEC001BFW1001A06
add PIPE /MEC001BFW1002A06
add PIPE /MEC001BFW1003A06

New:

$M "%MECISO%/STD/D_sheet"
/MMEC001ANS1901A01            ; Matches below
$M "%MECISO%/STD/D_sheet"
detail /MEC001ANS1901A01      ; Matches above

$M "%MECISO%/STD/D_sheet"
/MEC001BFW1001A06             ; Does not match below
$M "%MECISO%/STD/D_sheet"
detail /MEC001BFW1002A06      ; Does not match above

$M "%MECISO%/STD/D_sheet"
/MEC001BFW1002A06             ; Matches below
$M "%MECISO%/STD/D_sheet"
detail /MEC001BFW1002A06      ; Matches above

And where is MEC001BFW1003A06?

Are you sure you have the correct required output there?

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

My tentative try  :huh2:

;#Include <Array.au3>

$txt = FileRead("Batch")
$MECs = StringRegExp($txt, '/MEC\w+', 3)
;_ArrayDisplay($MECs)

$out = "Isodraft" & @crlf & "options default" & @crlf
For $i = 0 to UBound($MECs)-1
   $out &= '$M "%MECISO%/STD/D_sheet"' & @crlf & _ 
        $MECs[$i] & @crlf & _ 
        '$M "%MECISO%/STD/D_sheet"' & @crlf & _ 
        "detail " & $MECs[$i] & @crlf & @crlf 
Next
$out &= "$."
Msgbox(0,"", $out)

 

Link to comment
Share on other sites

20 minutes ago, mikell said:

My tentative try  :huh2:

;#Include <Array.au3>

$txt = FileRead("Batch")
$MECs = StringRegExp($txt, '/MEC\w+', 3)
;_ArrayDisplay($MECs)

$out = "Isodraft" & @crlf & "options default" & @crlf
For $i = 0 to UBound($MECs)-1
   $out &= '$M "%MECISO%/STD/D_sheet"' & @crlf & _ 
        $MECs[$i] & @crlf & _ 
        '$M "%MECISO%/STD/D_sheet"' & @crlf & _ 
        "detail " & $MECs[$i] & @crlf & @crlf 
Next
$out &= "$."
Msgbox(0,"", $out)

 

This works... We just need to output it to a text file. Thanks very much for your help. I've been trying to get this done for a while now. I've been writing a ton of PML code lately... Good to get back to AutoIT..... 

Edited by wklw2010
Link to comment
Share on other sites

1 hour ago, Melba23 said:

wklw2010,

So what is the pattern?

Original:

add PIPE /MEC001ANS1901A01   
add PIPE /MEC001BFW1001A06
add PIPE /MEC001BFW1002A06
add PIPE /MEC001BFW1003A06

New:

$M "%MECISO%/STD/D_sheet"
/MMEC001ANS1901A01            ; Matches below
$M "%MECISO%/STD/D_sheet"
detail /MEC001ANS1901A01      ; Matches above

$M "%MECISO%/STD/D_sheet"
/MEC001BFW1001A06             ; Does not match below
$M "%MECISO%/STD/D_sheet"
detail /MEC001BFW1002A06      ; Does not match above

$M "%MECISO%/STD/D_sheet"
/MEC001BFW1002A06             ; Matches below
$M "%MECISO%/STD/D_sheet"
detail /MEC001BFW1002A06      ; Matches above

And where is MEC001BFW1003A06?

Are you sure you have the correct required output there?

M23

 

Thank you for the help and looking at this. Have a great day!!!

Link to comment
Share on other sites

1 hour ago, mikell said:

FileWrite("file_out.txt", $string_out)

... what else ?  :)

#include <GUIConstants.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("ISO Convert", 303, 141, 193, 115)
$Input1 = GUICtrlCreateInput("", 21, 19, 201, 21)
$Button1 = GUICtrlCreateButton("Browse...", 235, 16, 64, 25, 0)
$Group1 = GUICtrlCreateGroup("Browse what", 20, 44, 279, 63)
$Radio1 = GUICtrlCreateRadio("Folder", 26, 60, 113, 17)
$Radio2 = GUICtrlCreateRadio("File to open", 26, 78, 113, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Button2 = GUICtrlCreateButton("Exit", 220, 111, 75, 25, 0)
$Button3 = GUICtrlCreateButton("Run", 10, 111, 75, 25, 0)
;~ $Label1 = GUICtrlCreateLabel("ISO COnvert", 0, 0, 35, 25, -1, $GUI_WS_EX_PARENTDRAG)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Select
        Case $nMsg = $GUI_EVENT_CLOSE Or $nMsg = $Button2
            Exit
        Case $nMsg = $Button1
            If GuiCtrlRead($Radio1) = $GUI_CHECKED Then
                $txt=FileSelectFolder( "Open", "C:\")
                GUICtrlSetData($Input1,$txt)
            ElseIf GuiCtrlRead($Radio2) = $GUI_CHECKED Then
                $txt=FileOpenDialog("Open", "C:\","All (*.*)")
                GUICtrlSetData($Input1,$txt)
            Else
                MsgBox(64,"Browse button","You must select something.")
            EndIf

    Case $nMsg = $Button3
        $txt = FileRead("")
        $MECs = StringRegExp($txt, '/MEC\w+', 3)
        ;_ArrayDisplay($MECs)

        $out = "Isodraft" & @crlf & "options default" & @crlf
        For $i = 0 to UBound($MECs)-1
        $out &= '$M "%MECISO%/STD/D_sheet"' & @crlf & _
        $MECs[$i] & @crlf & _
        '$M "%MECISO%/STD/D_sheet"' & @crlf & _
        "detail" & $MECs[$i] & @crlf & @crlf
Next
    $out &= "$."
    FileWrite(@desktopdir & "\New_Batch.txt", $out)


    EndSelect
WEnd

ha ha, I really thank you for the support.

I am trying to make button3 read the input box, instead of hard coding a path to the batch file...

This is not working...

Case $msg = $button3 AND BitAND(GUICtrlRead($button3), $input) = $input

Edited by wklw2010
Link to comment
Share on other sites

Hmmmm... you're a bit tired I guess  :)

#include <GUIConstants.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("ISO Convert", 303, 141, 193, 115)
$Input1 = GUICtrlCreateInput("", 21, 19, 201, 21)
$Button1 = GUICtrlCreateButton("Browse...", 235, 16, 64, 25, 0)
$Group1 = GUICtrlCreateGroup("Browse what", 20, 44, 279, 63)
$Radio1 = GUICtrlCreateRadio("Folder", 26, 60, 113, 17)
$Radio2 = GUICtrlCreateRadio("File to open", 26, 78, 113, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Button2 = GUICtrlCreateButton("Exit", 220, 111, 75, 25, 0)
$Button3 = GUICtrlCreateButton("Run", 10, 111, 75, 25, 0)
;~ $Label1 = GUICtrlCreateLabel("ISO COnvert", 0, 0, 35, 25, -1, $GUI_WS_EX_PARENTDRAG)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Global $file = ""

While 1
    $nMsg = GUIGetMsg()
    Select
        Case $nMsg = $GUI_EVENT_CLOSE Or $nMsg = $Button2
            Exit
        Case $nMsg = $Button1
            If GuiCtrlRead($Radio1) = $GUI_CHECKED Then
                  ; choose folder
                $folder = FileSelectFolder( "Open", "C:\")
                If @error Then ContinueLoop
                  ; in chosen folder, choose file
                $file = FileOpenDialog("Open", $folder,"All (*.*)")
                If @error Then ContinueLoop
                GUICtrlSetData($Input1, $file)
            ElseIf GuiCtrlRead($Radio2) = $GUI_CHECKED Then
                  ; choose file in C:\
                $file = FileOpenDialog("Open", "C:\","All (*.*)")
                If @error Then ContinueLoop
                GUICtrlSetData($Input1, $file)
            Else
                MsgBox(64,"Browse button","You must select something.")
            EndIf

    Case $nMsg = $Button3
          ; read file path from input
        $file = GUICtrlRead($Input1)
        If $file = "" Then ContinueLoop
          ; read content and convert
        $txt = FileRead($file)
        $MECs = StringRegExp($txt, '/MEC\w+', 3)
          ; you should put serious error checking right here (and further)
          ; If @error Then ... etc
        $out = "Isodraft" & @crlf & "options default" & @crlf
        For $i = 0 to UBound($MECs)-1
            $out &= '$M "%MECISO%/STD/D_sheet"' & @crlf & _
            $MECs[$i] & @crlf & _
           '$M "%MECISO%/STD/D_sheet"' & @crlf & _
           "detail" & $MECs[$i] & @crlf & @crlf
        Next
        $out &= "$."
           ; open dest file in write/overwrite mode
        $hFile = FileOpen(@desktopdir & "\New_Batch.txt", 2) 
        FileWrite($hFile, $out)
        FileClose($hFile)
    EndSelect
WEnd

 

Edited by mikell
Link to comment
Share on other sites

  • 3 weeks later...
#include <GUIConstants.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("ISO Convert", 303, 141, 193, 115)
$Input1 = GUICtrlCreateInput("", 21, 19, 201, 21)
$Button1 = GUICtrlCreateButton("Browse...", 235, 16, 64, 25, 0)
$Group1 = GUICtrlCreateGroup("Browse", 20, 44, 279, 63)
$Radio1 = GUICtrlCreateRadio("Folder", 26, 60, 113, 17)
$Radio2 = GUICtrlCreateRadio("File", 26, 78, 113, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Button2 = GUICtrlCreateButton("Exit", 220, 111, 75, 25, 0)
$Button3 = GUICtrlCreateButton("Run", 10, 111, 75, 25, 0)
;~ $Label1 = GUICtrlCreateLabel("ISO COnvert", 0, 0, 35, 25, -1, $GUI_WS_EX_PARENTDRAG)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Global $file = ""

While 1
    $nMsg = GUIGetMsg()
    Select
        Case $nMsg = $GUI_EVENT_CLOSE Or $nMsg = $Button2
            Exit
        Case $nMsg = $Button1
            If GuiCtrlRead($Radio1) = $GUI_CHECKED Then
                  ; choose folder
                $folder = FileSelectFolder( "Open", "C:\")
                If @error Then ContinueLoop
                  ; in chosen folder, choose file
                $file = FileOpenDialog("Open", $folder,"All (*.*)")
                If @error Then ContinueLoop
                GUICtrlSetData($Input1, $file)
            ElseIf GuiCtrlRead($Radio2) = $GUI_CHECKED Then
                  ; choose file in C:\
                $file = FileOpenDialog("Open", "C:\","All (*.*)")
                If @error Then ContinueLoop
                GUICtrlSetData($Input1, $file)
            Else
                MsgBox(64,"Browse button","You must select something.")
            EndIf

    Case $nMsg = $Button3
          ; read file path from input
        $file = GUICtrlRead($Input1)
        If $file = "" Then ContinueLoop
          ; read content and convert
        $txt = FileRead($file)
        $MECs = StringRegExp($txt, '/MEC\w+', 3)
          ; you should put serious error checking right here (and further)
          ; If @error Then ... etc
        $out = "Isodraft" & @crlf & "options default" & @crlf
        For $i = 0 to UBound($MECs)-1
            $out &= '$M "%MECISO%/STD/D_sheet"' & @crlf & _
            $MECs[$i] & @crlf & _
           '$M "%MECISO%/STD/D_sheet"' & @crlf & _
           "detail" & $MECs[$i] & @crlf & @crlf
        Next
        $out &= "$."
           ; open dest file in write/overwrite mode
         If not FileExists ("C:\Users\Public\Documents\BatchPlots") then
            DirCreate ("C:\Users\Public\Documents\BatchPlots")
            EndIf
        $hFile = FileOpen("C:\Users\Public\Documents\BatchPlots" & "\NewBatch", 2)
        FileWrite($hFile, $out)
        FileClose($hFile)
        Sleep (2500)
        #Region --- CodeWizard generated code Start ---
        ;MsgBox features: Title=No, Text=Yes, Buttons=OK, Icon=None
        MsgBox(0,"","Converted!! Click OK to Open The Batch File Location")
        Run("explorer.exe " & "C:\Users\Public\Documents\BatchPlots")

      Sleep (2500)

   ProcessClose("BatchScript.exe")


    EndSelect
WEnd

What is the best way to make the newly created file read from the $file = GUICtrlRead($Input1) input box and name it according to the to what is selected? This what I currently have...

 $hFile = FileOpen("C:\Users\Public\Documents\BatchPlots" & "\NewBatch", 2)
        FileWrite($hFile, $out)
        FileClose($hFile)

 

Thank you very much for the help...

 

Edited by wklw2010
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...