Sign in to follow this  
Followers 0
Recce

Merge all TXT files in a folder into one file?

7 posts in this topic

Hi all,

I have a folder that contains TXT files. All TXT files have random names and lines. I need to merge all files into one new TXT file named LPF_TXT.TXT.

My problem is I dont know the names of the TXT files. But it is all the TXT files in this directory C:\Pfps\Data\locals

Is this possible?

/Thomas

Share this post


Link to post
Share on other sites



Hi all,

I have a folder that contains TXT files. All TXT files have random names and lines. I need to merge all files into one new TXT file named LPF_TXT.TXT.

My problem is I dont know the names of the TXT files. But it is all the TXT files in this directory C:\Pfps\Data\locals

Is this possible?

/Thomas

As the previous poster stated, you can list all files to the array and then process them:

#Include <File.au3>
#Include <Array.au3>
$fileout = @DesktopDir&"\out.txt"
if FileExists($fileout) then FileDelete($fileout)
$file = FileOpen($fileout, 1)

$FileList=_FileListToArray(@SystemDir, "*.txt")
If @Error=1 Then
    MsgBox (0,"","No Files\Folders Found.")
    Exit
EndIf

for $i = 1 to $FileList[0]
    $data = FileRead(@SystemDir&"\"&$FileList[$i])
    FileWrite($file, "-------------------- File:  "&$FileList[$i]&"--------------------------------"&@crlf)
    FileWrite($file, $data&@CRLF)
Next
FileClose($file)
ShellExecute($fileout)

-_-------__--_-_-____---_-_--_-__-__-_ ^^€ñ†®øÞÿ ë×阮§ wï†høµ† ƒë@®, wï†høµ† †ïmë, @ñd wï†høµ† @ †ïmïdï†ÿ ƒø® !ïƒë. €×阮 ñø†, bµ† ïñ§†ë@d wï†hïñ, ñ@ÿ, †h®øµghøµ† †hë 맧ëñ§ë øƒ !ïƒë.

Share this post


Link to post
Share on other sites

Thank you all!

zackrspv I do not understand the last part of the code:

for $i = 1 to $FileList[0]
    $data = FileRead(@SystemDir&"\"&$FileList[$i])
    FileWrite($file, "-------------------- File:  "&$FileList[$i]&"--------------------------------"&@crlf)
    FileWrite($file, $data&@CRLF)
Next
FileClose($file)
ShellExecute($fileout)

Care to explain?

:)

Share this post


Link to post
Share on other sites

. . . I do not understand the last part of the code: . . .

It's a For...Next loop that's doing what you asked. It would be best to search the Help file for the things that don't make sense. Start with "For...Next", FileRead, FileWrite, etc.

Basically the loop does this:

  • reads the contents of a specific txt file into memory
  • writes "---- File: <filename being read/written> ----"
  • writes the contents of the txt file
  • loops to NEXT file in list (array)

Share this post


Link to post
Share on other sites

Thank you all!

This is great help.

:)

/Thomas

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Thank you all!

zackrspv I do not understand the last part of the code:

for $i = 1 to $FileList[0]
    $data = FileRead(@SystemDir&"\"&$FileList[$i])
    FileWrite($file, "-------------------- File:  "&$FileList[$i]&"--------------------------------"&@crlf)
    FileWrite($file, $data&@CRLF)
Next
FileClose($file)
ShellExecute($fileout)

Care to explain?

:)

AS a further explanation:

;- Includes file handlers
#include <File.au3>

;- Includes Array handlers
#include <Array.au3>

;- Sets the file to output all text files too
$fileout = @DesktopDir & "\out.txt"

;- Identifies if file exists, if it does, delete it.  (this is optional)
If FileExists($fileout) Then FileDelete($fileout)

;- Sets the file to be opened w/ Append to End (add all new content at end of file)
$file = FileOpen($fileout, 1)

;- Lists all of the files in the specified directory w/ the *.txt extension into the $FileList Array
$FileList = _FileListToArray(@SystemDir, "*.txt")

;- If the files do not exist
If @error = 1 Then

;- Provide feedback
    MsgBox(0, "", "No Files\Folders Found.")

;- Exit script
    Exit
EndIf


;-  contains the # of elements in the array (Number of Files) and you start at 1 because element 0 is a count
For $i = 1 To $FileList[0];- $FileList[0]

;- Read each file into memory starting at the specified array element
    $data = FileRead(@SystemDir & "\" & $FileList[$i])

;- Write a header into the file identifying which file is being written
    FileWrite($file, "-------------------- File:  " & $FileList[$i] & "--------------------------------" & @CRLF)

;- Write the remainder of the file from memory into the output file under the header
    FileWrite($file, $data & @CRLF)

;- Process next file, when the loop hits the last element, this closes the loop
Next

;- Closes the output file
FileClose($file)

;- Starts your shell's default text editor to see the text file.
ShellExecute($fileout)

Note here, You do not need to delete the $fileOut file if you do not wish too. For example, if you are needing to run this command whenever, like monthly

or whatever, and you want all of those files to continually add to the end of the previously created file, you can comment out this line, or remove it

completely, and all processed files will remain being added to the end of the file. (Note, this will increase the file size extremely, however.)

Note, also, that the memory usage of this script can be astronomical as well, considering that you are READING the CONTENTS of the file into memory in the

for/next loop. If it's a huge file, then this could take some time. But, it is what you asked for.

You could, potentially, also create the ability to run this as a service and monitor the specified folder for files, and then add them, as they are created

at the end of the $fileOut file. To do this, you would simply set this program to run as a service (see the various UDF's on this in the forum), and then

change your code to reflect the following:

#include <File.au3>
#include <Array.au3>
$fileout = @DesktopDir & "\out.txt"
If FileExists($fileout) Then FileDelete($fileout)

While 1
    $FileList = _FileListToArray(@SystemDir, "*.txt")
    If @error = 1 Then
        $FileList = ""
        Sleep(5000)
    Else
        $file = FileOpen($fileout, 1)
        For $i = 1 To $FileList[0]
            $data = FileRead(@SystemDir & "\" & $FileList[$i])
            FileWrite($file, "-------------------- File:  " & $FileList[$i] & "--------------------------------" & @CRLF)
            FileWrite($file, $data & @CRLF)
        Next
        $data = ""
        FileClose($file)
    EndIf
WEnd

Note some differences here. We have encapsulated the code within a While/Wend loop, with the While being equal to 1. This allows us to continually

run this code as long as the program is open, and the status is equal to 1. As we have nothing changing the status from 1 to anything else, this will

essentially run the program forever.

Note, also that, if there are no files found, then it will clear the array, and then sleep for 5000 miliseconds. You can increase this time delay

if you wish, so that you are no longer forcing the CPU to constantly run the commands. It will decrease the amount of CPU usage that exists saving

your computer heacaches in the future.

After each process, it closes the output file, and frees up your memory. Ie $data = "".

Hope that gives you much more idea of how powerful and detailed that this language can be for you.

Edited by zackrspv

-_-------__--_-_-____---_-_--_-__-__-_ ^^€ñ†®øÞÿ ë×阮§ wï†høµ† ƒë@®, wï†høµ† †ïmë, @ñd wï†høµ† @ †ïmïdï†ÿ ƒø® !ïƒë. €×阮 ñø†, bµ† ïñ§†ë@d wï†hïñ, ñ@ÿ, †h®øµghøµ† †hë 맧ëñ§ë øƒ !ïƒë.

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  
Followers 0