Format input TXT file to output CSV file

Discuss and share scripts and script files...
Post Reply
hermhart
Posts: 244
Joined: 13 Jan 2015 18:41

Format input TXT file to output CSV file

Post by hermhart »

Could someone help with a script that would take a selected .txt file and turn the sections of it into a comma delimited .csv file so as to open it up in Excel? The blocks of text in the .txt file would look something like:

&LOG Part: "blah blah blah.file"
&LOG Cloning_Action: DEFAULT_DISP Naming_Technique: DEFAULT_NAMING Clone_Name: "@a/file/-"
&LOG Container: ""
&LOG Part_Type: BLAH
&LOG Part_Name: The new number
&LOG Part_Description: The description
&LOG Associated_Files_Directory: "blah blah blah.file"
&LOG 
&LOG Part: "blah blah blah.file"
&LOG Cloning_Action: DEFAULT_DISP Naming_Technique: DEFAULT_NAMING Clone_Name: "@a/file/-"
&LOG Container: ""
&LOG Part_Type: BLAH
&LOG Part_Name: The new number
&LOG Part_Description: The description
&LOG Associated_Files_Directory: "blah blah blah.file"
&LOG
&LOG Part: "blah blah blah.file"
&LOG Cloning_Action: DEFAULT_DISP Naming_Technique: DEFAULT_NAMING Clone_Name: "@a/file/-"
&LOG Container: ""
&LOG Part_Type: BLAH
&LOG Part_Name: The new number
&LOG Part_Description: The description
&LOG Associated_Files_Directory: "blah blah blah.file"
&LOG
etc...


The .csv file would have the headers of what it says after "&LOG" up to the first ":" and each new line would be created from each section, of which sections are separated by a line of just "&LOG ".

The result would look something like:

Part,Cloning_Action,Container,Part_Type,Part_Name,Part_Description,Associated_Files_Directory
blah blah blah.file,DEFAULT_DISP Naming_Technique: DEFAULT_NAMING Clone_Name: @a/file/-,,BLAH,The new number,The description,blah blah blah.file
blah blah blah.file,DEFAULT_DISP Naming_Technique: DEFAULT_NAMING Clone_Name: @a/file/-,,BLAH,The new number,The description,blah blah blah.file
blah blah blah.file,DEFAULT_DISP Naming_Technique: DEFAULT_NAMING Clone_Name: @a/file/-,,BLAH,The new number,The description,blah blah blah.file
Windows 11 Pro; 100% scaling

highend
Posts: 14940
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Format input TXT file to output CSV file

Post by highend »

Code: Select all

    $content = readfile("<curitem>");
    $sections = regexreplace(regexmatches($content, "^&Log Part:[\s\S]*?^&Log\s?(\r?\n|$)", "<crlf 2>"), "^&Log\s?");
    $cvs = "";
    foreach($section, $sections, "<crlf 2>", "e") {
        $curLine = "";
        foreach($line, $section, "<crlf>", "e") {
            $value = trim(regexreplace($line, "^(.*?:)(.*)", "$2"));
            if (substr($value, 0, 1) == chr(34) && substr($value, -1) == chr(34)) {
                $value = trim($value, '"');
            }
            $curLine = $curLine . $value . ",";
        }
        $curLine = regexreplace($curLine, ",$");
        $cvs = $cvs . $curLine . "<crlf>";
    }
    $cvs = "Part,Cloning_Action,Container,Part_Type,Part_Name,Part_Description,Associated_Files_Directory" . "<crlf>$cvs";
    text $cvs;
One of my scripts helped you out? Please donate via Paypal

hermhart
Posts: 244
Joined: 13 Jan 2015 18:41

Re: Format input TXT file to output CSV file

Post by hermhart »

highend,

Amazing, works great!

Thank you so much!
Windows 11 Pro; 100% scaling

hermhart
Posts: 244
Joined: 13 Jan 2015 18:41

Re: Format input TXT file to output CSV file

Post by hermhart »

Would you be able to tell me how to correct this? I took your code from before and modified it to do it for each selected file. It seems to filter through each file and create an empty file, but does not fill the file with the contents of $csv. When I pick a single file, it works just fine.

Code: Select all

    foreach($item, "<get SelectedItemsPathNames |>") {
    $content = readfile("$item");
    $sections = regexreplace(regexmatches($content, "^&Log Part:[\s\S]*?^&Log\s?(\r?\n|$)", "<crlf 2>"), "^&Log\s?");
    $csv = "";
    foreach($section, $sections, "<crlf 2>", "e") {
        $curLine = "";
        foreach($line, $section, "<crlf>", "e") {
            $value = trim(regexreplace($line, "^(.*?:)(.*)", "$2"));
            if (substr($value, 0, 1) == chr(34) && substr($value, -1) == chr(34)) {
                $value = trim($value, '"');
            }
            $curLine = $curLine . $value . ",";
        }
        $curLine = regexreplace($curLine, ",$");
        $csv = $csv . $curLine . "<crlf>";
    }

    $csv = "Part,Cloning_Action,Container,Part_Type,Part_Name,Part_Description,Associated_Files_Directory" . "<crlf>$csv";

    $Bshort = gettoken(getpathcomponent($item, "base"), 1, " noseparator ", "t");
    writefile(new("$Bshort.csv", "file"), $csv, "o", "t");
    }
Windows 11 Pro; 100% scaling

highend
Posts: 14940
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Format input TXT file to output CSV file

Post by highend »

Works for me (creates correct and non empty .csv files)...

Use

Code: Select all

step;
before the $Bshort line and see what exactly is going wrong
One of my scripts helped you out? Please donate via Paypal

hermhart
Posts: 244
Joined: 13 Jan 2015 18:41

Re: Format input TXT file to output CSV file

Post by hermhart »

That's very strange. It works for me if I put the step; command in there. When I do, the dialogue at the point of the writefile(); shows:

call
3

But when I take out the step; it no longer works. It just ends up creating the files empty.
Windows 11 Pro; 100% scaling

highend
Posts: 14940
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Format input TXT file to output CSV file

Post by highend »

Try these lines instead:

Code: Select all

        $Bshort = gettoken(getpathcomponent($item, "base"), 1, " noseparator ", "t");
        writefile("$Bshort.csv", $csv);
One of my scripts helped you out? Please donate via Paypal

hermhart
Posts: 244
Joined: 13 Jan 2015 18:41

Re: Format input TXT file to output CSV file

Post by hermhart »

Thank you, highend. This did the trick.

Code: Select all

    writefile("$Bshort.csv", $csv, "o", "t");
Windows 11 Pro; 100% scaling

Post Reply