Jump to content
Sign in to follow this  
ennis

merge text files

Recommended Posts

ennis

Im trying to merge some text files into a new empty text file..some are a few megs others smaller. My code doesnt seem to work. It only adds carriage returns into the new file. I've tried the _fileappend function as well..it does the same thing mine does.

here it is (btw the array contents or size really doesnt matter as long as it has an entry for each file which it does):

Local $regHandle = fileopen("C:\reg.txt", 1)
    for $count = 0 to ubound($regArray) - 1
        if fileexists('C:\temp\reg' & $count & '.tmp') then
            Local $tmpregHandle = fileopen('C:\temp\reg' & $count & '.tmp', 0)
            Local $regString 
            while 1
                $regString = filereadLine($tmpregHandle)
                if @ERROR = -1 then EXITLOOP
                filewriteLine($regHandle, $regString)
            WEND
            fileclose($tmpregHandle)
        else
            msgbox(4096, "Error", "File " & $count & " does not exist!")
        endif
    Next
    fileclose($regHandle)

and ive also tried this

Local $regHandle = fileopen("C:\reg.txt", 1)
    for $count = 0 to ubound($regArray) - 1
        if fileexists('C:\temp\reg' & $count & '.tmp') then
            Local $tmpregHandle = fileopen('C:\temp\reg' & $count & '.tmp', 0)
            Local $regString = fileread($tmpregHandle, filegetsize('C:\temp\reg' & $count & '.tmp'))
            filewrite($regHandle, $regString)
            fileclose($tmpregHandle)
        else
            msgbox(4096, "Error", "File " & $count & " does not exist!")
        endif
    Next
    fileclose($regHandle)

not sure why it wont work..my best guess is maybe its too big of a string but the line by line version should have fixed that. Well thnx if u figure it out.

Ennis

Edited by ennis

Share this post


Link to post
Share on other sites
automagician

Try something along these lines:

#include <file.au3>

    Local $regHandle = fileopen("C:\reg.txt", 1)
    for $count = 0 to ubound($regArray) - 1
        if fileexists('C:\temp\reg' & $count & '.tmp') then
            $file = 'C:\temp\reg' & $count & '.tmp'
            Local $tmpregHandle = fileopen($file, 0)
            Local $regString 
            
            $tot_lines = _FileCountLines($file)
            $i = 1

            While $i <= $tot_lines
                $regString = filereadLine($tmpregHandle, $i)
                if @ERROR = -1 then EXITLOOP
                filewriteLine($regHandle, $regString)
                $i = $i + 1
            WEnd
            fileclose($tmpregHandle)
        else
            msgbox(4096, "Error", "File " & $count & " does not exist!")
        endif
    Next
    fileclose($regHandle)

I didn't test the code, but it should work....When you are doing the FileReadLine, you need to specify what line.

***_FileCountLines needs file location, not handle, sorry...I just updated the snipit above.

Edited by automagician

Share this post


Link to post
Share on other sites
zcoacoaz

you don't nned to specify the line but it helps in some cases


[font="Times"] If anyone remembers me, I am back. Maybe to stay, maybe not.----------------------------------------------------------------------------------------------------------[/font][font="Times"]Things I am proud of: Pong! in AutoIt | SearchbarMy website: F.R.I.E.S.A little website that is trying to get started: http://thepiratelounge.net/ (not mine)[/font][font="Times"] ----------------------------------------------------------------------------------------------------------[/font][font="Arial"]The newbies need to stop stealing avatars!!! It is confusing!![/font]

Share this post


Link to post
Share on other sites
automagician

Sorry about that, I used the the line number and it worked for me, I didn't mean to say *need*

Share this post


Link to post
Share on other sites
phillip123adams

Im trying to merge some text files into a new empty text file..some are a few megs others smaller.

<{POST_SNAPBACK}>

How about the COPY command. It allows contatenation of multiple files into a new file.

Example: COPY "file1"+"file2"+"file3" "file4"

Run(@comspec & ' /c COPY "' & $File1 & '"+"' & $File2 & '" "' & $File3 & '"')

Phillip

Share this post


Link to post
Share on other sites
ennis

How about the COPY command.  It allows contatenation of multiple files into a new file.

Example: COPY "file1"+"file2"+"file3" "file4"

Run(@comspec & ' /c COPY "' & $File1 & '"+"' & $File2 & '" "' & $File3 & '"')

<{POST_SNAPBACK}>

Thnx bud..the copy command did it..One weird thing is sometimes it adds a weird square character at the end -> -.maybe its a dos carriage return.not sure..but it doesnt on larger files...

I appreciate the help

Ennis

Edited by ennis

Share this post


Link to post
Share on other sites
Blue_Drache

Thnx bud..the copy command did it..One weird thing is sometimes it adds a weird square character at the end -> -.maybe its a dos carriage return.not sure..but it doesnt on larger files...

I appreciate the help

Ennis

<{POST_SNAPBACK}>

That's acutally the EOF marker. I sometimes see it as a left arrow <- (all one character though). Dependant on the font.... I use TERMINAL font for notepad. Lets me see all the old skewel DOS ASCII characters.... Edited by Blue_Drache

Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Share this post


Link to post
Share on other sites
phillip123adams

Thnx bud..the copy command did it..One weird thing is sometimes it adds a weird square character at the end -> -.maybe its a dos carriage return.not sure..but it doesnt on larger files...

I appreciate the help

Ennis

<{POST_SNAPBACK}>

You're welcome. Like Larry says, include the /b switch and that should solve that problem.

Phillip

Share this post


Link to post
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
Sign in to follow this  

×