Renaming: random numbers?

Is it possible to rename a file to a two digit random number? Would I have to use the script function? I attempted this but as I know nothing about scripting I failed pretty miserably. :blush:

I ask because right now I have a button that renames like this:
[Parent folder name] - [##]_[imagewidth]px.jpg

Changing the number to something random would be awesome.:slight_smile:

Edit: Also, how would I go about making the renamed file open with a certain program after it's renamed? I know how to get the program opened but that's about it.

The easiest way to do this is probably to use a rename script (assuming you have Opus 9) since VBScript has a convenient random number function.

Would you need it to check that the number isn't already in use by another file, though? That is still possible but a bit more work.

I'm not sure how you would do that. You'd need to know the new file's name but the only thing that knows that is the rename script, before the rename has happened, so it can't launch the program. (Unless it does something complicated, like launch a batch file which waits a couple of seconds and then launches the program with the expected new name, but that could be unreliable.)

[quote]Would you need it to check that the number isn't already in use by another file, though? [/quote] Nope, and I have Opus 9.

And after using it this way for a while, it looks like just having the program open (which I've managed myself) will do just fine. :slight_smile:

Ohh, I've done it, I think. There's probably a much better way, but this is what I have to generate a random two digit number:

[code]@script vbscript
Option Explicit

Function Rename_GetNewName ( strFileName, strFilePath, _
fIsFolder, strOldName, ByRef strNewName )

dim rndNum
RANDOMIZE ()
rndNum=Int(9 * rnd())

strNewName = Replace(strNewName, "#", rndNum)

dim rndNumb
RANDOMIZE ()
rndNumb=Int(9 * rnd())

strNewName = Replace(strNewName, "~", rndNumb)

End Function[/code]

And then my New Name code looks like this: {parent} - ~#_{picwidth}px.jpg

I couldn't work out how to make it put a zero if the number was less than 10 when I had it randomizing between 0-99 (:oops:) , which is why I went with two different functions (I don't know if that's the right word?) of 0-9.

This is slightly neater and copes with padding the numbers if less than 10.

[code]@script vbscript
Option Explicit

Function Rename_GetNewName ( strFileName, strFilePath, _
fIsFolder, strOldName, ByRef strNewName )
' Add script code here

dim rndNumber

Randomize()

rndNumber = Int(100 * rnd())

if (rndNumber < 10) then
	strNewName = Replace(strNewName, "##", "0" & rndNumber)
else
	strNewName = Replace(strNewName, "##", rndNumber)
end if

End Function[/code]

Put {parent} - ##_{picwidth}px.jpg in your New name field.

Oh, that works perfectly! Thanks so much. :slight_smile:

The scripts in this thread don't check that the chosen random number isn't already used for an existing file, which could cause problems.

I have updated my Unique Numbers rename script to add a RandomNumbers option. That script will check that the chosen number isn't already in use. You can also use the PadTo setting at the top of the script to control the range of random numbers that are generated.