Posted 8 years ago
·
Author
After spending the night with my head in WPF dependency properties and styles, I decided to take a break an work on something that was a little easier.
This script will rename all chkns in a folder with random names. The script itself isn't really that useful, writing it was a way for me to get back into AutoIT and to see what all was possible. I had also wanted to give this section of the forums more content and had hoped this would spark some creativity.
You can find the full source code below with comments so it should be easier to edit.
This script will rename all chkns in a folder with random names. The script itself isn't really that useful, writing it was a way for me to get back into AutoIT and to see what all was possible. I had also wanted to give this section of the forums more content and had hoped this would spark some creativity.
You can find the full source code below with comments so it should be easier to edit.
#include <Array.au3>
#include <File.au3>
;The directory to search in
Global $workingDirectory = "C:\Users\User\Desktop\New folder"
;Create an array of all the CHKNs found in the working directory
$array = _FileListToArray($workingDirectory, "*.chkn")
;If no CHKNs were found
If @error = 4 Then
MsgBox($MB_SYSTEMMODAL, "", "No CHKNs were found.")
Exit
EndIf
;Iterate through the CHKNs
For $i = 1 to $array[0]
Local $newName = genRandomName()
FileMove($workingDirectory & "\" & $array[$i], $workingDirectory & "\" & $newName & ".chkn")
ConsoleWrite($array[$i] & " was renamed to " & $newName & ".chkn" & @LF);
Next
;This function returns a random string
Func genRandomName()
;Create an array of ASCII codes to generate the string from.
;We do this rather than using Chr(Random(num,num,1)) because
;this is less work
Local $characters = StringToASCIIArray("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
;create a variable to store the random string in
Local $name = ""
;the size of the random string to generate
Local $size = 10
For $i = 1 To $size
;Create local variable to store the previously chosen character
Local $previousC = ""
;Create a variable to store the chosen character and give it a value
;by selecting a random ASCII code from the $characters array And
;converting it to a character
Local $c = Chr($characters[Random(1, UBound($characters))])
;prevent duplicate characters side-by-side by repeatedly generating a new character
;using the same technique as above until a unique one is found
Do
$c = Chr($characters[Random(1, UBound($characters))])
;ConsoleWrite($c & @LF)
Until Not ($c = $previousC)
;Set the newly selected character as the previous one
$previousC = $c
;append the character to the string
$name &= $c
Next
Return $name
EndFunc
@Don Von Alpha Dom