Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How can I use script change long list format?

Explorer ,
Dec 25, 2014 Dec 25, 2014

Hi everyone

If I have this kind of long list

0101
Long white radish
Chinese Celery
Tah tsai
Round cucumber
Cherry radish
fennel
Small-leaf arugula
Small wax gourd
0102
Green-leaf Lettuce
Broad-leaf arugula
UFO squash
Crystal radish
Red wave lettuce
Green endive
Sponge gourd
Rose-heart radish
Butter head lettuce
Radicchio
0103
Chinese mustard
New Zealand spinach
Yellow summer squash
Turnip
Malabar spinach
Chinese kale
Green bean
Edible rape
Begonia fimbristipulata hance
Purple bean
Cabbage
Water Spinach
Okra
Snow peas

How can I change the format into this:

0101
0101/Long white radish
0101/Chinese Celery
0101/Tah tsai
0101/Round cucumber
0101/Cherry radish
0101/fennel
0101/Small-leaf arugula
0101/Small wax gourd
0102
0102/Green-leaf Lettuce
0102/Broad-leaf arugula
0102/UFO squash
0102/Crystal radish
0102/Red wave lettuce
0102/Green endive
0102/Sponge gourd
0102/Rose-heart radish
0102/Butter head lettuce
0102/Radicchio
0103
0103/Chinese mustard
0103/New Zealand spinach
0103/Yellow summer squash
0103/Turnip
0103/Malabar spinach
0103/Chinese kale
0103/Green bean
0103/Edible rape
0103/Begonia fimbristipulata hance
0103/Purple bean
0103/Cabbage
0103/Water Spinach
0103/Okra
0103/Snow peas

by useing script?

thanks

Teetan

TOPICS
Scripting
1.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guru , Dec 27, 2014 Dec 27, 2014

I prefer this method

var sourceFile =  File.openDialog ("Select the source .txt file",   ($.os[0] == "W") ? "*.txt" : function (f)  { return (!f.name.match(/\./)) || (f instanceof Folder) || f.name.match(/\.txt$/i);});

if (sourceFile) {

    sourceFile.open('r');

    var text = sourceFile.read().replace(/\n/g, "\u6754"),

        r1 = /(\d{4})(((.(?!\d{4})){1,4})+)/g,

        r2 = /\u6754/g,

        result, a = [], n = 0

        destFile = new File (File.decode(sourceFile.fullName).replace(/\.txt

...
Translate
Enthusiast ,
Dec 25, 2014 Dec 25, 2014

Hi,

Try this ...

var myFolder=Folder("~/Documents");

var logFile = new File(myFolder + "/"+"logFile.txt");

var elementList = Array('0101','Long white radish','Chinese Celery','Tah tsai','Round cucumber','Cherry radish','fennel','Small-leaf arugula','Small wax gourd','0102','Green-leaf Lettuce','Broad-leaf arugula','UFO squash','Crystal radish','Red wave lettuce','Green endive','Sponge gourd','Rose-heart radish','Butter head lettuce','Radicchio','0103','Chinese mustard','New Zealand spinach','Yellow summer squash','Turnip','Malabar spinach','Chinese kale','Green bean','Edible rape','Begonia fimbristipulata hance','Purple bean','Cabbage','Water Spinach','Okra','Snow peas')

var newElementList = new Array;

var myPattern = new RegExp("^\\d{4,4}$");

var myPrefix="";

for (var i= 0; i< elementList.length;i++) {

          var aElement = elementList;

          if (myPattern.test(aElement)){

              newElementList.push(aElement);

              writeLogFile(aElement)

              myPrefix = aElement + "/";

          } else {

               newElementList.push(myPrefix+ aElement);

               writeLogFile(myPrefix+ aElement)

          }

     }

    $.write(newElementList);

  

function writeLogFile(theString) {

    logFile.open( "a" );

    logFile.writeln(theString);

    logFile.close();  

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 26, 2014 Dec 26, 2014

Hi Ronald

Thank you for your idea

thank so much

but it's not a good idea to set an Array for this long list, because it's a very long list, not just these.

Teetan

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Dec 26, 2014 Dec 26, 2014

You can set this array by script 😉

What is the list format (xml, txt, ...) and how many items?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 26, 2014 Dec 26, 2014

Hi Ronald

it's a text file

it's a long list for one year

those I posted just 3 days, and I make it as an example

Teetan

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Dec 26, 2014 Dec 26, 2014

Hi,

Perhaps not a problem, try this code ...


var myFolder=Folder("~/Documents");

var sourceFile = new File(myFolder + "/"+"sourceFile.txt");

var logFile = new File(myFolder + "/"+"logFile.txt");

//var elementList = Array('0101','Long white radish','Chinese Celery','Tah tsai','Round cucumber','Cherry radish','fennel','Small-leaf arugula','Small wax gourd','0102','Green-leaf Lettuce','Broad-leaf arugula','UFO squash','Crystal radish','Red wave lettuce','Green endive','Sponge gourd','Rose-heart radish','Butter head lettuce','Radicchio','0103','Chinese mustard','New Zealand spinach','Yellow summer squash','Turnip','Malabar spinach','Chinese kale','Green bean','Edible rape','Begonia fimbristipulata hance','Purple bean','Cabbage','Water Spinach','Okra','Snow peas')

//var newElementList = new Array;

var myPattern = new RegExp("^\\d{4,4}$");

var myPrefix="";

if (sourceFile.exists){sourceFile.open( 'r' );

while( !sourceFile.eof ) {

          var aElement  = sourceFile.readln();

          if (myPattern.test(aElement)){

              //newElementList.push(aElement);

              myPrefix = aElement + "/";

              writeLogFile(aElement)

          } else {

               //newElementList.push(myPrefix+ aElement);

               writeLogFile(myPrefix+ aElement)

          }

     }

}

    //$.write(newElementList);

   

function writeLogFile(theString) {

    logFile.open( "a" );

    logFile.writeln(theString);

    logFile.close();   

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 26, 2014 Dec 26, 2014

Hi Ronald

I run the script

but I got nothing

Teetan

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Dec 27, 2014 Dec 27, 2014

Have you defined the parh of your text file containing the list of values ?


  1. var myFolder=Folder("~/Documents"); 
  2. var sourceFile = new File(myFolder + "/"+"sourceFile.txt"); 
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Dec 27, 2014 Dec 27, 2014

I prefer this method

var sourceFile =  File.openDialog ("Select the source .txt file",   ($.os[0] == "W") ? "*.txt" : function (f)  { return (!f.name.match(/\./)) || (f instanceof Folder) || f.name.match(/\.txt$/i);});

if (sourceFile) {

    sourceFile.open('r');

    var text = sourceFile.read().replace(/\n/g, "\u6754"),

        r1 = /(\d{4})(((.(?!\d{4})){1,4})+)/g,

        r2 = /\u6754/g,

        result, a = [], n = 0

        destFile = new File (File.decode(sourceFile.fullName).replace(/\.txt$/i, " changed " + (new Date).toString ().replace(/:/g, ";") + ".txt" ));

    sourceFile.close();

    while((result = r1.exec(text)) != null) {

        a[n++] = (result[1] + (result[2].replace(r2, "\n" + result[1] + "/")));

    }

    destFile.open ('w');

    destFile.write (a.join("\n"));

    $.sleep(200);

    destFile.close();

    destFile.execute();

    alert("new Document: " + destFile.fsName + " created")

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Dec 28, 2014 Dec 28, 2014

Nice code Trevor 😉

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 28, 2014 Dec 28, 2014
LATEST

Thank you all guys

Nice try

NIce Code

Thank you Trevor

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines