Hello
I need to find the best option for renaming the files in an assembled job in which all the file names are identical (except for their unique prefix). They need to be renamed to include a suffix in the format _1, _2, _3, etc.
The number of jobs will always be variable, and I need to understand how to build the logic to loop through the files, add the correct suffix based on the number of files in the job folder and then exit.
The Rename element has a script expression option property, but I think this functionality may require a script element.
Does anyone have any code snippets for this kind of functionality to point me in the right direction?
Thanks in advance.
Script Expression or Script...?
Script Expression or Script...?
Regards,
William Buckingham
William Buckingham
-
- TOP CONTRIBUTOR
- Posts: 628
- Joined: Mon Nov 29, 2010 8:45 pm
- Location: Alpharetta GA USA
- Contact:
Script Expression or Script...?
A script expression will not work since it only allows you to modify a single file at a time.
Here's a basic Javascript loop that enumerates each file in a job and adds a number suffix.
var theJobDir = new Dir(job.getPath());
var theDirEntries = theJobDir.entryList("*.pdf", Dir.Files, Dir.Name);
for (var i=0; i < theDirEntries.length; i=i+1)
{
var theOrigName = theJobDir.path + "/" + theDirEntries;
var theOrigFile = new File(theOrigName);
var theNewFile = theJobDir.path + "/" + theOrigFile.baseName + "_" + (i+1) + "." + theOrigFile.extension;
s.copy(theOrigName, theNewFile);
}
Dwight Kelly
Apago, Inc.
dkelly@apago.com
Here's a basic Javascript loop that enumerates each file in a job and adds a number suffix.
var theJobDir = new Dir(job.getPath());
var theDirEntries = theJobDir.entryList("*.pdf", Dir.Files, Dir.Name);
for (var i=0; i < theDirEntries.length; i=i+1)
{
var theOrigName = theJobDir.path + "/" + theDirEntries;
var theOrigFile = new File(theOrigName);
var theNewFile = theJobDir.path + "/" + theOrigFile.baseName + "_" + (i+1) + "." + theOrigFile.extension;
s.copy(theOrigName, theNewFile);
}
Dwight Kelly
Apago, Inc.
dkelly@apago.com
Script Expression or Script...?
Dwight
Thanks for that.
It has me most of the way there.
I note from some earlier posts by you and Ben Schumeth at Enfocus that possibly I could use a Windows ren function using the Process class instead of copying the file.
This would actually be ideal as some of these files are quite large and there are hundreds of them per day.
Do you know what that syntax looks like?
Thanks again.
Thanks for that.
It has me most of the way there.
I note from some earlier posts by you and Ben Schumeth at Enfocus that possibly I could use a Windows ren function using the Process class instead of copying the file.
This would actually be ideal as some of these files are quite large and there are hundreds of them per day.
Do you know what that syntax looks like?
Thanks again.
Regards,
William Buckingham
William Buckingham
-
- TOP CONTRIBUTOR
- Posts: 628
- Joined: Mon Nov 29, 2010 8:45 pm
- Location: Alpharetta GA USA
- Contact:
Script Expression or Script...?
Look at the Process() class
Script Expression or Script...?
Dwight
I did so and was completely perplexed at what the correct syntax should be based on the manual.
Ben at Enfocus suggested the following:
Process.execute( ["cmd","/C",ren, theOldName, theNewName] );
However I can't get that to work within the loop function you kindly provided. No errors, but nothing happens.
I'll go with the copy method for now and can hopefully work this out in the future as it would save some time and resources.
Thanks again.
I did so and was completely perplexed at what the correct syntax should be based on the manual.
Ben at Enfocus suggested the following:
Process.execute( ["cmd","/C",ren, theOldName, theNewName] );
However I can't get that to work within the loop function you kindly provided. No errors, but nothing happens.
I'll go with the copy method for now and can hopefully work this out in the future as it would save some time and resources.
Thanks again.
Regards,
William Buckingham
William Buckingham
-
- TOP CONTRIBUTOR
- Posts: 628
- Joined: Mon Nov 29, 2010 8:45 pm
- Location: Alpharetta GA USA
- Contact:
Script Expression or Script...?
You have to tell Switch about the new names and also tell it to drop the old files.
job.sendToNull(theOldName);
job.sendToSingle(theNewName);
job.sendToNull(theOldName);
job.sendToSingle(theNewName);