Jump to content

Recommended Posts

Posted (edited)

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:

  Quote

 

i96lg.jpg

copy.jpg

Edited by clicklogin1
Posted

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

Posted

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.

  • Moderators
Posted

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:

  Reveal hidden contents

 

  • Moderators
Posted

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:

  Reveal hidden contents

 

Posted

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
Posted

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

:)

Posted

; 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 :)

Posted

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

 

 

Posted

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+ :)

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...