tuatara Posted March 14, 2012 Share Posted March 14, 2012 (edited) Can anyone help me with this? In a big diretory tree i need to replace all white space in directory names by underscores. In de test below i just replaced [:blank:] with a separate part for a space and (below that) a tab The problem i encounter is: It also finds folders in a wrong level like: f:folder-is-okfolder with blanksfolder-is-also-ok and i want to change these like: f:folder-is-okfolder with blanksfolder-is-also-ok in: f:folder-is-okfolder_with_blanksfolder-is-also-ok so both $FullFilePath and $withoutwhitespace are full pathnames like f:directory1directory2 but the DirMove fails. This is a part of the script that doesn't work: btw: $oldpath = <filled in by selecting a directory> $aList = _RecFileListToArray($sOldPath, "*.*", 2 , 1 , 1) If IsArray($aList) Then For $i = 1 To $aList[0] $item = $aList[$i] $aa = $aList[0] $FullFilePath = $sOldPath & "" & $item ElseIf StringInStr($FullFilePath," ") Then $withoutwhitespace = StringRegExpReplace($FullFilePath, " ", "_") DirMove($FullFilePath, $withoutwhitespace) If @error = 1 Then exit ElseIf StringInStr($FullFilePath," ") Then $withoutwhitespace = StringRegExpReplace($FullFilePath, " ", "_") DirMove($FullFilePath, $withoutwhitespace) If @error = 1 Then exit EndIf Next EndIf Tuatara Edited March 14, 2012 by tuatara Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted March 14, 2012 Moderators Share Posted March 14, 2012 tuatara,This is not trivial as AutoIt does not have a DirRename and actually moves the folders. So you have to make sure you move them in the right order or the paths do not match. Give me a while and I will see what I can do. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
tuatara Posted March 14, 2012 Author Share Posted March 14, 2012 Thanks Melba23,It would be great if i could find the correct regular expression to remove the trailing folders after the folder with the blanks in the full pathIf i can change this: f:folder-is-okfolder with blanksfolder-is-also-okto:f:folder-is-okfolder with blanksbysomething like: $shortedDir = StringRegExpReplace($FullFilePath,'.*Z','')i was thinking that it might be possible to DirMove it that way?But the regular expression is not correct.It must be to end of string containing any character except blanks Link to comment Share on other sites More sharing options...
tuatara Posted March 14, 2012 Author Share Posted March 14, 2012 hmm . this one is not ok either: $shortedDir = StringRegExpReplace($FullFilePath,'.*Z','') Link to comment Share on other sites More sharing options...
tuatara Posted March 14, 2012 Author Share Posted March 14, 2012 this doesn't work either: StringRegExpReplace($FullFilePath,'.*[^]','') Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted March 14, 2012 Moderators Share Posted March 14, 2012 tuatara, That was fun! A little explanation first. You need to start by renaming the deepest folders so that the paths for the higher level folders still match - otherwise you get into all sorts of trouble. You also need to refresh Explorer after each rename so that Windows realises that you have done the rename - otherwise you only ever rename the deepest folder on a branch and the rest fail. So now we know what we have to do, this is how we do it: expandcollapse popup#include <Array.au3> ; Only for display purposes #include <RecFileListToArray.au3> ; Set the path under which we will replace spaces with underscores $sOldPath = "N:Test UnderScore" ; Work out the level by counting the StringReplace($sOldPath, "", "", 0, 2) $iShallowest = @extended ; Now list the folders $aList = _RecFileListToArray($sOldPath, "*", 2, 1, 0, 2) ; Parameters: ; $sOldPath - the path ; "*" - use this for folders as ther are probably no extensions ; 2 - return folders only ; 1 - recursive ; 1 - sort - not strictly necessary but it made it easy to check the result ; 2 - return the FULL PATH ; See what you get _ArrayDisplay($aList) ; If we found folders If IsArray($aList) Then ; Find deepest folder by counting the $iDeepest = 0 For $i = 1 To $aList[0] StringReplace($aList[$i], "", "", 0, 2) $iLevel = @extended If $iLevel > $iDeepest Then $iDeepest = $iLevel EndIf Next ; So start with the deepest folders For $iCurrentlevel = $iDeepest To $iShallowest Step -1 ; Refresh by rerunning the search $aList = _RecFileListToArray($sOldPath, "*", 2, 1, 0, 2) ; Now go through the list For $i = 1 To $aList[0] ; Checkif we match the current level StringReplace($aList[$i], "", "", 0, 2) If @extended = $iCurrentlevel Then ; Extract the folder name $sOldFolderName = StringRegExpReplace($aList[$i], "^.*", "") ; Change the spaces into underscores $sNewFolderName = StringReplace($sOldFolderName, " ", "_", 0, 2) ; Get the path of the folder by removing the old name from the full path $sFolderPath = StringReplace($aList[$i], $sOldFolderName, "", 0, 2) ; Move the folder $iRet = DirMove($aList[$i], $sFolderPath & $sNewFolderName) ; This just displays teh result of each move ConsoleWrite($iRet & " - " & $aList[$i] & " - " & $sFolderPath & $sNewFolderName & @CRLF) ; Now refresh Explorer Sleep(1000) ; Might be able to reduce this - I did not test too much _Update_Explorer() Sleep(1000) ; And this too EndIf Next Next EndIf ; Credit KaFu Func _Update_Explorer() Local $iOld = Opt("WinSearchChildren", True) Local $a = WinList("[CLASS:SHELLDLL_DefView]") For $i = 0 To UBound($a) - 1 DllCall("user32.dll", "long", "SendMessage", "hwnd", $a[$i][1], "int", 0x111, "int", 28931, "int", 0) Next Opt("WinSearchChildren", $iOld) EndFunc ;==>_Update_Explorer Let me know how you get on - it is working for me every time. And do ask if you have any questions. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
tuatara Posted March 14, 2012 Author Share Posted March 14, 2012 Wow , i fell of my chair, when i read this.... i am testing it right now ... Link to comment Share on other sites More sharing options...
tuatara Posted March 14, 2012 Author Share Posted March 14, 2012 YES YES YES It works perfectly !!! It took some time, before it reached the directories i was checking, but it is working great!!! Thanks you very very very much !!! And i am sure more people will be able to use this script in any way. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted March 14, 2012 Moderators Share Posted March 14, 2012 tuatara, Delighted I could help! M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area 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