Jump to content

help: copy file to folder


Recommended Posts

everyday, i received many fixed .xls file. EX: abc mmm.xls, hhh klm.xls. lkj hgf.xls. I put all in a folder, ex:C:dailymail.

i have many fixed folder with special name,.EX: mmm,klm,hgf...(a part of filename i received).

everyday, i have to copy manual many file to many folder. EX: C:dailymailabc mmm.xls into mmm, C:dailymailhhh klm.xls into klm...

can you help me do it automatically.

Thanks so much.

(My English is not good)

update image:

 

i96lg.jpg

copy.jpg

Edited by clicklogin1
Link to comment
Share on other sites

clicklogin,

1 - Is the target folder always that part of the filename between a space and a period? 

     e.g. c:testmy new dataset.xls target folder = "dataset"

2 - Are all of the files that you receive in a specific folder? 

3 - If 2 is 'yes', then do you want to move all files everytime the automation runs?

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

1. filename contain folder name. eg:

folder i received many file. eg C:daily mail

file name:

data bush obama xxx.xls

mail la ny xyz.xls

...

target folder .eg "D:data mail" have many folder

bush obama

la ny

...

i have to copy file "C:daily maidata bush obama xxx.xls" to folder "D:data mailbush obama"

i have to copy file "C:daily maimail la ny xyz.xls" to folder "D:data mailla ny"

note:

xxx, xyz: random string

thanks.

Link to comment
Share on other sites

  • Moderators

clicklogin and clicklogin1,

You appear to have multiple accounts - this is not permitted here. I can merge them into a single account - which one do you wish to keep. :huh:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
Share on other sites

  • Moderators

clicklogin1,

No problem - the accounts have been merged as you will see if you refresh the page. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
Share on other sites

This is untested, but might do what you want without making duplicates:

#include <File.au3>

$sPath = @HomeDrive & "\daily mai"
$aFiles = _FileListToArray($sPath)


$sBushObama = "D:\data mail\bush obama"
$sLaNy = "D:\data mail\la ny"


For $i = 0 To UBound($aFiles) - 1
  If StringInStr($aFiles[$i], "Bush Obama") && Not FileExists($sBushObama & "\" & $aFiles[$i]) Then FileCopy($sPath & "\" & $aFiles[i], $sBushObama & "\" & $aFiles[$i])
  If StringInStr($aFiles[$i], "la ny") && Not FileExists($sLaNy & "\" & $aFiles[$i]) Then FileCopy($sPath & "\" & $aFiles[i], $sLaNy & "\" & $aFiles[$i])
Next
Link to comment
Share on other sites

With the pattern in post #3, this code may do as requested.

; set paths
$source = 'C:\daily mail'
$destination = 'D:\daily mail'

; check if paths exist
For $1 In StringSplit($source & '|' & $destination, '|', 2); no count
    If Not FileExists($1) Then
        MsgBox(0x40030, @ScriptName, '"' & $1 & '" does not exist' & @CRLF)
        Exit 1
    EndIf
Next

; change to source directory
If FileChangeDir($source) Then
    ; open a handle to find xls files
    $handle_find = FileFindFirstFile('*.xls')
    ; check if the handle is valid
    If $handle_find = -1 Then
        MsgBox(0x40030, @ScriptName, 'No xls files found')
        Exit 2
    EndIf
    ;
    While 1
        ; look for the next file
        $file_found = FileFindNextFile($handle_find)
        If @error Then ExitLoop
        ; check the file name with this pattern which returns the group in the name
        $folder = StringRegExpReplace($file_found, '\A\w{4} (.*) \w{3}\.xls\Z', '$1')
        ; if replacements occurred and folder is something
        If @extended And $folder Then
            ; if destination file exists then restart the loop
            If FileExists($destination & '\' & $folder & '\' & $file_found) Then
                ContinueLoop
            EndIf
            ; create the destination\folder
            If DirCreate($destination & '\' & $folder) Then
                ; copy the file to the destination\folder
                If Not FileCopy($file_found, $destination & '\' & $folder & '\') Then
                    ConsoleWriteError('Failed to copy "' & $file_found & '"' & @CRLF)
                EndIf
            Else
                ConsoleWriteError('Failed to create folder "' & $folder & '"' & @CRLF)
            EndIf
        Else
            ConsoleWriteError('File "' & $file_found & '" did not match the pattern' & @CRLF)
        EndIf
    WEnd
    ; close the find xls files handle
    FileClose($handle_find)
Else
    MsgBox(0x40030, @ScriptName, 'Unable to change working directory to "' & $source & '"' & @CRLF)
EndIf

:)

Link to comment
Share on other sites

; set paths
$source = 'C:\daily mail'
$destination = 'D:\daily mail'

; set regex pattern which returns the folder in the group
$pattern = '.*_(.*?)_\d+\.xls\Z'


; check if paths exist
For $1 In StringSplit($source & '|' & $destination, '|', 2); no count
    If Not FileExists($1) Then
        MsgBox(0x40030, @ScriptName, '"' & $1 & '" does not exist' & @CRLF)
        Exit 1
    EndIf
Next

; change to source directory
If FileChangeDir($source) Then
    ; open a handle to find xls files
    $handle_find = FileFindFirstFile('*.xls')
    ; check if the handle is valid
    If $handle_find = -1 Then
        MsgBox(0x40030, @ScriptName, 'No xls files found')
        Exit 2
    EndIf
    ;
    While 1
        ; look for the next file
        $file_found = FileFindNextFile($handle_find)
        If @error Then ExitLoop
        ; check the file name with this pattern which returns the group in the name
        $folder = StringRegExpReplace($file_found, $pattern, '$1')
        ; if replacements occurred and folder is something
        If @extended And $folder Then
            ; if destination file exists then restart the loop
            If FileExists($destination & '\' & $folder & '\' & $file_found) Then
                ContinueLoop
            EndIf
            ; create the destination\folder
            If DirCreate($destination & '\' & $folder) Then
                ; copy the file to the destination\folder
                If Not FileCopy($file_found, $destination & '\' & $folder & '\') Then
                    ConsoleWriteError('Failed to copy "' & $file_found & '"' & @CRLF)
                EndIf
            Else
                ConsoleWriteError('Failed to create folder "' & $folder & '"' & @CRLF)
            EndIf
        Else
            ConsoleWriteError('File "' & $file_found & '" did not match the pattern' & @CRLF)
        EndIf
    WEnd
    ; close the find xls files handle
    FileClose($handle_find)
Else
    MsgBox(0x40030, @ScriptName, 'Unable to change working directory to "' & $source & '"' & @CRLF)
EndIf

Updated to match :)

Link to comment
Share on other sites

thanks.

script create new folder, but i need copy to exits folder.

copy2.jpg

C:daily mail

more than 4 .xls files (100+ files)

eg:

D:daily mail

more than 4 folder (30+ folders)

.xls files in C:daily mail many name. But a part string of file name is folder name in D:daily mail

eg:

C:daily mailsecret_canada money_sate bank_bush obama_2013.xls

C:daily mailmail_acc dept_bush obama_11.xls

2 files above copy to D:daily mailhuman rightbush obama

copy3.jpg

 

 

Link to comment
Share on other sites

I am not sure where you can get some of those folder names from so perhaps you need to set some FileCopy functions up manually.

; set paths
$source = 'C:\daily mail'
$destination = 'D:\daily mail'

If FileChangeDir($source) Then
    FileCopy('*_nasa cia_*.xls', $destination & '\bussiness dept\nasa cia\', 8)
    FileCopy('*_white house_*.xls', $destination & '\bussiness dept\white house\', 8)
    FileCopy('*_bush obama_*.xls', $destination & '\human right\bush obama\', 8)
    FileCopy('*_ly na_*.xls', $destination & '\human right\ly na\', 8)
    ; FileCopy('*_folder name_*.xls', $destination & '\subject\folder name\', 8)
Else
    MsgBox(0x40030, @ScriptName, 'Unable to change working directory to "' & $source & '"' & @CRLF)
EndIf

That is 4. You can follow the pattern with the other 30+ :)

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