Jump to content
Sign in to follow this  
fdmautokj

Rename a folder with a random name

Recommended Posts

fdmautokj

i want to rename a folder present in the folder path c:/temp with a random name this folder inside c:/temp is called update2016 how i can ranzomize this folder ?

example one update2016_809394816

example two update2016_271394881

example three update2016_529396814

Share this post


Link to post
Share on other sites
JLogan3o13

@fdmautokj A glance at the help file would show you have answered your own question - look at the Random keyword. Simply use Random to generate a number and then tack it on to the end of the folder name.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
Chimp
Posted (edited)

I assume that OP have not to rename that folder just one time, but needs to generate many folders  with the same "prefix" (that is "update2016") followed by a suffix guarantee to be unique.
In this case using the random() function doesn't guarantee that the generated number is not been already generated, causing in that case a duplicate folder name.

I would suggest 3 possible alternatives:

1)  append the year + month + day + hour + minute + second + millisecond to your base foldername

$sFoldername = "update2016" & "_" & @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC & @MSEC

however this may generate duplicate filenames if you generate more folders in less than a millisecond

2)  another way is to use the _WinAPI_PathYetAnotherMakeUniqueName() function;

3)  or, as a third alternative, you can generate a unique ID using the _WinAPI_CreateGUID() function and append it to your folder name.

p.s.

Because AutoIt lacks a "DirRename" function, use the DirMove() function to rename a folder.

Edited by Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites
JLogan3o13

I believe _WinAPI_PathYestAnotherMakeUniqueName() is for files, not folders. Regardless, @fdmautokj as you can see there are many ways to skin the proverbial cat.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
Chimp
3 hours ago, JLogan3o13 said:

I believe _WinAPI_PathYestAnotherMakeUniqueName() is for files, not folders ...

At first I thoug the same, but with this little test i saw it also works with foldernames..

#include <WinAPIShPath.au3>

; foldername to be renamed
$sFolderName = @TempDir & "\update2016"

For $i = 1 To 4
    ; create the "base" folder
    DirCreate($sFolderName)

    ; rename the base folder with a new unique name
    ; (actually we move the old folder to new one with a new name)
    DirMove($sFolderName, _WinAPI_PathYetAnotherMakeUniqueName($sFolderName))
Next

; show the result
ShellExecute(@TempDir)

 


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites
JLogan3o13

Learn something new, I have only seen it used no files.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×