Jump to content

Rename a folder with a random name


Recommended Posts

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

Link to comment
Share on other sites

  • Moderators

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

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

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

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

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

Link to comment
Share on other sites

  • Moderators

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

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

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)

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

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

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...