Jump to content

_DirRename() can anyone make the function with different code? *Challenge*


guinness
 Share

Recommended Posts

_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 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 parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

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

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

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

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 parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

  • 2 weeks later...

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 by Spiff59
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...