# Renaming: random numbers?

**URL:** https://resource.dopus.com/t/renaming-random-numbers/3542
**Category:** Help & Support
**Created:** [May 6, 2007, 3:11am UTC](https://resource.dopus.com/t/renaming-random-numbers/3542 "2007-05-06T03:11:31Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![roxy](https://resource.dopus.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@roxy](https://resource.dopus.com/u/roxy)
#### Post date: [May 6, 2007, 3:11am UTC](https://resource.dopus.com/t/renaming-random-numbers/3542/1 "2007-05-06T03:11:31Z")

</div>

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. 😊

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.🙂

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.

---

<div class="post-metadata">

### Author: ![Leo](https://resource.dopus.com/user_avatar/resource.dopus.com/leo/32/69732_2.png) [@Leo](https://resource.dopus.com/u/Leo)
#### Post date: [May 6, 2007, 7:46am UTC](https://resource.dopus.com/t/renaming-random-numbers/3542/2 "2007-05-06T07:46:58Z")

</div>

> [@](#):
>
> Changing the number to something random would be awesome.

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.

> [@](#):
>
> 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

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.)

---

<div class="post-metadata">

### Author: ![roxy](https://resource.dopus.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@roxy](https://resource.dopus.com/u/roxy)
#### Post date: [May 6, 2007, 4:58pm UTC](https://resource.dopus.com/t/renaming-random-numbers/3542/3 "2007-05-06T16:58:06Z")

</div>

[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. 🙂

---

<div class="post-metadata">

### Author: ![roxy](https://resource.dopus.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@roxy](https://resource.dopus.com/u/roxy)
#### Post date: [May 6, 2007, 10:30pm UTC](https://resource.dopus.com/t/renaming-random-numbers/3542/4 "2007-05-06T22:30:56Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Steve](https://resource.dopus.com/user_avatar/resource.dopus.com/steve/32/34171_2.png) [@Steve](https://resource.dopus.com/u/Steve)
#### Post date: [May 6, 2007, 10:39pm UTC](https://resource.dopus.com/t/renaming-random-numbers/3542/5 "2007-05-06T22:39:51Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![roxy](https://resource.dopus.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@roxy](https://resource.dopus.com/u/roxy)
#### Post date: [May 6, 2007, 10:44pm UTC](https://resource.dopus.com/t/renaming-random-numbers/3542/6 "2007-05-06T22:44:13Z")

</div>

Oh, that works perfectly! Thanks so much. 🙂

---

<div class="post-metadata">

### Author: ![Leo](https://resource.dopus.com/user_avatar/resource.dopus.com/leo/32/69732_2.png) [@Leo](https://resource.dopus.com/u/Leo)
#### Post date: [August 20, 2007, 8:49am UTC](https://resource.dopus.com/t/renaming-random-numbers/3542/7 "2007-08-20T08:49:31Z")

</div>

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](https://resource.dopus.com/t/add-a-unique-number-while-renaming/3900/1) 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.
