Script replacement for command Convert to Character Delimited

Script replacement for command Convert to Character Delimited

6,610548
Grand MasterGrand Master
6,610548

    Apr 26, 2015#1

    I wrote a script for command Convert to Character Delimited in menu Column which is not available as scripting or macro command.

    The script converts a file with fixed column (all lines have same length) to a CSV file. The script converts the active file. It can have any size as the conversion is done using Perl regular expression replaces and UltraEdit/UEStudio supports editing files of any size, even files with more than 4 GB.

    The script is listed on Macros & Scripts page with title Fixed column to CSV.

    It contains at top a small introduction. After downloading the file to any folder and adding it to the lists of scripts via Scripting - Scripts (traditional menus) or Advanced - All scripts (contemporary menus and ribbon mode), it is already ready for usage on a fixed column width file from menu Scripting (traditional menus) or Advanced - Play script (contemporary menus and ribbon mode) or the Script List view.

    The entire code for conversion is included in function FixedColumnToCSV in case of somebody needs such a function in larger scripts.

    On execution it asks by default the script user for a comma separated list of field widths and after some checks also for the separator as those two parameters can be entered also in dialog of command Convert to Character Delimited.

    Supported are by default just the separators \t for a horizontal tab character, comma and semi-colon. Other not so common separators can be added by modifying the IF condition in the script which checks the entered (or defined) separator.

    There are some differences to built-in command:
    • The script function does not support fixed column files containing already at end of each field value the separator.
      I did not add support for such files as this is very uncommon and a single Perl regular expression Replace All can be used in this case also for converting the fixed column file (back) to a CSV file.
    • The script function does not append the separator at end of each line as the built-in command of UE v22.0.0.58 (and all previous versions) do which I is in my opinion not right. Of course I reported this in my point of view not correct behavior as issue to IDM support by email.
    • The script function correctly encloses a field value containing 1 or more double quotes in double quotes with escaping each double quote being part of the field value which is not done by built-in command of UltraEdit/UEStudio.
    • And last the script function correctly encloses a field value containing 1 or more times the separator in double quotes, too.
    The script contains also some hints on what to change on this script to do a conversion of all fixed column files in a folder to CSV files as the initial event for writing this script was Batch Convert to Character Delimited.

    But the script is currently not completely written for running the replaces on all files of a folder instead of active file using Replace in Files command. The check for correct sum of field widths with first line of active file must be removed in case of modifying the script for converting all fixed column width files in a folder with same line lengths to CSV files, or one of those files is the active file on script execution. And of course the replaces executed on active document after inserting the vertical tab as temporary separator must be also changed to replace in files in addition to toggling the comments already present for this alternate usage of the script.

    Let me know with a post here if somebody needs a version of this script for conversion of all files in a folder instead of active file with asking script user for file type and directory path and is not capable to modify the script accordingly without my help.

    The line and block comments can be removed from entire script or function FixedColumnToCSV by running a replace all (from top of file) searching with Perl regular expression for ^ *//.+[\r\n]+|^ */\*[\s\S]+?\*/[\r\n]+| +//.+$ and using an empty replace string. The first part in this OR expression with three arguments matches entire lines containing only a line comment, the second part matches block comments, and third part matches line comments right to code. Removal of the comments makes the usage of this script or just the function in other scripts more efficient because of JavaScript interpreter has to interpret less characters and lines.
    Best regards from an UC/UE/UES for Windows user from Austria