skysel Posted October 17, 2013 Share Posted October 17, 2013 (edited) Hello everyone, Below is a modified script, which I use to create folder names based on file names and then move each file in folder created. Which works good. However, the modification below I'm trying to achieve now is: - search for files, create folders based on that files (filename is xxxxxx_001.ext, so stringtrim takes care of _001.ext so it isn't included in folder name) - this works ok - function KopirajDatoteke() should now move the files, starting with xxxxxx_***.*** to folder xxxxxx. I am not sure how to achieve this, I would like some pointers. Basically, if part of filename matches folder name, then it should move to the folder that it matches with. Since I need to get Arrays of folders and files, I'm guessing both array functions should stay. I was looking at StringCompare for example, but I think its wrong direction. thanks. expandcollapse popup #include <File.au3> #include <Array.au3> #include <RecFileListToArray.au3> $DIR = "C:\1" $TIFF = "C:\1" KreirajMape() KopirajDatoteke() Func KreirajMape() $FileArray = _RecFileListToArray($DIR, "*", 1, 0, 1, 0) For $i = 1 to UBound($FileArray) - 1 ;need -1 or you'll go out of range $FileArray[$i] = StringTrimRight(StringRegExpReplace($FileArray[$i], '[^\_]+$', ''), 1) ;Use this just in case the extension is more than 3 letters like "Video.dvr-ms" Next _ArrayDelete($FileArray,0) For $i = 0 to Ubound($FileArray) - 1 DirCreate($DIR & "\" & $FileArray[$i]) Next EndFunc Func KopirajDatoteke() $FileArray = _RecFileListToArray($DIR, "*", 1, 0, 0, 2) $FolderArray = _RecFileListToArray($DIR, "*", 2, 0, 1, 2) For $i = 1 To $FileArray[0] Next For $i = 1 To $FolderArray[0] FileMove($FileArray[$i],$FolderArray[$i],8) Next ;_ArrayDisplay($FileArray) ;_ArrayDisplay($FolderArray) EndFunc Edited October 17, 2013 by skysel Link to comment Share on other sites More sharing options...
Moderators Solution Melba23 Posted October 17, 2013 Moderators Solution Share Posted October 17, 2013 skysel,I am feeling generous today: expandcollapse popup#include <File.au3> #include <Array.au3> #include <RecFileListToArray.au3> $DIR = "C:\1" $TIFF = "C:\1" KreirajMape() KopirajDatoteke() Func KreirajMape() $FileArray = _RecFileListToArray($DIR, "*", 1, 0, 1, 0) ; You have a count element - so why not use it? For $i = 1 To $FileArray[0] ; This SRER removes the need to trim the string $FileArray[$i] = StringRegExpReplace($FileArray[$i], '^(.*)_.*$', '$1') ; Why have 2 loops? Just do it all in the one ; Check if the folder already exists (there might be more than one file with the same base name) If Not FileExists($DIR & "\" & $FileArray[$i]) Then ; Create the folder DirCreate($DIR & "\" & $FileArray[$i]) EndIf Next EndFunc ;==>KreirajMape Func KopirajDatoteke() ; Just return the file name, not the full path - see the FileMove line below to understand why $FileArray = _RecFileListToArray($DIR, "*", 1) ; And re-read the folders $FolderArray = _RecFileListToArray($DIR, "*", 2, 0, 1, 2) For $i = 1 To $FileArray[0] ; You used the count element here!!!! ; Strip off the _***.*** as we did above $sFileBase = StringRegExpReplace($FileArray[$i], '^(.*)_.*$', '$1') ; Again, why 2 loops? ; Check the relevant folder exists If FileExists($DIR & "\" & $sFileBase) Then ; And move the file - note we need to add the different paths to the same file name FileMove($DIR & "\" &$FileArray[$i], $DIR & "\" &$sFileBase & "\" & $FileArray[$i], 8) EndIf Next EndFunc ;==>KopirajDatotekeThat works perfectly for me when tested on my machine. How about for you? And if you have any questions about how and why I have changed the script - or about any of the comments - please do ask. 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...
skysel Posted October 17, 2013 Author Share Posted October 17, 2013 Wow Melba23, many thanks for your generous help! I am constantly trying to improve in AutoIT, but since logic isn't my strong attribute, I am struggling with programming :-) That's why so many "errors" in the script, sometimes I don't understand how specific functions work exactly (like loops, ...) Thank you for commenting on the code as it will improve my understanding :-) skysel, I am feeling generous today: expandcollapse popup#include <File.au3> #include <Array.au3> #include <RecFileListToArray.au3> $DIR = "C:\1" $TIFF = "C:\1" KreirajMape() KopirajDatoteke() Func KreirajMape() $FileArray = _RecFileListToArray($DIR, "*", 1, 0, 1, 0) ; You have a count element - so why not use it? For $i = 1 To $FileArray[0] ; This SRER removes the need to trim the string $FileArray[$i] = StringRegExpReplace($FileArray[$i], '^(.*)_.*$', '$1') ; Why have 2 loops? Just do it all in the one ; Check if the folder already exists (there might be more than one file with the same base name) If Not FileExists($DIR & "\" & $FileArray[$i]) Then ; Create the folder DirCreate($DIR & "\" & $FileArray[$i]) EndIf Next EndFunc ;==>KreirajMape Func KopirajDatoteke() ; Just return the file name, not the full path - see the FileMove line below to understand why $FileArray = _RecFileListToArray($DIR, "*", 1) ; And re-read the folders $FolderArray = _RecFileListToArray($DIR, "*", 2, 0, 1, 2) For $i = 1 To $FileArray[0] ; You used the count element here!!!! ; Strip off the _***.*** as we did above $sFileBase = StringRegExpReplace($FileArray[$i], '^(.*)_.*$', '$1') ; Again, why 2 loops? ; Check the relevant folder exists If FileExists($DIR & "\" & $sFileBase) Then ; And move the file - note we need to add the different paths to the same file name FileMove($DIR & "\" &$FileArray[$i], $DIR & "\" &$sFileBase & "\" & $FileArray[$i], 8) EndIf Next EndFunc ;==>KopirajDatoteke That works perfectly for me when tested on my machine. How about for you? And if you have any questions about how and why I have changed the script - or about any of the comments - please do ask. M23 Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted October 17, 2013 Moderators Share Posted October 17, 2013 skysel,Glad I could help. And do ask if anything is unclear - improving your understanding is my aim too. 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...
skysel Posted October 17, 2013 Author Share Posted October 17, 2013 I will, thanks skysel, Glad I could help. And do ask if anything is unclear - improving your understanding is my aim too. M23 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