Copy link to clipboard
Copied
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
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
Copy link to clipboard
Copied
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();
}
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
You can set this array by script 😉
What is the list format (xml, txt, ...) and how many items?
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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();
}
Copy link to clipboard
Copied
Hi Ronald
I run the script
but I got nothing
Teetan
Copy link to clipboard
Copied
Have you defined the parh of your text file containing the list of values ?
- var myFolder=Folder("~/Documents");
- var sourceFile = new File(myFolder + "/"+"sourceFile.txt");
Copy link to clipboard
Copied
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")
}
Copy link to clipboard
Copied
Nice code Trevor 😉
Copy link to clipboard
Copied
Thank you all guys
Nice try
NIce Code
Thank you Trevor
Find more inspiration, events, and resources on the new Adobe Community
Explore Now