Jump to content
Sign in to follow this  
tuatara

_RecFileListToArray question

Recommended Posts

tuatara

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 by tuatara

Share this post


Link to post
Share on other sites
Melba23

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. :bye:

Give me a while and I will see what I can do. :oops:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
tuatara

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 path

If i can change this: f:folder-is-okfolder with blanksfolder-is-also-ok

to:

f:folder-is-okfolder with blanks

by

something 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

Share this post


Link to post
Share on other sites
tuatara

hmm . this one is not ok either:

$shortedDir = StringRegExpReplace($FullFilePath,'.*Z','')

Share this post


Link to post
Share on other sites
tuatara

this doesn't work either: StringRegExpReplace($FullFilePath,'.*[^]','')

Share this post


Link to post
Share on other sites
Melba23

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. :oops:

So now we know what we have to do, this is how we do it: :doh:

#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. :bye:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
tuatara

Wow , i fell of my chair, when i read this....

i am testing it right now ...

Share this post


Link to post
Share on other sites
tuatara

YES YES YES :oops:

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.

:bye:

Share this post


Link to post
Share on other sites
Melba23

tuatara,

Delighted I could help! :oops:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
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
Sign in to follow this  

×