Solved BAT or VB file help

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

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Employer provided Dell Latitude
OS
W7 Pro SP1 64bit
CPU
i7
Memory
8GB
Graphics Card(s)
Intel HD Graphics
Hard Drives
crappy SSD
Antivirus
Employer mandated Symantec Endpoint Protection
Browser
Pale Moon 64bit, IE11 64bit & Chrome 64bit
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.
 

Attachments

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Dell
OS
Windows 7 Ultimate x64
CPU
intel
Motherboard
intel
Memory
8 GB
Graphics Card(s)
2 GB
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]
        [COLOR=Red]If $sentence_array[$i] = "" Then ContinueLoop[/COLOR]
        $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

Computer type
Laptop
Computer Manufacturer/Model Number
Employer provided Dell Latitude
OS
W7 Pro SP1 64bit
CPU
i7
Memory
8GB
Graphics Card(s)
Intel HD Graphics
Hard Drives
crappy SSD
Antivirus
Employer mandated Symantec Endpoint Protection
Browser
Pale Moon 64bit, IE11 64bit & Chrome 64bit
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

Computer type
Laptop
Computer Manufacturer/Model Number
Dell
OS
Windows 7 Ultimate x64
CPU
intel
Motherboard
intel
Memory
8 GB
Graphics Card(s)
2 GB
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]
        [COLOR=Red]If $sentence_array[$i] = "" Then
            If $i < $sentence_array[0] Then $new_file_info &= @CRLF
            ContinueLoop
        EndIf[/COLOR]
        $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

Computer type
Laptop
Computer Manufacturer/Model Number
Employer provided Dell Latitude
OS
W7 Pro SP1 64bit
CPU
i7
Memory
8GB
Graphics Card(s)
Intel HD Graphics
Hard Drives
crappy SSD
Antivirus
Employer mandated Symantec Endpoint Protection
Browser
Pale Moon 64bit, IE11 64bit & Chrome 64bit
Back
Top