Get file name of img seq except frame padding?

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
Simma
Posts: 98
Joined: June 8th, 2010, 2:57 pm

October 10th, 2011, 3:01 am

Is there a way to get the file name of a image sequence, minus the frame padding? For example, the name of a file could be any of the following (and more):

temp/shot01_####.dpx
temp/shot01_#######.dpx
temp/shot01.#.dpx

How would I do to find just "shot01"? It would be hard to search the string and just ignore those characters, as there are so many possible naming conventions.

Also, is there a way to find the number of the first frame? For example, a image sequence could be imported as shot01_[0030-0050].dpx into AE, how would I know could I tell AE that the first frame is frame 30? Same problem here, as there are a number of possible naming conventions, it could just as well be shot01_[0000030-0000050].dpx.

Thank you.
zold
Posts: 14
Joined: March 25th, 2011, 9:15 am
Contact:

October 14th, 2011, 3:05 pm

Simma wrote: temp/shot01_####.dpx
temp/shot01_#######.dpx
temp/shot01.#.dpx

How would I do to find just "shot01"? It would be hard to search the string and just ignore those characters, as there are so many possible naming conventions.
For the first problem, you could do something like the following:
Two step process:
first, you isolate the file name:

Code: Select all

fName = fullPath.replace(/\\/g,'/').replace( /.*\//, '' );
This gives you the file name, with the extension. Then,

Code: Select all

baseName = fName.split('.')[0];
gives you the basename w/o extension.
Of course, this has no error correction, if the file does not have a file extension.
Also, is there a way to find the number of the first frame? For example, a image sequence could be imported as shot01_[0030-0050].dpx into AE, how would I know could I tell AE that the first frame is frame 30? Same problem here, as there are a number of possible naming conventions, it could just as well be shot01_[0000030-0000050].dpx.
.
Let me make sure I understand. You want to find the beginning number of an imported file sequence?
If you have already have the sequence in a variable like "thisFileSeq":

Code: Select all

firstFileName = thisFileSeq.file.name;
This gives you the file name of the first file of the sequence. Then, if you need the number, you can do:

Code: Select all

firstFileName.split('.')[0].split("_").pop();
This will ONLY work if there is an underscore in the name before the number.
Looking for the number without the underscore gets quite a bit more complicated.

-crg
Simma
Posts: 98
Joined: June 8th, 2010, 2:57 pm

October 15th, 2011, 7:52 am

Thanks for your reply zold. I played some with your suggestion but didn't get it to work. I might have done something wrong though, as it gave me the full file name.

I think I got it to work with this code in most cases at least:

Code: Select all

var fileName = "shot01_01_007";

// Put every character in an array.
var myArray = new Array();
    for (var i = 0; i <= fileName.length - 1; i++){
    myArray[myArray.length] = fileName.charAt(i -1 );
    }

// Loop through the array (backwards) until a a character that isn't a number is found.
    for(var i = myArray.length - 1; i>=0; i--){
        var character = myArray[i].toString();
        var noNumber = isNaN(character);
        if(noNumber == true || character == "."){
        var theNoNumber = i;
        break;
        }
    }

// Subtract the frame padding from the filename.
var subtractNumber = myArray.length - theNoNumber + 1;
var myFileName = fileName.substring(0,fileName.length-subtractNumber);

alert("The filename without framepadding is: " + myFileName);

var firstFrame = parseFloat(fileName.slice(theNoNumber));

alert("The firsframe of the sequence is: " + firstFrame);


zold
Posts: 14
Joined: March 25th, 2011, 9:15 am
Contact:

October 18th, 2011, 8:34 am

[UPDATE: Okay I just realized that using parseInt() is a very bad idea in this case, as using "padded" zeros is interpreted as octal (see http://www.w3schools.com/jsref/jsref_parseInt.asp -- "If the string begins with "0", the radix is 8 (octal). This feature is deprecated" [so even though it is deprecated, it still does this])
SORRY ABOUT THAT.

Simma wrote:Thanks for your reply zold. I played some with your suggestion but didn't get it to work. I might have done something wrong though, as it gave me the full file name.
"it" refers to what? Which suggestion didn't work? Running this in AE works for me, but maybe I'm missing something:

Code: Select all

n = "shot01_01_007".split('.')[0].split("_").pop();
//returns "007"
//then, to 'strip' the leading zeros:
//makes "007" into an integer, then back to string:
[UPDATE: DO NOT USE THIS TECHNIQUE; SEE ABOVE!]

Code: Select all

n = (parseInt(n).toString());
alert(n);//should show "7"
[2nd UPDATE: THIS IS WHAT YOU SHOULD USE. PARSEFLOAT, NOT PARSEINT:]

Code: Select all

n = (parseFloat(n).toString());
alert(n);//should show "7"
Cheers!
zold
Posts: 14
Joined: March 25th, 2011, 9:15 am
Contact:

October 18th, 2011, 5:49 pm

and you could also use the regular expression solution (but if you want a number, parseInt it):

Code: Select all

n = "000100";
nAsString = (n.replace(/^[0]+/g,""));
//100 as string ("100")
nAsInteger = parseInt(nAsString);
// 100 as integer
User avatar
lloydalvarez
Enhancement master
Posts: 460
Joined: June 17th, 2004, 9:27 am
Location: New York City, NY
Contact:

October 24th, 2011, 2:07 pm

parseInt has an optional radix parameter (http://www.w3schools.com/jsref/jsref_parseInt.asp) so you would simply tell it you want it to parse the integer on a base of 10 like so:

parseInt("00007",10); //result: 7


I think having leading zero's is an undocumented way of defining an octal base which is probably why you are getting the unusual output. I would say it is good practice to always define the base when using parseInt. Also, if what you want is an integer use parseInt instead of parseFloat.

-Lloyd
Alan Eddie
Posts: 28
Joined: January 12th, 2012, 1:36 am
Location: Ireland
Contact:

January 18th, 2012, 5:28 am

Hi there, before I found this post I was working with some of these problems.
This is some code I had generated, I just said I would post it here for reference.



var testRegExp = new RegExp ("[0-9]+\.[A-Za-z]{1,3}$");
//will return 0023.tga for example...

var testString = app.project.item(i).mainSource.file.displayName;
//DO WE HAVE NUMBERS IN THE FILE THAT LOOK LIKE A SEQUENCE..?
var x =testRegExp.test(app.project.item(i).mainSource.file.displayName);

// IF WE DO , REPLACE THE MATCH WITH NOTHING...+ (seq)
if ( x == true){
//CURRENT STRING FOR ITEM
var curName =app.project.item(i).name;
//THE NEW STRING FOR ITEM
var newName = curName.replace(testRegExp," (Seq)");
//ASSIGN THE NEW STRING TO THE ITEM.
app.project.item(i).name = newName;
}//CLOSE IF.
Alan Eddie
_______________________
www.alaneddie.com
3d Animation and VFX
RTE
Dublin
Post Reply