Concatenate Text Files

This process will prompt the user to select multiple text files in a folder. It will then create a new text file by concatenating these selected files. Multiple files can be selected by holding down the ‘Ctrl’ or “Shift” button.

Display.SelectFiles Title:'Please select the files that will be concatenated' \
                    InitialDirectory:'' \
                    FileFilter:'*.txt' \
                    IsTopMost:False \
                    CheckIfFileExists:False \
                    SelectedFiles=> FilesToConcatenate \
                    ButtonPressed=> ButtonPressed

if ButtonPressed = 'Open' then
	loop foreach CurrentFile in FilesToConcatenate
		File.ReadText File: CurrentFile \
                    Encoding:File.TextFileEncoding.UTF8 \
                    Content=> CurrentFileContents

		File.GetPathPart File:CurrentFile  \
                    RootPath=> RootPath \
                    Directory=> Directory \
                    FileName=> FileName \
                    FileNameWithoutExtension=> FileNameWithoutExtension \
                    Extension=> Extension
		File.WriteText File: Directory+'\ConcatenatedFiles.txt'  \
                    TextToWrite: CurrentFileContents \
                    AppendNewLine:True \
                    IfFileExists:File.IfFileExists.Append \
                    Encoding:File.FileEncoding.Unicode
	end
end

Display.ShowMessage Title:'Process Completed!' \
                    Message:'Process "Concatenate Text Files" completed.' \
                    Icon:Display.Icon.None \
                    Buttons:Display.Buttons.OK \
                    DefaultButton:Display.DefaultButton.Button1 \
                    IsTopMost:False \
                    ButtonPressed=> ButtonPressed

In more detail:


The process will prompt the user to select the files that will be concatenated. If the user has selected files, the process will append the content of each file to the “ConcatenatedFiles.txt” file, that will reside in the same directory as the selected files.

Step 1: Prompt the User to Select Files

Prompt the user to select multiple text files in a folder.

Display.SelectFiles Title:'Please select the files that will be concatenated' \
                    InitialDirectory:'' \
                    FileFilter:'*.txt' \
                    IsTopMost:False \
                    CheckIfFileExists:False \
                    SelectedFiles=> FilesToConcatenate \
                    ButtonPressed=> ButtonPressed

Use the “Display.SelectFiles”action:

  1. Set the Dialog title.
  2. Set the “FileFilter” argument to ‘*.txt’ to make sure that only files with this extension will be available for selection.
  3. Store the list of files in the output variable named “FilesToConcatenate”.

Step 2: Concatenate the Selected Files

Check whether the user has selected files (i.e. if the user pressed the “Open” button). If so, read the content of each file and append it to a new or existing file.

if ButtonPressed = 'Open' then
    File.GetPathPart File:FilesToConcatenate[0] \
                    RootPath=> RootPath \
                    Directory=> Directory \
                    FileName=> FileName \
                    FileNameWithoutExtension=> FileNameWithoutExtension \
                    Extension=> Extension

    loop foreach CurrentFile in FilesToConcatenate
        File.ReadText File: CurrentFile \
                    Encoding:File.TextFileEncoding.UTF8 \
                    Content=> CurrentFileContents
		
        File.WriteText File: Directory+'\ConcatenatedFiles.txt' \
                    TextToWrite: CurrentFileContents \
                    AppendNewLine:True \
                    IfFileExists:File.IfFileExists.Append \
                    Encoding:File.FileEncoding.Unicode
	end
end
  1. Use the “if” statement to check whether the user has pressed the “Open” button.
  2. Use the “File.GetPathPart” action to get the directory where the selected files are. This is done by selecting a file from the list of files, named “FilesToConcatenate”, which is the output variable of the action in the first step. In our example, we select the first file of the list, i.e. “FilesToConcatenate[0]”.
  3. Use the “for each” loop to iterate through the list of files that have been selected, named “FilesToConcatenate”. In each iteration, the current file of the list will be stored into the “CurrentFile” loop variable. It will be read and its content will be appended to a new or existing file.
  4. Use the “File.ReadText” action to read the content of each file and store it in the output variable “CurrentFileContents”.
  5. Use the “File.WriteText” action to append the content that was read through the previous action to the “ConcatenatedFiles.txt”, that will reside in the same directory where the selected files are. If the file does not exist, this action will automatically create it.
    1. Set the “File” argument to the “Directory+’\ConcatenatedFiles.txt’”, where “Directory” is an output variable of the “File.GetPathPart” action.
    2. Set the “IfFileExists” argument to “Append”, in order to append the content to the end of the file’s existing content.
  6. Use the “end” statement to close the “loop foreach” block.
  7. Use another “end” statement to close the conditional “if” block.
  8. Finally, inform the user when the process is complete.