BAT or VB file help

Page 3 of 3 FirstFirst 123

  1. Posts : 10,485
    W7 Pro SP1 64bit
       #21

    I'll be away from this forum for several hours - silly real world :-(
      My Computer


  2. Posts : 21
    Windows 7 Ultimate x64
    Thread Starter
       #22

    each file contain more than 100 lines.
    Starting with this:
    ABAN,20130611,09:16:00,291.15,292
    ABAN,20130611,09:17:00,292.35,292
    ABAN,20130611,09:18:00,292.5,292

    I get this when I run the script above:
    ABAN.fm,20130611,09:16:00,291.15,292
    ABAN.fm,20130611,09:17:00,292.35,292
    ABAN.fm,20130611,09:18:00,292.5,292.fm

    .fm adds at the end of the file.i want to remove that.
    Pls use the attached text file.
    BAT or VB file help Attached Files
      My Computer


  3. Posts : 10,485
    W7 Pro SP1 64bit
       #23

    That is caused by an empty line at the very bottom of the original text file. I did not see that in my testing since I did not have an empty line there. See the line of code in red below - that should fix things.

    Code:
    ;AutoIt Version: 3.3.8.1
    
    $search = FileFindFirstFile("*.txt")
    If $search = -1 Then
        MsgBox(0, "Error", "No files/directories matched the search pattern")
        FileClose($search)
        Exit
    EndIf
    
    While 1
        $file = FileFindNextFile($search)
        If @error Then ExitLoop
    
        $whole_file = FileRead($file)
        $file_handle = FileOpen($file, 2)
        $new_file_info = ""
        $sentence_array = StringSplit($whole_file, @CRLF, 1)
    
        For $i = 1 To $sentence_array[0]
            If $sentence_array[$i] = "" Then ContinueLoop
            $word_array = StringSplit($sentence_array[$i], ",", 1)
            For $ii = 1 To $word_array[0]
                If $ii = 1 Then $word_array[$ii] &= ".fm"
                If $ii = $word_array[0] Then
                    If $i = $sentence_array[0] Then
                        $new_file_info &= $word_array[$ii]
                    Else
                        $new_file_info &= $word_array[$ii] & @CRLF
                    EndIf
                Else
                    $new_file_info &= $word_array[$ii] & ","
                EndIf
            Next
        Next
        FileWrite($file_handle, $new_file_info)
        FileClose($file_handle)
    WEnd
    FileClose($search)
    MsgBox(0, "AutoIt", "Finished!")
      My Computer


  4. Posts : 21
    Windows 7 Ultimate x64
    Thread Starter
       #24

    Thank you very much for the script. I use our first script,"Remove last four lines" to solve the issue but you use only a single line to solve it,Great.You are awesome.
      My Computer


  5. Posts : 10,485
    W7 Pro SP1 64bit
       #25

    You are welcome.

    In the unlikely event that someone else want to modify this script for their use, I'll include a slightly better version. The version below would preserve any blank lines that might be in the the main part of the file. If you do not have (or do not want to keep) blank lines, then you don't need this version.

    Input would be:
    ABAN,20130612,09:16:00,281,282.8,281,282.3,686

    ABAN,20130612,09:17:00,282.8,284.9,282.8,283.95,946
    ABAN,20130612,09:18:00,283.9,284.1,283,283,587

    Output would be:
    ABAN.fm,20130612,09:16:00,281,282.8,281,282.3,686

    ABAN.fm,20130612,09:17:00,282.8,284.9,282.8,283.95,946
    ABAN.fm,20130612,09:18:00,283.9,284.1,283,283,587

    The second and last lines would remain blank.

    Again - you probably don't need/want blank lines in the main part of the file, but the intent of the code is to not modify the input file beyond what was originally specified (e.g. add ".fm" to the first word of each line). If the line is blank, then there is no first word and ".fm" should not be added to blank lines.

    Code:
    ;AutoIt Version: 3.3.8.1
    
    $search = FileFindFirstFile("*.txt")
    If $search = -1 Then
        MsgBox(0, "Error", "No files/directories matched the search pattern")
        FileClose($search)
        Exit
    EndIf
    
    While 1
        $file = FileFindNextFile($search)
        If @error Then ExitLoop
    
        $whole_file = FileRead($file)
        $file_handle = FileOpen($file, 2)
        $new_file_info = ""
        $sentence_array = StringSplit($whole_file, @CRLF, 1)
    
        For $i = 1 To $sentence_array[0]
            If $sentence_array[$i] = "" Then
                If $i < $sentence_array[0] Then $new_file_info &= @CRLF
                ContinueLoop
            EndIf
            $word_array = StringSplit($sentence_array[$i], ",", 1)
            For $ii = 1 To $word_array[0]
                If $ii = 1 Then $word_array[$ii] &= ".fm"
                If $ii = $word_array[0] Then
                    If $i = $sentence_array[0] Then
                        $new_file_info &= $word_array[$ii]
                    Else
                        $new_file_info &= $word_array[$ii] & @CRLF
                    EndIf
                Else
                    $new_file_info &= $word_array[$ii] & ","
                EndIf
            Next
        Next
        FileWrite($file_handle, $new_file_info)
        FileClose($file_handle)
    WEnd
    FileClose($search)
    MsgBox(0, "AutoIt", "Finished!")
    I've looked at lots of AutoIt code that was written by programmers - compared to their code, mine is usually a mess. They know ways to do stuff that is just better and cleaner looking. Since programming is not my job, I just keep messing with the code until it does what I want.

    As you see flaws in the output file, you try and add code to fix those flaws. When you look at the final code, it may look messy and hard to understand. But if you watched it develop, it would be a bit easier to see why each section is there and what it does.
      My Computer


 
Page 3 of 3 FirstFirst 123

  Related Discussions
Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 01:31.
Find Us