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
UsernameIssues

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
thanku

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
UsernameIssues

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
thanku

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
UsernameIssues

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




Similar help and support threads
Thread Forum
File Open or Choose File dialog hangs application after selection made
Hi. Windows 7 Pro SP1 64 bit Intel i5 CPU @2.67 GHz 8 GB RAM This problem just started recently or I only noticed it in the past week or so. When I'm in any application that launches the File Open dialog, including any browser's "Select File/Browse" button to choose a local file, when I...
General Discussion
when open a word file or pwrpnt file another file is opens
God bless you, I am not a computer person please help me to fix the problem I have. Every time I open a word, power point and note pad program another file is opens with that (with a infinity sign in the front with the same extension ) is that a thread or how can I stop this thank you...
Microsoft Office
CBS.log file: Getting a usable txt file and interpreting the file
First, what does "CBS" mean in cbs.log? Does the cbs.log file show only errors? I create a cbs.log file a lot, so if there are errors, I'd like to correct them. However, my computer seems to be working just fine. sfc /scannow shows everything is OK. I've tried findstr /c:""...
Performance & Maintenance
hidden file of docx file created with ~$name of file.docx file
whenever I open docx file there is hidden file ~$name of file.docx created. which type of file is that? Is it in virus, maleware. for example If I open house.docx file then ~$house.docx file created which is hidden form. I had saw this file when i select show all hidden file option. when i try to...
Microsoft Office
Batch File that grabs file names from folder to insert into batch file
Ok I'm totally at a loss on how to word this so I'm just going to write it out the best that I can... :confused: First I have a batch file that I'm writing that will convert mp4 files to mp3 files (I have a lot) for a buddies band using ffmpeg. Here's the code that I'm using: ffmpeg -i...
General Discussion
How do I change a file's file type (not only file association)?
I need to change a zip file to an epub file. I have heard this can be done, but when I add the .epub after the file name, I notice the file is still actually zip type file when I right click properties. This is causing a problem when I try to open it in in different ebook reader programs. I...
General Discussion


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:47.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App