XYplorer: Concatenating Files

Today I’m going to share an XYplorer script for combining files through concatenation - i.e. merging them together by appending the files to one another in order to create a new combined file. Whilst it isn’t the sort of script I use everyday it does speed up the occasions when I need to combine plain text based files into a single file; in my case often for post-test data loading.

How the script works

The first step in the script captures the number of files that have been selected and stores it in a variable called $itemCount. This will be used immediately, but we’ll also be making use of it later on as well. The first thing we use it for is simply to check that we have an appropriate file selection for concatenation. If there are no files or just one file selected then we have nothing to concatenate and a warning message is displayed to the user.

Assuming we do have two or more files selected the script then begins checking for modifier keys. When the script is run it checks on what modifier keys are being held down and then modifies its operation accordingly.

Holding down the SHIFT key will add a new line (carriage return + line feed) between each file; not holding it down will mean the script does not put any separator between the files. This can be useful for scenarios where your files may or may not have trailing new lines. Simply choose the modifier as appropriate.

Holding down the CTRL key will prompt the user to enter a file extension for the concatenated file (default is TXT). Not holding down the CTRL key means that the script will default to the file extension of the first file in the selection. This is particularly useful where you have files of varying file extension to concatenate or where you need to change the file extension of the concatenated file.

The first part of the script builds a list of the file paths. This will be based on the order they are displayed in XYplorer and so you can change the ordering of the list by sorting the columns in XYplorer - e.g. by name, by modified date; and of course the sort order is applied.

Note the ALT key has no effect and if you happen to press CTRL + ALT + SHIFT, CTRL + ALT or SHIFT + ALT, the script does not handle those combinations. One day I may use the ALT key for another option like selecting the output location, but until then I’m explicitly leaving out any of these combinations.

Once the results of the modifier keys have been stored, the next step is to populate some variables for the output file path and to initialise an index variable that is used to count how many of the files have been concatenated as we are looping though the selection. The file path is set to the same folder as the other files with the current time stamp as the file name and the file extension as specified earlier through the modifier key selection. The index variable is simply set to zero.

Now with all the preparations in place, the final stage of the script is to produce the output file. The script does this by looping through all of the selected files, reading in their content, and appending the content out to the output file. Note that if the SHIFT modifier key is pressed then the script will insert a new line after each of the files, except for the last one. The index variable is compared to the indexCount variable to ensure that the last file does not have a new line added to it, thus avoiding the addition of an extraneous blank line to the end of the concatenated file. Once the file is generated a message regarding the file is placed in the XYplorer status bar.

The Script

Below is the XYplorer script. I added mine to a script library associated with a button, but you can of course deploy it in a number of ways.

//Combine selected files in order
"Concatenate Files"
    $itemCount = get("CountSelected");
    if ($itemCount < 2){
        msg "Please select two or more files to concatenate.", 0 + 48;
    }
    else {
        //Set variables by modifier keys
        if (get("shift") == 0) {
            //No modifier keys pressed - use defaults
            $fileSeparator = "";
            $outputFileExt = getpathcomponent("<selitem>", "ext");
        }
        if (get("shift") == 1) {
            //SHIFT key pressed - add new line between files
            $fileSeparator = "<crlf>";
            $outputFileExt = getpathcomponent("<selitem>", "ext");
        }
        if (get("shift") == 2) {
            //CTRL key pressed - ask user for file extension, default to TXT
            $fileSeparator = "";
            $outputFileExt = input("Enter the file extension you wish to use", "Enter the extension without the preceding dot." , "txt");
        }
        if (get("shift") == 2) {
            //CTRL + SHIFT key pressed - add new line between files and ask user for file extension, default to TXT
            $fileSeparator = "<crlf>";
            $outputFileExt = input("Enter the file extension you wish to use", "Enter the extension without the preceding dot." , "txt");
        }
        
        //Initialise variables used in loop
        $outputFilePath = <curpath> . "\" . <date yyyy-mm-dd-hh-mm-ss> . ".$outputFileExt";
        $index = 0;

        //Process files
        foreach($token, <get selecteditemspathnames |>) {
            $index = $index + 1;
            $fileContent = readfile("$token");
            //If we're on the final file then don't append any file separator
            if ($index == $itemCount) {
                writefile("$outputFilePath", $fileContent, "a");
            }
            else {
                writefile("$outputFilePath", $fileContent . $fileSeparator, "a");
            }
        }
        status "Concatenated file generation complete - $outputFilePath";
    }

Summary

That’s all there is to it. You can create an extremely simple script to do file concatenation but I find the options offered by the modifier keys and the small bit of error handling to be extremely helpful. It’s still a relatively simple script but it can really save a lot of time or dealing with the slightly surprising outputs using COPY statements on the command line can produce on occasion.

If you want to keep up with me and my posts on topics like using XYplorer please consider following me on Twitter where I post as @sylumer; and of course if you found this post useful do please pay it forward by sharing with your friends, colleagues and social media followers.

Author: Stephen Millard
Tags: | scripting | xyplorer |

Buy me a coffeeBuy me a coffee



Related posts that you may also like to read