Jump to content

Text file copy


 Share

Recommended Posts

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

Link to comment
Share on other sites

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

#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
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...