Sign in to follow this  
Followers 0
skysel

Copy files to folders, if it matches specific pattern

5 posts in this topic

#1 ·  Posted (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.

 
#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 by skysel

Share this post


Link to post
Share on other sites



skysel,

I am feeling generous today: :D

#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? :huh:

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._______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

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: :D

#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? :huh:

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

 

Share this post


Link to post
Share on other sites

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._______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

I will, thanks :thumbsup:

skysel,

Glad I could help. And do ask if anything is unclear - improving your understanding is my aim too. :)

M23

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  
Followers 0