Jump to content

Appending a HUGE number of text files


Styler001
 Share

Recommended Posts

I really struggled over whether I should dredge up a year-old topic that had some relevant suggestions, or starting up a new topic. I'm still not sure a new topic was the best way to go, but here it is anyway.

I've got between 200 and 300 text files that I want to merge into one. There is no commonality between the filenames, other than the TXT extension. Nothing like FILE1.TXT, FILE2.TXT, FILE3.TXT, etc. Each file pretty much has a completely different filename. This isn't the actual list, but filenames could be as different as CATS.TXT, DOGS.TXT, RACING.TXT, DIRT.TXT. So no way of simply saying something like:

Append fileX.txt through fileY.txt into fileZ.txt

They are, however, all in the same directory. I know I can accomplish the basic append through the DOS prompt, but that just crams all of the text files into one seemless file. I'd like to be able to put some kind of divider in between each file to differentiate between them so each "section" would be easier to recognize.

I've found a program called UUMERGE that will put a divider in place, but it's little better than the DOS copy I mentioned above. Is there a good way to do this in AutoIt?

Link to comment
Share on other sites

This should do the trick

#Include <File.au3>
$Location = "C:\"
$FileList = _FileListToArray($Location)

If @error = 1 Then
    MsgBox(0, "", "No Files\Folders Found.")
    Exit
EndIf

$Opener = FileOpen($Location & "All_Files.txt", 2) ; #2 replaces old text

For $x = 1 To UBound($FileList) - 1
    ToolTip("File #" & $x, 10, 10, "Tecter", 1)
    If StringInStr($FileList[$x], ".txt") Then
        FileWrite($Opener, FileRead($Location & $FileList[$x]))
        FileWrite($Opener, @CRLF & @CRLF & "************* divider **************** File Number = " & $x & @CRLF & @CRLF)
    EndIf
Next

FileClose($Opener)
ShellExecute($Location & "All_Files.txt")

8)

NEWHeader1.png

Link to comment
Share on other sites

Thanks, Siao, for the hints.

And thanks, Valuater, for the script. I wasn't expecting anyone to write one for me, but I do appreciate it. It's been awhile since I've done anything with AutoIt, so I was not looking forward to struggling through relearning what I might've forgotten. Your script works perfectly. Thanks again. :)

Link to comment
Share on other sites

  • 1 month later...

Isn't there an easier way to do this with autoit?

with batch code you can just run this code on a command line or a batch...

type *.txt >> "c:\backup\%username%.backup.txt"

type *.txt ;reads all text files

>> ;appends read files to %username%.backup.txt file

This should do the trick

#Include <File.au3>
$Location = "C:\"
$FileList = _FileListToArray($Location)

If @error = 1 Then
    MsgBox(0, "", "No Files\Folders Found.")
    Exit
EndIf

$Opener = FileOpen($Location & "All_Files.txt", 2) ; #2 replaces old text

For $x = 1 To UBound($FileList) - 1
    ToolTip("File #" & $x, 10, 10, "Tecter", 1)
    If StringInStr($FileList[$x], ".txt") Then
        FileWrite($Opener, FileRead($Location & $FileList[$x]))
        FileWrite($Opener, @CRLF & @CRLF & "************* divider **************** File Number = " & $x & @CRLF & @CRLF)
    EndIf
Next

FileClose($Opener)
ShellExecute($Location & "All_Files.txt")

8)

Edited by gfunk999
Link to comment
Share on other sites

  • Moderators

Isn't there an easier way to do this with autoit?

with batch code you can just run this code on a command line or a batch...

type *.txt >> "c:\backup\%username%.backup.txt"

type *.txt ;reads all text files

>> ;appends read files to %username%.backup.txt file

I would imagine your request to look something like this:

_DirFileAppend(@UserName & "\Au3Files\", "*.au3", @UserName & "\Au3Files\AU3.BACK.log", 1)

Func _DirFileAppend($sDir, $sMod, $sBackDir, $bWait = 0)
    If StringRight($sDir, 1) <> "\" Then $sDir &= "\"
    If StringLeft($sMod, 1) = "\" Then $sMod = StringTrimLeft($sMod, 1)
    If $bWait Then Return RunWait(@Comspec & ' /c type "' & _
        $sDir & $sMod &  '" >> "' & $sBackDir & '"', '', @SW_HIDE)
    Return  Run(@Comspec & ' /c type "' & _
        $sDir & $sMod &  '" >> "' & $sBackDir & '"', '', @SW_HIDE)
EndFunc

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Can we do it like this... I think it's a lot easier, but I may be wrong... It works

#include <Process.au3>
Run(@ComSpec & " " & "/c" & " " & "type" & " " & @TempDir & "\*.txt" & " " & ">>" & " " & "c:\backup\%username%.backup.txt", "", @SW_HIDE)

%username% is called from the built-in command environment variable or we could always replace it with @username

Edited by gfunk999
Link to comment
Share on other sites

  • Moderators

Can we do it like this... I think it's a lot easier, but I may be wrong... It works

#include <Process.au3>
Run(@ComSpec & " " & "/c" & " " & "type" & " " & @TempDir & "\*.txt" & " " & ">>" & " " & "c:\backup\%username%.backup.txt", "", @SW_HIDE)

%username% is called from the built-in command environment variable or we could always replace it with @username

Why is that easier... you've put unnecessary ampersands and limited yourself on where your output file would be.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...