Page 1 of 1

Format input TXT file to output CSV file

Posted: 27 Dec 2015 22:51
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

Re: Format input TXT file to output CSV file

Posted: 27 Dec 2015 23:40
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;

Re: Format input TXT file to output CSV file

Posted: 28 Dec 2015 16:52
by hermhart
highend,

Amazing, works great!

Thank you so much!

Re: Format input TXT file to output CSV file

Posted: 04 Jan 2016 19:17
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");
    }

Re: Format input TXT file to output CSV file

Posted: 04 Jan 2016 20:22
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

Re: Format input TXT file to output CSV file

Posted: 04 Jan 2016 21:17
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.

Re: Format input TXT file to output CSV file

Posted: 04 Jan 2016 21:26
by highend
Try these lines instead:

Code: Select all

        $Bshort = gettoken(getpathcomponent($item, "base"), 1, " noseparator ", "t");
        writefile("$Bshort.csv", $csv);

Re: Format input TXT file to output CSV file

Posted: 05 Jan 2016 13:26
by hermhart
Thank you, highend. This did the trick.

Code: Select all

    writefile("$Bshort.csv", $csv, "o", "t");