Windows 7 Forums
Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: BAT or VB file help


12 Jun 2013   #21

W7 Pro SP1 64bit
 
 

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

My System SpecsSystem Spec
.

12 Jun 2013   #22

Windows 7 Ultimate x64
 
 

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.


Attached Files
File Type: txt ABAN.txt (18.3 KB, 4 views)
My System SpecsSystem Spec
12 Jun 2013   #23

W7 Pro SP1 64bit
 
 

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 System SpecsSystem Spec
.


13 Jun 2013   #24

Windows 7 Ultimate x64
 
 

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 System SpecsSystem Spec
13 Jun 2013   #25

W7 Pro SP1 64bit
 
 

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 System SpecsSystem Spec
Reply

 BAT or VB file help




Thread Tools




Our Sites

Site Links

About Us

Find 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 02:34 AM.
Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App
  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33