guinness Posted September 10, 2010 Share Posted September 10, 2010 (edited) _DirRename() is able to rename folders randomly. You specify the parent directory (the initial folder) and then specify a sub-folder(s) that are included within the parent folder. It will then rename all the sub-folder(s) til it reaches the parent directory.The function is working as intended, but I wanted to throw the question out of if anyone can create a similar function with different code? I simply want to learn!#AutoIt3Wrapper_au3check_parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 _DirRename("C:\Test Folder\", "C:\Test Folder\Test\Test1\Test2\Test3\") ; Result: "C:\Test Folder\RANDOM_0\RANDOM_1\RANDOM_2\RANDOM_3\") Func _DirRename($iInPath, $iOutPath) Local $TEMPiFrom, $TEMPiTo If StringRight($iInPath, 1) <> "\" Then $iInPath &= "\" If StringRight($iOutPath, 1) = "\" Then $iOutPath = StringTrimRight($iOutPath, 1) Local $iStringSplit = StringSplit(StringReplace($iOutPath, $iInPath, ""), "\") Local $TEMPiStringSplit = $iStringSplit[0] For $A = 1 To $iStringSplit[0] For $B = 1 To $TEMPiStringSplit $TEMPiFrom &= $iStringSplit[$B] & "\" If $B > 1 Then $TEMPiTo &= $iStringSplit[$B - 1] & "\" Next Local $iRandom = Chr(Random(Asc("a"), Asc("z"), 1)) & Random(1, 500, 1) If FileExists($iInPath & $TEMPiFrom) Then DirMove($iInPath & $TEMPiFrom, $iInPath & $TEMPiTo & $iRandom & "\") DirRemove($iInPath & $TEMPiTo & $iRandom) EndIf $TEMPiFrom = "" $TEMPiTo = "" $TEMPiStringSplit -= 1 Next EndFunc ;==>_DirRename Edited September 10, 2010 by guinness UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
zorphnog Posted September 10, 2010 Share Posted September 10, 2010 Here's my crack at it:_DirRandomize("C:\Test Folder\", "Test\Test1\Test2\Test3\") Func _DirRandomize($sParent, $sChildren) Local $aChildren, $sCurDir, $i, $sNewDir If StringRight($sParent, 1) <> "\" Then $sParent &= "\" If StringRight($sChildren, 1) <> "\" Then $sChildren &= "\" $aChildren = StringRegExp($sChildren, "(?U:.+)\\", 3) If @error Then Return SetError(1, 0, 0) $sCurDir = $sParent For $i=0 To UBound($aChildren) - 1 Do $sNewDir = $sCurDir & Chr(Random(97, 122, 1)) & Random(1, 500, 1) & "\" Until Not FileExists($sNewDir) DirMove($sCurDir & $aChildren[$i], $sNewDir) $sCurDir = $sNewDir Next EndFunc Link to comment Share on other sites More sharing options...
XKahn Posted September 10, 2010 Share Posted September 10, 2010 Instead of DirMove () - Moving the contents of the sub directory and deleting the old folder you could use; RunWait (@ComSpec & " /c rename " & $old_name & " " & $new_name, "", @SW_HIDE) This drops to the cmd prompt level and uses rename to simply rename the folder, the contents remain unchanged. That should help cut down a lot of the bulk in your script. Link to comment Share on other sites More sharing options...
zorphnog Posted September 10, 2010 Share Posted September 10, 2010 Instead of DirMove () - Moving the contents of the sub directory and deleting the old folder you could use;RunWait (@ComSpec & " /c rename " & $old_name & " " & $new_name, "", @SW_HIDE)This drops to the cmd prompt level and uses rename to simply rename the folder, the contents remain unchanged. That should help cut down a lot of the bulk in your script.Using DirMove with the source and destination in the same folder will simply rename the folder. Much in the way that the cmd prompt move works. Link to comment Share on other sites More sharing options...
guinness Posted September 10, 2010 Author Share Posted September 10, 2010 Excellent zorphnog! Different to how I did it and I especially like the use of StringRegExp instead of StringSplit. I wanted to post a message like this because it's always great to see different styles of coding. Most of the 'stuff' I have learnt from the Help File (which is detailed) and Searching, hence why my number of posts are so low. I may do these challenge posts from time to time, just to learn different styles. UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
Spiff59 Posted September 21, 2010 Share Posted September 21, 2010 (edited) There is one HUGE advantage to the DOS "rename" command, versus using the built-in DirMove() function to rename a folder. DirMove() starts copying files, and only fails once it encounters a read-only or open file. You're left with some of your files/folders moved, some not moved, and no record of which is which. The DOS "rename" command functions much better as it will fail with an error code if any file is open, but leave your initial files and directory structure intact. No mess to clean up Edited September 21, 2010 by Spiff59 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now