[Closed] Extract Numbers from a Filename
Hello Again
I am trying to read in a bunch of files from a directory then extract the number located in tere filename i.e. render_0001.tga would return 0001 . For the life of me I cannot manage to do it. I can add numbers ot the name but not remove characters to remove only the numbers.
Any help would be appreciated.
Thanks
Harry
Hi,
You can use a fn like this:
fn removeChars str = (local nums = “0123456789”; for i = str.count to 1 by -1 where (local index = findString nums str[i]) == undefined do str = replace str i 1 “”; str)
Or you can use filterString to get the last part as in (local arr = filterString str “_”)[arr.count]
Light
Hi Light
That works nicely thanks. However you dont get away that easy I have a couple of questions on your solution. I ma still learning so try and pick apart every script. Lets see if I get this right.
fn removeChars str = –defines a function to which the variable str is passed.
(local nums = “0123456789”;
for i = str.count to 1 by -1 where [color=yellowgreen]– I dont get why you are going from Str.count to -1 is this not going backwards? [/color]
(local index = findString nums str[i]) == undefined – Ok this looks for the numbers you defined and goes through the entered string to find them one character at a time?
do str = replace str i 1 “”; str) – this replaces the current str variable with itself but with all the letters removed and finaly returns itself?
I think that right. I think it is important to understand how someone has come to a solution so please bear with me
One last question. If you use get GetFiles to create an array it enters the complete files name and path in the array. Is there a simple enough way to remove the path.
Thanks again
Harry
getFiles “c:\.”
#(“c:\hiberfil.sys”, “c:\IO.SYS”, “c:\MSDOS.SYS”, “c:\NTDETECT.COM”, “c:
tldr”, “c:\pagefile.sys”, “c: est.txt”, “c:\vraylog.txt”)
namesOnly = for i in (getFiles “c:\.”) collect filenameFromPath i
#(“hiberfil.sys”, “IO.SYS”, “MSDOS.SYS”, “NTDETECT.COM”, “ntldr”, “pagefile.sys”, “test.txt”, “vraylog.txt”)
All correct. In fact you are right in your second statement, I probably tried to replace one character at a time and then left it there after changing the method.
The better version would be:
fn removeChars str = (local nums = “0123456789”, txt = “”; for i = 1 to str.count where (local index = findString nums str[i]) != undefined do txt += str[i]; txt)
- One last question. If you use get GetFiles to create an array it enters the complete files name and path in the array. Is there a simple enough way to remove the path.
Yeah. But what for?
Because I do it in a script of mine which only removes the common path from all pathnames. You can also replace them with numbers which refer to an array like #(3, 5, 12, “83747.max”)
Light
Hiya
sorry this isnt so much about your question but it reminded me of working with files back
in the day…
Its useful for formatting file name numbers with leading zeroes…might be useful to number
large groups of models?
– n = 0-999…
fn putZeroes n
(
t100=((n/100) as integer) as string
t10=((mod (n/10) 10) as integer) as string
t1=(mod n 10 as integer) as string
return (t100+t10+t1)+”_”
)
outputString=putzeroes 1 – returns “001_”
– then
filename=outputstring+(common name of file and extension)
maldwyn
Hey Guys
Thjanks for all the replies and info.
Hi Light
“Yeah. But what for? :)”
I am scanning a list of files in a flder the folder structure may contain a number. I dont want the number of the folder to be left.
Hi Bobo
Thanks again this will be very usefull.
Hi maldwyn
Again useful stuff. Not for this particular script however. In this one I am scanning a list of image files, putting them into an array in positions that correspond to their numbers. I can then work out which files are missing from a sequence. Its to try and help out in the rendering process so the artist doesnt have to try and workout what is missing in After Effects
Thanks again guys
Harry
hiya
have a go at this for your missing files thingy…
local listoffiles=#(“test_001.bmp”,“test_002.bmp”,“test_004.bmp”,“test_006.bmp”)
local resultarray=#()
for xx in listoffiles do
(
– convert string to stringstream
local tmp=xx as stringstream
– move to just after ““
skiptostring tmp “”
– grab everything from there up to “.”
local res=readdelimitedstring tmp “.”
– convert the value of the resulting string to an integer
res=res as integer
– [should do some kind of validation thing to make sure its a number (its up to you…)]
– use that number to fill in a value other than ‘undefined’ at that pt in the array
resultarray[res]=“ok”
)
for xx=1 to resultarray.count do
(
if resultarray[xx]==undefined do
(
– print to the Listener window
format “Missing: %
” xx
)
)
maldwyn
Hi maldwyn
Sorry it took so long to get back.
Thanks for the script. I will use it as reference rather than simply copying. Otherwise I will never learn
Thanks
Harry