Jump to content

Copying files from many folders to one folder


Recommended Posts

I'm having trouble creating a folder on line 17.

When the folder doesn't exist the folder gets created.
When the folder does exist it gets deleted but the folder doesn't get created.

#include <WinAPIFiles.au3>
#include <File.au3>

Global $sFileToPath = "C:\MyPictures"
Global $sFileFromPath = @AppDataDir & "\Corel\Messages\540217727_100001\en\MessageCache1\Workflow\shared\Images"
Global $sExt = "jpg"
; Time to start over with a new set of files
Local $iFileExists = FileExists($sFileToPath)

If $iFileExists Then
    DirRemove($sFileToPath, $DIR_REMOVE)
EndIf
;~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~
;When the folder doesn't exist the folder gets created.
;When the folder does exist it gets deleted but the folder doesn't get created.
; On the next line the folder doesn't get created if it did exist.
DirCreate($sFileToPath)

MoveFiles($sFileFromPath, $sFileToPath, $sExt)

Func MoveFiles($sFileFromPath, $sFileToPath, $sExt = ".*")

    If Not StringRegExp($sFileFromPath, "\\\z") Then $sFileFromPath &= "\" ; Append missing '\' to $sFileFromPath if needed
    If Not StringRegExp($sFileToPath, "\\\z") Then $sFileToPath &= "\" ; Append missing '\' to $sFileToPath if needed
    FileChangeDir($sFileFromPath)
    $hSearch = FileFindFirstFile("*.*")
    $hFile = FileFindNextFile($hSearch)
    While Not @error
        If @extended Then ; $hFile is a directory
            MoveFiles($sFileFromPath & $hFile, $sFileToPath, $sExt) ; Scan the subdirectory
        Else          ; $hFile is a file
            FileChangeDir($sFileFromPath)
            Global $sFileFrom = $sFileFromPath & $hFile
            Global $sFileTo = $sFileToPath & $hFile
            $vResult = _SecureFileMove($sFileFrom, $sFileTo)
            If $vResult == 0 Then ; 0 = error
                MsgBox(0, "", "Error copping file to: " & $sFileTo)
            Else ; on success the name of the moved file is returned
                ;               MsgBox(0, "", "File successfully Copied to: " & $vResult)
            EndIf
            ;       Exit
            ;If StringRegExp($hFile, "(?i)\." & $sExt & "\z") Then FileCopy($hFile, $sFileToPath, 8 + 1) ; Copy $hFile to $sFileToPath (create directory if needed and overwrite existing files -- flag 8+1)
            ;               FileCopy($hFile, $sFileToPath) ; Copy $hFile to $sFileToPath (create directory if needed and overwrite existing files -- flag 8+1)
        EndIf
        $hFile = FileFindNextFile($hSearch)
    WEnd
    FileClose($hSearch)
EndFunc   ;==>MoveFiles

Func _SecureFileMove($sFileFrom, $sFileTo)

    Local $iIndex = 0, $sFileTemp, $iResult
    Local $sDrive, $sDir, $sFName, $sExt

    If FileExists($sFileTo) Then
        _PathSplit($sFileTo, $sDrive, $sDir, $sFName, $sExt)
        While 1
            $iIndex = $iIndex + 1
            $sFileTemp = $sDrive & $sDir & $sFName & "_" & $iIndex & $sExt
            If Not FileExists($sFileTemp) Then ExitLoop
        WEnd
        $sFileTo = $sFileTemp
    EndIf
    $iResult = FileCopy($sFileFrom, $sFileTo)
    If $iResult = 1 Then Return $sFileTo
    Return 0

EndFunc   ;==>_SecureFileMove

Thanks,

Docfxit

Edited by Docfxit
Link to comment
Share on other sites

This is the code I currently have:

Local $iFileExists = FileExists($sFileToPath)
If $iFileExists Then
    DirRemove($sFileToPath, $DIR_REMOVE)
EndIf
;~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~
;When the folder doesn't exist the folder gets created.
;When the folder does exist it gets deleted but the folder doesn't get created.
; On the next line the folder doesn't get created if it did exist.
DirCreate($sFileToPath)

I am currently making sure the folder doesn't exist by deleting it first.

Then I am trying to create the folder.

As I have said:  It works the first time I run it.  It doesn't work the second time I run it.

I don't see how your code will fix this problem.

Thanks,

Docfxit

Edited by Docfxit
Link to comment
Share on other sites

@Docfxit, not sure if this is what you want or mean.

On 6/8/2019 at 12:52 PM, Docfxit said:

When the folder does exist it gets deleted but the folder doesn't get created.

#include <File.au3>

Global $sFileToPath = "C:\MyPictures"
Global $sFileFromPath = @AppDataDir & "\Corel\Messages\540217727_100001\en\MessageCache1\Workflow\shared\Images"
Global $sExt = "jpg"
; Time to start over with a new set of files
Local $iFileExists = FileExists($sFileToPath)

If Not $iFileExists then
   DirCreate($sFileToPath)
Else
   DirRemove($sFileToPath, 1)
   DirCreate($sFileToPath) ; redeclared folder creation
EndIf
;...next code
;..and so on...

 

Edited by KickStarter15

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

@KickStarter15

Thank you for the code.  I like your approach.  I'm getting the same problem.

When the folder isn't there the script runs fine and creates the folder.

When the folder is there the script removes the folder but doesn't create the folder.

Does anyone know any error code that would show what the problem is?

Thanks,

Docfxit

 

Link to comment
Share on other sites

@Docfxit, sorry but everything is working fine in my testing.

Q: When the folder isn't there the script runs fine and creates the folder.

A: Yes, it does.

Q: When the folder is there the script removes the folder but doesn't create the folder.

A: It will copy all jpg files from different folders found in \image folder and create the folder.

After executing the code and run again, still everything is fine.🤨

#include <WinAPIFiles.au3>
#include <File.au3>

Global $sFileToPath = @ScriptDir & "\MyPictures"
Global $sFileFromPath = @AppDataDir & "\Corel\Messages\540217727_100001\en\MessageCache1\Workflow\shared\Images"
Global $sExt = "jpg"
; Time to start over with a new set of files
Local $iFileExists =  FileExists($sFileToPath)

If $iFileExists then
   DirCreate($sFileToPath)
Else
   DirRemove($sFileToPath, 1)
   DirCreate($sFileToPath)
EndIf
;~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~
;When the folder doesn't exist the folder gets created.
;When the folder does exist it gets deleted but the folder doesn't get created.
; On the next line the folder doesn't get created if it did exist.


MoveFiles($sFileFromPath, $sFileToPath, $sExt)

Func MoveFiles($sFileFromPath, $sFileToPath, $sExt = ".*")

    If Not StringRegExp($sFileFromPath, "\\\z") Then $sFileFromPath &= "\" ; Append missing '\' to $sFileFromPath if needed
    If Not StringRegExp($sFileToPath, "\\\z") Then $sFileToPath &= "\" ; Append missing '\' to $sFileToPath if needed
    FileChangeDir($sFileFromPath)
    $hSearch = FileFindFirstFile("*.*")
    $hFile = FileFindNextFile($hSearch)
    While Not @error
        If @extended Then ; $hFile is a directory
            MoveFiles($sFileFromPath & $hFile, $sFileToPath, $sExt) ; Scan the subdirectory
        Else          ; $hFile is a file
            FileChangeDir($sFileFromPath)
            Global $sFileFrom = $sFileFromPath & $hFile
            Global $sFileTo = $sFileToPath & $hFile
            $vResult = _SecureFileMove($sFileFrom, $sFileTo)
            If $vResult == 0 Then ; 0 = error
                MsgBox(0, "", "Error copping file to: " & $sFileTo)
                Exit
            Else ; on success the name of the moved file is returned
                ;               MsgBox(0, "", "File successfully Copied to: " & $vResult)
            EndIf
            ;       Exit
            ;If StringRegExp($hFile, "(?i)\." & $sExt & "\z") Then FileCopy($hFile, $sFileToPath, 8 + 1) ; Copy $hFile to $sFileToPath (create directory if needed and overwrite existing files -- flag 8+1)
            ;               FileCopy($hFile, $sFileToPath) ; Copy $hFile to $sFileToPath (create directory if needed and overwrite existing files -- flag 8+1)
        EndIf
        $hFile = FileFindNextFile($hSearch)
    WEnd
    FileClose($hSearch)
EndFunc   ;==>MoveFiles

Func _SecureFileMove($sFileFrom, $sFileTo)

    Local $iIndex = 0, $sFileTemp, $iResult
    Local $sDrive, $sDir, $sFName, $sExt

    If FileExists($sFileTo) Then
        _PathSplit($sFileTo, $sDrive, $sDir, $sFName, $sExt)
        While 1
            $iIndex = $iIndex + 1
            $sFileTemp = $sDrive & $sDir & $sFName & "_" & $iIndex & $sExt
            If Not FileExists($sFileTemp) Then ExitLoop
        WEnd
        $sFileTo = $sFileTemp
    EndIf
    $iResult = FileCopy($sFileFrom, $sFileTo)
    If $iResult = 1 Then Return $sFileTo
    Return 0

EndFunc   ;==>_SecureFileMove

 

Edited by KickStarter15

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

I have made some discoveries, but I don't understand how to solve the problem.

I found that the script runs fine if I don't open the folder to look at the files with Salamander.

After the sFileToPath is created I have been opening it to check to make sure the files are correct.  The user of this script will most probably do the same.  When that path is open and the script is run it does delete and create the path but something else is deleting the path.

After the path is deleted and created I have added a test and it test fine.  Later in the script I put in a second test and the sFileToPath isn't found.

What can I do to fix it?  This is my latest code:

#include <WinAPIFiles.au3>
#include <File.au3>
; This script will copy all files from many folders in the $sFileFromPath to one folder in the $sFileToPath
;    If it finds a duplicate file name it will add a number to the file name
;
Global $sFileToPath = "C:\MyPictures"
Global $sFileFromPath = @AppDataDir & "\Corel\Messages\540217727_100001\en\MessageCache1\Workflow\shared\Images"
Global $sExt = "jpg"
Local $bStatus = FileSetAttrib($sFileToPath, "-R")

; Time to start over with a new set of files
Local $iFileExists = FileExists($sFileToPath)
If Not $iFileExists Then
    DirCreate($sFileToPath)
Else
    DirRemove($sFileToPath, 1)
    DirCreate($sFileToPath) ; redeclared folder creation
EndIf
Local $iFileExists = FileExists($sFileToPath)
If Not $iFileExists Then
    MsgBox($MB_ICONERROR, "Error", "Folder not created = " & $sFileToPath & @CRLF & "At Line# = " & @ScriptLineNumber, @error)
    ; I expected this to pop up.  It didn't.
    Exit
EndIf

MoveFiles($sFileFromPath, $sFileToPath, $sExt)
;Protect the folder making it read only
Local $bStatus = FileSetAttrib($sFileToPath, "+R")
Exit

Func MoveFiles($sFileFromPath, $sFileToPath, $sExt = ".*")

    Local $iFileExists = FileExists($sFileToPath)
    If Not $iFileExists Then
        MsgBox($MB_ICONERROR, "Error", "Folder not created = " & $sFileToPath & @CRLF & "At Line# = " & @ScriptLineNumber, @error)
        ; This is not popping up.
        Exit
    EndIf
    If Not StringRegExp($sFileFromPath, "\\\z") Then $sFileFromPath &= "\" ; Append missing '\' to $sFileFromPath if needed
    If Not StringRegExp($sFileToPath, "\\\z") Then $sFileToPath &= "\" ; Append missing '\' to $sFileToPath if needed
    Local $iFileExists = FileExists($sFileToPath)
    If Not $iFileExists Then
        MsgBox($MB_ICONERROR, "Error", "Folder not created = " & $sFileToPath & @CRLF & "At Line# = " & @ScriptLineNumber, @error)
        ; This is popping up.
        Exit
    EndIf
    FileChangeDir($sFileFromPath)
    $hSearch = FileFindFirstFile("*.*")
    $hFile = FileFindNextFile($hSearch)
    While Not @error
        If @extended Then ; $hFile is a directory
            MoveFiles($sFileFromPath & $hFile, $sFileToPath, $sExt) ; Scan the subdirectory
        Else          ; $hFile is a file
            FileChangeDir($sFileFromPath)
            Global $sFileFrom = $sFileFromPath & $hFile
            Global $sFileTo = $sFileToPath & $hFile
            $vResult = _SecureFileMove($sFileFrom, $sFileTo)
            If $vResult == 0 Then ; 0 = error
                MsgBox(0, "", "Error copping file to: " & $sFileTo)
            Else ; on success the name of the moved file is returned
                ;               MsgBox(0, "", "File successfully Copied to: " & $vResult)
            EndIf
        EndIf
        $hFile = FileFindNextFile($hSearch)
    WEnd
    FileClose($hSearch)
EndFunc   ;==>MoveFiles

Func _SecureFileMove($sFileFrom, $sFileTo)

    Local $iIndex = 0, $sFileTemp, $iResult
    Local $sDrive, $sDir, $sFName, $sExt

    If FileExists($sFileTo) Then
        _PathSplit($sFileTo, $sDrive, $sDir, $sFName, $sExt)
        While 1
            $iIndex = $iIndex + 1
            $sFileTemp = $sDrive & $sDir & $sFName & "_" & $iIndex & $sExt
            If Not FileExists($sFileTemp) Then ExitLoop
        WEnd
        ;       MsgBox(0, "", "Duplicate file found: " & $sFileTemp)
        $sFileTo = $sFileTemp
    EndIf
    $iResult = FileCopy($sFileFrom, $sFileTo)
    If $iResult = 1 Then Return $sFileTo
    Return 0

EndFunc   ;==>_SecureFileMove

 

Edited by Docfxit
Link to comment
Share on other sites

@Docfxit

9 hours ago, Docfxit said:

but something else is deleting the path

Is that something is another program? or just your code.

 

9 hours ago, Docfxit said:

Later in the script I put in a second test and the sFileToPath isn't found.

This means the path was open in the background that causes your "DirCreate($sFileToPath)" won't work. This is the same issue that I've got before with the same conditions that i made. What I did is I made a unique folder name to avoid issues in the future.

In you case, truly you can't create a path "sFileToPath" because it was opened. Close that first to properly execute the code.

 

Edited by KickStarter15

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

I have added code to check to see if the folder is open.  The file really is open but the check I am doing says it isn't open.

Maybe someone will know how to fix my code to close the program that has the folder open.

This is my latest code:

#include <WinAPIFiles.au3>
#include <File.au3>
; This script will copy all files from many folders in the $sFileFromPath to one folder in the $sFileToPath
;    If it finds a duplicate file name it will add a number to the file name
;
Global $sFileToPath = "C:\MyPictures"
Global $sFileToPathSlash = $sFileToPath
Global $sFileFromPath = @AppDataDir & "\Corel\Messages\540217727_100001\en\MessageCache1\Workflow\shared\Images"
Global $sExt = "jpg"
Global $sCount
Local $bStatus = FileSetAttrib($sFileToPath, "-R")

; Time to start over with a new set of files
Local $iFileExists = FileExists($sFileToPath)
If Not $iFileExists Then
    DirCreate($sFileToPath)
Else
    While _IsFileInUse($sFileToPath)
        Sleep(500)
    WEnd
    DirRemove($sFileToPath, 1)
    DirCreate($sFileToPath) ; redeclared folder creation
EndIf
Local $iFileExists = FileExists($sFileToPath)
_CheckIfFolderIsPresent(@ScriptLineNumber)
If Not StringRegExp($sFileToPath, "\\\z") Then $sFileToPathSlash &= "\"     ; Append missing '\' to $sFileToPath if needed
_CheckIfFolderIsPresent(@ScriptLineNumber)
MoveFiles($sFileFromPath, $sFileToPathSlash, $sExt)
;Protect the folder making it read only
Local $bStatus = FileSetAttrib($sFileToPath, "+R")
Exit

Func MoveFiles($sFileFromPath, $sFileToPathSlash, $sExt = ".*")
    _CheckIfFolderIsPresent(@ScriptLineNumber)
    If Not StringRegExp($sFileFromPath, "\\\z") Then $sFileFromPath &= "\" ; Append missing '\' to $sFileFromPath if needed
    _CheckIfFolderIsPresent(@ScriptLineNumber)

    FileChangeDir($sFileFromPath)
    $hSearch = FileFindFirstFile("*.*")
    $hFile = FileFindNextFile($hSearch)
    While Not @error
        If @extended Then ; $hFile is a directory
            MoveFiles($sFileFromPath & $hFile, $sFileToPathSlash, $sExt) ; Scan the subdirectory
            _CheckIfFolderIsPresent(@ScriptLineNumber)
        Else          ; $hFile is a file
            _CheckIfFolderIsPresent(@ScriptLineNumber)
            FileChangeDir($sFileFromPath)
            Global $sFileFrom = $sFileFromPath & $hFile
            Global $sFileTo = $sFileToPathSlash & $hFile
            _CheckIfFolderIsPresent(@ScriptLineNumber)
            $vResult = _SecureFileMove($sFileFrom, $sFileTo)
            ;   Folder not created = C:\MyPictures Error pops up here at line 51
            _CheckIfFolderIsPresent(@ScriptLineNumber)
            If $vResult == 0 Then ; 0 = error
                MsgBox(0, "", "Error copying file to: " & $sFileTo & @CRLF & "At Line# = " & @ScriptLineNumber & @CRLF & "$vResult = " & $vResult)
            Else ; on success the name of the moved file is returned
                ;               MsgBox(0, "", "File successfully Copied to: " & $vResult)
            EndIf
            EndIf
        $hFile = FileFindNextFile($hSearch)
    WEnd
    FileClose($hSearch)
EndFunc   ;==>MoveFiles

Func _SecureFileMove($sFileFrom, $sFileTo)

    Local $iIndex = 0, $sFileTemp, $iResult
    Local $sDrive, $sDir, $sFName, $sExt

    If FileExists($sFileTo) Then
        _PathSplit($sFileTo, $sDrive, $sDir, $sFName, $sExt)
        While 1
            $iIndex = $iIndex + 1
            $sFileTemp = $sDrive & $sDir & $sFName & "_" & $iIndex & $sExt
            If Not FileExists($sFileTemp) Then ExitLoop
        WEnd
        ;       MsgBox(0, "", "Duplicate file found: " & $sFileTemp)
        $sFileTo = $sFileTemp
    EndIf
    _CheckIfFolderIsPresent(@ScriptLineNumber)
    $iResult = FileCopy($sFileFrom, $sFileTo)
    If $iResult = 1 Then
        Return $sFileTo
    Else
        $sCount = $sCount + 1 ; Stopping on the first file
        MsgBox(0, "", "Error copying file to: " & $sFileTo & @CRLF & "At Line# = " & @ScriptLineNumber & @CRLF & "$iResult = " & $iResult & @CRLF & "$sCount = " & $sCount)
        ;   When the problem happens This MsgBox pops up
        ;   $iResult = 0
        ;   $sCount = 1
        Return 0
    EndIf

EndFunc   ;==>_SecureFileMove

Func _CheckIfFolderIsPresent($iLineNumer)
    Local $iFileExists = FileExists($sFileToPath)
    If Not $iFileExists Then
        $iLineNumer = $iLineNumer - 1
        MsgBox($MB_ICONERROR, "Error", "Folder not created = " & $sFileToPath & @CRLF & "At Line# = " & $iLineNumer, @error)
        ;   When the problem happens This MsgBox pops up From Line# 51
        Exit
    EndIf
EndFunc   ;==>_CheckIfFolderIsPresent


Func _IsFileInUse($sPath) ;Return 0 if not in use
    Local $nError, $hOpen = FileOpen($sPath, 1)
    $nError = @error
    MsgBox($MB_ICONERROR, "Error", "Folder In Use = " & $sPath & @CRLF & "@error = " & $nError)
    ;   When the problem happens This MsgBox pops up
    ;   @error = 0
    FileClose($hOpen)
    Return $nError
EndFunc   ;==>_IsFileInUse

 

Thank you for the great suggestion,

Docfxit

Link to comment
Share on other sites

6 hours ago, FrancescoDiMuro said:

@Docfxit
A bit "off-topic", but if the task is to copy some files from a source directory to a source directory, then use something like XCOPY command, from which you can set a lot of parameters for the copy process :)

There are lots of ways to copy files. I'm not having a problem copying files.  The problem I am having is deleting and recreating the folder I am copying the files to.

Thanks,

Docfxit

Link to comment
Share on other sites

33 minutes ago, Earthshine said:

why not just delete the folders contents and continue to copy data to it? why delete the folder? 

Also, you are moving files, not

That's a good idea.

I tried this code:

Local $bStatus = FileSetAttrib($sFileToPath & "\*.*", "-R")

; Time to start over with a new set of files
Local $iFileExists = FileExists($sFileToPath)
If Not $iFileExists Then
    DirCreate($sFileToPath)
Else
FileDelete($sFileToPath & "\*.*")
        MsgBox(0, "", "Check the files in: " & $sFileToPath & @CRLF & "At Line# = " & @ScriptLineNumber)
EndIf

I have it working with this code.

That solved the problem.

 

Edited by Docfxit
Link to comment
Share on other sites

dude, your code is really messy for what you are doing. try this udf i wrote as an example. In your main script, include this file, then in main script just feed it from path, to-path and you can include file types, or if you leave $filespec blank it is assumed you want everything. Save this file as BackupFiles.au3 and include it. You can comment out the stuff to massage the copy-to file path. I have done this as it is used to create backups of users data

#include-once
#include <Date.au3>
#include <FileConstants.au3>
#include "log4a.au3"


; #FUNCTION# ====================================================================================================================
; Name ..........: _BackupFiles
; Description ...:
; Syntax ........: _BackupFiles($from, $to, $filespec)
; Parameters ....: $from        - dirctory you wish to back up
;                  $to          - base directory where output goes
;                  $filespec    - filename. supports wildcards
;                               - if $filespec = '' then it copies entire directory and all files
;                               - Error Code set to 1 if failure so it will be in @error to the returning function
;                               - should the $TargetDir be '' blank, either way you know you have an error
; Return values .: Unique, Time/Date stamped folder name containing the backups
; Author ........: Earthshine
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _BackupFiles($from, $to, $filespec = "")
    ; Get the time
    Local $sTime = _Date_Time_GetSystemTime()
    ; convert to string
    $sTime = _Date_Time_SystemTimeToDateTimeStr($sTime)
    _log4a_Info('Captured System Date and Time String: ' & $sTime)

    Local $rep1 = StringReplace($sTime, " ", "-")
    Local $rep2 = StringReplace($rep1, "/", "-")
    Local $rep3 = StringReplace($rep2, ":", "-")
    $rep3 = StringReplace($rep2, ":", "-")
    ; Set dir string
    Local $TargetDir = $to &"\Backups\BACKUP-" & $rep3

    If (DirCreate($TargetDir) > 0) Then
        _log4a_Info('Creation of Backup Directory Successful: ' & $TargetDir)
        ; copy file to backup location
        If ( Stringlen($filespec) = 0 ) Then
            If (DirCopy($from, $TargetDir, 1) = 1) Then
                _log4a_Info('Directory Copy Successful: ' & $TargetDir & '\' & $filespec)
            Else
                _log4a_Error('Directory Copy Unsuccessful: ' & $TargetDir & '\' & $filespec)
            Endif
        Else
            If (FileCopy($from &"\"&$filespec, $TargetDir, 1) = 1) Then
                _log4a_Info('File Copy Successful: ' & $TargetDir & '\' & $filespec)
            Else
                _log4a_Error('File Copy Unsuccessful: ' & $TargetDir & '\' & $filespec)
            EndIf
        EndIf
    Else
        _log4a_Error('Creation of Backup Directory Failed: ' & $TargetDir)
        $TargetDir = ''
        SetError (1)
    EndIf

    return($TargetDir)
EndFunc

Just use this as an example and make this function do what you want it to do, also you need to download log4a.au3 and include it. 

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

I found an unviewable character after Pictures.  I have removed it and it's working now.

This is my entire script:

#include <WinAPIFiles.au3>
#include <File.au3>
; This script will copy all files from many folders in the $sFileFromPath to one folder in the $sFileToPath
;    If it finds a duplicate file name it will add a number to the file name
;
Global $sFileToPath = "C:\MyPictures"
Global $sFileToPathSlash = $sFileToPath
;Global $sFileFromPath = @AppDataDir & "\Corel\Messages\540217727_100001\en\MessageCache1\Workflow\shared\Images"
Global $sFileFromPath = @UserProfileDir & "\Pictures"
MsgBox(0, "", "From file Path: " & $sFileFromPath & @CRLF & "At Line# = " & @ScriptLineNumber)
Global $sExt = "jpg"
Local $bStatus = FileSetAttrib($sFileToPath & "\*.*", "-RS")
; Time to start over with a new set of files
Local $iFileExists = FileExists($sFileToPath)
If Not $iFileExists Then
    DirCreate($sFileToPath)
Else
    FileDelete($sFileToPath & "\*.*")
EndIf
If Not StringRegExp($sFileToPath, "\\\z") Then $sFileToPathSlash &= "\"     ; Append missing '\' to $sFileToPath if needed
MoveFiles($sFileFromPath, $sFileToPathSlash, $sExt)
;Protect the folder making it read only
Local $bStatus = FileSetAttrib($sFileToPath & "\*.*", "+RS")
Local $bStatus = FileSetAttrib($sFileToPath, "+RS")
Exit

Func MoveFiles($sFileFromPath, $sFileToPathSlash, $sExt = ".*")
    If Not StringRegExp($sFileFromPath, "\\\z") Then $sFileFromPath &= "\" ; Append missing '\' to $sFileFromPath if needed

    FileChangeDir($sFileFromPath)
    $hSearch = FileFindFirstFile("*.*")
    $hFile = FileFindNextFile($hSearch)
    While Not @error
        If @extended Then ; $hFile is a directory
            MoveFiles($sFileFromPath & $hFile, $sFileToPathSlash, $sExt) ; Scan the subdirectory
        Else          ; $hFile is a file
            FileChangeDir($sFileFromPath)
            Global $sFileFrom = $sFileFromPath & $hFile
            Global $sFileTo = $sFileToPathSlash & $hFile
            $vResult = _SecureFileMove($sFileFrom, $sFileTo)
            ;   Folder not created = C:\MyPictures Error pops up here at line 51
            If $vResult == 0 Then ; 0 = error
                MsgBox(0, "", "Error copying file to: " & $sFileTo & @CRLF & "From file: " & $sFileFrom & @CRLF & "At Line# = " & @ScriptLineNumber & @CRLF & "$vResult = " & $vResult)
            Else ; on success the name of the moved file is returned
                ;               MsgBox(0, "", "File successfully Copied to: " & $vResult)
            EndIf
        EndIf
        $hFile = FileFindNextFile($hSearch)
    WEnd
    FileClose($hSearch)
EndFunc   ;==>MoveFiles

Func _SecureFileMove($sFileFrom, $sFileTo)

    Local $iIndex = 0, $sFileTemp, $iResult
    Local $sDrive, $sDir, $sFName, $sExt

    If FileExists($sFileTo) Then
        _PathSplit($sFileTo, $sDrive, $sDir, $sFName, $sExt)
        While 1
            $iIndex = $iIndex + 1
            $sFileTemp = $sDrive & $sDir & $sFName & "_" & $iIndex & $sExt
            If Not FileExists($sFileTemp) Then ExitLoop
        WEnd
        ;       MsgBox(0, "", "Duplicate file found: " & $sFileTemp)
        $sFileTo = $sFileTemp
    EndIf
    $iResult = FileCopy($sFileFrom, $sFileTo)
    If $iResult = 1 Then
        Return $sFileTo
    Else
        Return 0
    EndIf

EndFunc   ;==>_SecureFileMove

Thanks for the help.

Docfxit

Edited by Docfxit
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...