[Closed] save prefix script
Hi,
Just a simple save scripts i need help with:
what im after is:
If you have a file called project.max
when you hit “save major script” or “save minor script” for the first time will add project_v01_01.max prefix to the file and save it, if it doesnt have the *v01_01.max prefix already.
When hitting the save “save major script” i want the script save max file but change it to like this:
project_v02_01.max
if the ““save major script” is clicked again the will count up and save.
When save minor script is clicked
it will save the file like this:
project_v01_02.max
If clicked again it will count up and save.
thanks in advance
If your naming convention is strict then you can simply have a script which filters the string into tokens.
tokens = filterstring RendOutputFilename “v_.”
majorVersion = tokens[tokens.count – 2] as integer
minorVersion = tokens[tokens.count – 1] as integer
majorVersion += 1
minorVersion += 1
Then you want to look at using formattedPrint to piece it back together again. You’ll also want to check to see if the filename you want already exists or not before saving.
if the naming convention is strict:
(substring filename (filename.count-1) 2) as integer -- minor id
(substring filename (filename.count-4) 2) as integer -- major id
Like in the Nexus Pipeline Ravi?
There’s a few ways to do this, You’ll need to see if the file is already in the correct format. I like to use Regular Expressions (Regex) to check the version data -it makes validation a little more straightforward as the isMatch() method is very clear. Once you have established it is in the correct format, you can use Regex, filterstring or what you like to version up the filename. There’s lots of different ways to format Regex, but here’s a simple example:
clearlistener()
-- Validates the following condition :
-- A word containing letters and numbers, an underscore, a 'v', any two numbers, an underscore, and any two numbers
validationString = "[a-zA-Z0-9]_v[0-9]{2}_[0-9]{2}.max"
rx = dotnetobject "System.Text.RegularExpressions.Regex" validationString
-- example filenames for testing. See how it filters out the illegal characters in val7.
testVal1 = "TestFile_v01_01.max"
testVal2 = "TestFile_V01_02.max"
testVal3 = "TestFile_01_01.max"
testVal4 = "TestFile_meh_v01.max"
testVal5 = "TestFile_01_v01.max"
testVal6 = "LR_v01.max"
testVal7 = "&$*_v01_01.max"
testArr = #(testVal1,testVal2,testVal3,testVal4,testVal5,testVal6,testVal7)
-- print the results of the regex comparision
for each in testArr do format "filename % is match ? :%
" each (rx.isMatch each)
-- to test a filename for the correct parts before saving
if rx.isMatch maxfilename then
(
-- file is in the correct save format
-- increment something like how dave described
)
else
(
--file is not in the correct format, construct the filename and save
)
You can use matchpattern, but it will only allow you to test for a certain number of characters, not the presence of a numerical value in the way regex allows.
-- matchpattern
for each in testArr do format "filename % is match ? :%
" each (matchpattern each pattern:"*_v??_??.max")
You can also adjust the validation string to allow lower case or uppercase by including the OR (|) operator in the regex string
"[a-zA-Z0-9]_[B](v|V)[/B][0-9]{2}_[0-9]{2}.max"
or use the ignore case option in the regex constructor :
rx = dotnetobject "System.Text.RegularExpressions.Regex" validationString (dotnetclass "System.Text.RegularExpressions.RegexOptions").IgnoreCase
The regex string above could be modified to extract the version string portion too as an alternative to filtering the string, using the match() method to yield the portion you need.
The example below uses negative lookbehind to ascertain the correct part, it only returns the two digit version string that is preceeded by a letter v, but doesn’t include it in the regex return value.
testVal1 = "TestFile_v03_01.max"
testVal2 = "Testv02Filev04_v05_01.max"
vRx = dotnetobject "System.Text.RegularExpressions.Regex" "(?<=_v)[0-9]{2}"
(vrx.match testVal1).value
-- "03"
(vrx.match testVal2).value
-- "05"
thanks guys for your help
thanks Dave, Pete
i have some testing to do now.
Pete, just like brilliant nexus pipeline!
If you want to go down the Regex route then you may find this useful.
fn incrementSpecialFilename filename type:#minor =
(
if matchpattern filename pattern:"*_v??_??" do try
(
shift = if type == #minor then 1 else 4
id = (substring filename (filename.count-shift) 2) as integer
filename = replace filename (filename.count-shift) 2 (formattedprint (id+1) format:"02d")
)
catch()
filename
)
/*
incrementSpecialFilename @"c: est est_v02_12"
incrementSpecialFilename @"c: est est_v02_12" type:#major
*/
i wrapped the code in the try-catch expression in case if v???? contains anything else than digits.