Sign in to follow this  
Followers 0
DreamVB

Utility to joint text files.

4 posts in this topic

#1 ·  Posted (edited)

This is a small program I made to join text files, I am one of them people that has text files here and there on the desktop, but after a while you end of deleteing them cos your desktop is a mess, so I made this to allow me to join them into one big file. it has the option to also add option header, Well here is the code hope you find it usfull.

;Ben's Text fILE jOINTER
#include <FileConstants.au3>
#include <String.au3>

Local $Buffer
Local $Filename
Local $LineSep
Local $Outfile
Local $Count
Local $AddSep = False
Local $Parm
Local $fp
Local $i

Func GetFileData($Filename)
    Local $fp
    Local $data

    If FileExists($Filename) Then
        $fp = FileOpen($Filename, 0)
        $data = FileRead($fp)
        FileClose($fp)
    EndIf

    Return $data
    ;Clear up
    $data = Null
EndFunc   ;==>GetFileData

;Get command line count.
$Count = UBound($CmdLine)

;Filename seperaor line
$LineSep = _StringRepeat("-", 69)

;Check parm count
If ($Count > 1) Then

    ;Check if we are adding file seperator.
    $AddSep = StringUpper($CmdLine[1]) = "-H"

    ;Check for out file parm
    $Parm = StringUpper($CmdLine[$Count - 2])

    ;Check for -O out file parm.
    If ($Parm <> "-O") Then
        Exit
    Else
        ;The file the data will be written to.
        $Outfile = $CmdLine[$Count - 1]
    EndIf

    ;If old file is found delete it.
    If FileExists($Outfile) Then
        FileDelete($Outfile)
    EndIf

    ;Open output file
    $fp = FileOpen($Outfile, $FO_APPEND)

    For $i = 1 To $Count - 2
        $parm = StringUpper($CmdLine[$i])
        If ($parm <> "-H") And ($parm <> "-O") Then
            ;Get filename
            $Filename = $CmdLine[$i]
            If FileExists($Filename) Then
                If ($AddSep = True) Then
                    ;Add sep
                    FileWrite($fp, $LineSep & @CRLF)
                    ;Add filename
                    FileWrite($fp, $Filename & @CRLF)
                    ;Add seperator string
                    FileWrite($fp, $LineSep & @CRLF)
                EndIf
                ;Get file contents
                $Buffer = GetFileData($CmdLine[$i])
                ;Append to the file
                FileWrite($fp, $Buffer)
                $Buffer = Null
            EndIf
        EndIf
    Next
    ;Close output file
    FileClose($fp)
EndIf

Example to join files with header.

JoinText.exe -h "c:\out\test.ini" "c:\out\test.txt" "C:\out\tasks.txt" -o "join.txt"

To joing without header just remove the -h

Edited by DreamVB

On Error Resume Pulling Hair Out.

Share this post


Link to post
Share on other sites



Run(@ComSpec & " /c copy /b *.txt all.txt")

:P

1 person likes this

Share this post


Link to post
Share on other sites
Run(@ComSpec & " /c copy /b *.txt all.txt")

:P

Thanks usfull tip to remmber, but also is there a way to use that to add headers like my code does.


On Error Resume Pulling Hair Out.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

is there a way to use that to add headers like my code does.

 

Yes...

RunWait(@ComSpec & " /c for %I in (*.txt) do @echo %~nxI >>all.txt >>all.txt && @echo %~tI >>all.txt && type ""%~nxI"" >>all.txt && @echo. >>all.txt && @echo. >>all.txt")
Edited by Werty

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