Sign in to follow this  
Followers 0
Dieuz

Text file copy

13 posts in this topic

#1 ·  Posted (edited)

Hi,

I was wondering if there is any code outhere that will do the following things:

Let's say I have a 1000 .txt file with text in it. I want to run a code that will copy every text in those files and paste it all in 1 big .txt file.

If nobody made a code for that, I might start one.

Thanks in advance,

Edited by Dieuz

Share this post


Link to post
Share on other sites



FileRead

FileWrite

_FileListToArray

Share this post


Link to post
Share on other sites

Thanks Jardenix, how long would it take to copy let's say 1000 files with 10k words in each one.

Share this post


Link to post
Share on other sites

I'm not sure, just a minute...

Share this post


Link to post
Share on other sites

kk I will test it right now! Thanks alot again!

Share this post


Link to post
Share on other sites

It took me around 7.6 seconds. And sorry, its #Include <File.au3> not Misc! :)

Share this post


Link to post
Share on other sites

This is the example I used:

#Include <File.au3>

Global $String, $H_GetTextFiles

DirCreate(@ScriptDir & "\Test")

For $I = 1 To 10000
    $String &= 1
Next

For $I = 1 To 1000
    FileWrite(@ScriptDir & "\Test\" & $I & ".txt", $String)
Next

_TextFileMerger(@ScriptDir & "\Test", @ScriptDir & "\Test\All.txt")

Func _TextFileMerger($H_FolderPath, $H_OutPutFilePath, $V_ExecuteWhenDone = 0)
    ;$Timer = TimerInit()
    $H_GetTextFiles = _FileListToArray($H_FolderPath, "*.txt")
    For $I = 1 To $H_GetTextFiles[0]
        $S_GetTextFileData = FileRead($H_FolderPath & "\" & $H_GetTextFiles[$I])
        FileWrite($H_OutPutFilePath, "   ***" & $H_GetTextFiles[$I] & "***" & @CRLF)
        FileWrite($H_OutPutFilePath, $S_GetTextFileData)
        FileWrite($H_OutPutFilePath, @CRLF & "***** Text File Divider *****" & @CRLF)
     Next
     ;MsgBox(0, "", TimerDiff($Timer) / 1000)
     If $V_ExecuteWhenDone = 1 Then ShellExecute($H_OutPutFilePath)
EndFunc

For $I = 1 To $H_GetTextFiles[0]
    FileDelete(@ScriptDir & "\Test\" & $H_GetTextFiles[$I])
Next

Share this post


Link to post
Share on other sites

Haha right!

#Include <Misc.au3>
#include <File.au3>

$H_FolderPath = @SCRIPTDIR & "\test"
$H_OutPutFilePath = @SCRIPTDIR & "\file.txt"


Func _TextFileMerger($H_FolderPath, $H_OutPutFilePath, $V_ExecuteWhenDone = 0)
     $H_GetTextFiles = _FileListToArray($H_FolderPath, "*.txt")
     For $I = 1 To $H_GetTextFiles[0]
          $S_GetTextFileData = FileRead($H_FolderPath & "\" & $H_GetTextFiles[$I])
          FileWrite($H_OutPutFilePath, "   ***" & $H_GetTextFiles[$I] & "***" & @CRLF)
          FileWrite($H_OutPutFilePath, $S_GetTextFileData)
          FileWrite($H_OutPutFilePath, @CRLF & "***** Text File Divider *****" & @CRLF)
      Next
     If $V_ExecuteWhenDone = 1 Then ShellExecute($H_OutPutFilePath)
EndFunc

Cant get this to work properly, any idea?

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

You could do this the old fashioned way using command line copy (see copy /?).

Concatenation is performed when multiple source files are seperated with a plus symbol or you can use wildcards.

Merge all txt files into output.txt:

copy *.txt output.txt

Merge only certain text files:

copy text1.txt+text2.txt+text3.txt output.txt

Of course you could easily generate a list of files to concatenate in AutoIt and use RunWait() to perform the copy.

Edited by weaponx

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

#include <File.au3>

_TextFileMerger(@ScriptDir & "\Test", @ScriptDir & "\All Files.txt")

Func _TextFileMerger($H_FolderPath, $H_OutPutFilePath, $V_ExecuteWhenDone = 0)
     $H_GetTextFiles = _FileListToArray($H_FolderPath, "*.txt")
     For $I = 1 To $H_GetTextFiles[0]
          $S_GetTextFileData = FileRead($H_FolderPath & "\" & $H_GetTextFiles[$I])
          FileWrite($H_OutPutFilePath, "   ***" & $H_GetTextFiles[$I] & "***" & @CRLF)
          FileWrite($H_OutPutFilePath, $S_GetTextFileData)
          FileWrite($H_OutPutFilePath, @CRLF & "***** Text File Divider *****" & @CRLF)
      Next
     If $V_ExecuteWhenDone = 1 Then ShellExecute($H_OutPutFilePath)
EndFunc

Edited by Jardenix

Share this post


Link to post
Share on other sites

Thanks Jardenix, appreciate your help alot!

Share this post


Link to post
Share on other sites

Your welcome, I'm also working on a _TextFileUnMerger().

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