Jump to content

Need help with my script.


avatar56
 Share

Recommended Posts

Ik wil de inhoud van een Dir\ *.* kopiëren naar een andere Dir en de bestanden daar overschrijven. Hoe doe ik dat in Autoit?

 

DirCopy ($sfldr1  & "Vervang\*.*" , @LocalAppDataDir ,9)

Edited by avatar56

Thanks from Avatar56

Link to comment
Share on other sites

I want to copy the contents of a Dir\ *.* to another Dir and overwrite the files there. How do I do that in Autoit?

 

DirCopy ($sfldr1 &  "Replace\*.*"  , @LocalAppDataDir ,9)

 

Hope you can help me

Edited by avatar56

Thanks from Avatar56

Link to comment
Share on other sites

  • Developers

I understood the first time when asked in Dutch. ;)

So did you check the helpfile for FileCopy() or is there something unclear?

 

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

So if you simply want to copy files from a directory to another directory with a specific mask then I would think the helpfle example shows you how that could be done.
in case you need to do something else then I guess I am not understanding exactly what you are looking for. 

Jos 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

Think we are going around in circles...  So again: the example in the helpfile shows you how to do it... What isn't working when you use it?  How does the codee look you tried?

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

25 minuten geleden zei Jos:

Think we are going around in circles...  So again: the example in the helpfile shows you how to do it... What isn't working when you use it?  How does the codee look you tried?

Jos

DirCopy ($sfldr1 & "Replace\*. *" , @LocalAppDataDir,)

Thanks from Avatar56

Link to comment
Share on other sites

  • Developers

We use English in the public forum, but just pm me in dutch when you want. ;)

Ps you are still not using FileCopy()!

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

The help file example for the FileCopy function is doing exactly what you want (as Jos mentioned). Check it out: https://www.autoitscript.com/autoit3/docs/functions/FileCopy.htm

It is an example of using FileCopy to copy all files in a folder to another folder using a mask (like your example).

DirCopy() is for moving everything in a folder to another location, and it's failing for you because you're using a mask in a function that doesn't support it. You've basically passed in an invalid directory for the first parameter.

We ought not to misbehave, but we should look as though we could.

Link to comment
Share on other sites

@avatar56 You can use the following function _DirCopyInside() to copy directories:

;#include <MsgBoxConstants.au3>
#include <File.au3>

Global $sPath_SourceDir = @ScriptDir & '\TEST'
Global $sPath_DestDir = @ScriptDir & '\MySoftware'

_DirCopyInside($sPath_SourceDir, $sPath_DestDir)


Func _DirCopyInside($sPath_SourceDir, $sPath_DestDir, $FD_OVERWRITE = 1)
    If Not StringInStr($sPath_SourceDir, ":") Then $sPath_SourceDir = _PathFull($sPath_SourceDir)
    If Not FileExists($sPath_SourceDir) Then Return SetError(-1, 0, 0)
    If ($FD_OVERWRITE = Default) Or ($FD_OVERWRITE <> 0) Then $FD_OVERWRITE = 1
    Local $OptionCopy_File = $FD_OVERWRITE, $OptionCopy_Dir = $FD_OVERWRITE + $FC_CREATEPATH
    Local $aFileList = _FileListToArray($sPath_SourceDir, '*', $FLTA_FILESFOLDERS, True)
    If IsArray($aFileList) Then
        If Not StringInStr($sPath_DestDir, ":") Then $sPath_DestDir = _PathFull($sPath_DestDir)
        $sPath_DestDir = _PathFix($sPath_DestDir)
        $sPath_DestDir = _PathRemove_Backslash($sPath_DestDir)
        If _IsFile($sPath_DestDir) Then
            ConsoleWrite('! ERROR: The destination directory cannot be a file:' & $sPath_DestDir & @CRLF)
            ;MsgBox($MB_ICONERROR + $MB_TOPMOST, '/!\ Copy Error !', 'The destination directory cannot be a file!' & @CRLF & '-> ' & $sPath_DestDir)
            Return SetError(1, 0, 0)
        EndIf
        If Not FileExists($sPath_DestDir) Then DirCreate($sPath_DestDir)
        $sPath_DestDir &= '\'
        ConsoleWrite('> Source Dir: ' & $sPath_SourceDir & @CRLF)
        ConsoleWrite('> Dest Dir: ' & $sPath_DestDir & @CRLF)
        Local $iError = 0, $iReturn = 0
        For $i = 1 To $aFileList[0]
            Local $sFileName = _PathGet_Part($aFileList[$i], 6)
            Local $sDirName = _PathRemove_Backslash(_PathGet_Part($aFileList[$i], 3))
            If _IsFile($aFileList[$i]) Then
                ConsoleWrite('- Copy File: ' & $aFileList[$i] & ' > TO > ' & $sPath_DestDir & $sFileName & @CRLF)
                $iReturn += FileCopy($aFileList[$i], $sPath_DestDir & $sFileName, $OptionCopy_File)
                $iError += @error
            Else
                ConsoleWrite('+ Copy Directory: ' & $aFileList[$i] & ' > TO > ' & $sPath_DestDir & $sDirName & @CRLF)
                $iReturn += DirCopy($aFileList[$i], $sPath_DestDir & $sDirName, $OptionCopy_Dir)
                $iError += @error
            EndIf
        Next
        Return SetError($iError, 0, $iReturn)
    Else
        ConsoleWrite('! Directory ' & $sPath_SourceDir & ' does not exist any files or directories!' & @CRLF)
        Return SetError(2, 0, 0)
    EndIf
EndFunc   ;==>_DirCopyInside
; * -----:|
Func _PathGet_Part($sFilePath, $returnPart = 0)
    ;ConsoleWrite(";~ + [" & $returnPart & "] PATH IN : " & $sFilePath & @CRLF)
    $sFilePath = _PathFix($sFilePath)
    Local $sDrive, $sParentDir, $sCurrentDir, $sFileNameNoExt, $sExtension, $sFileName, $sPathParentDir, $sPathCurrentDir, $sPathFileNameNoExt
    Local $aPathSplit = _SplitPath($sFilePath, $sDrive, $sParentDir, $sCurrentDir, $sFileNameNoExt, $sExtension, $sFileName, $sPathParentDir, $sPathCurrentDir, $sPathFileNameNoExt)
    ;Local $sCurrentDirPath= $sDrive&$sCurrentDir;StringRegExpReplace($aPathSplit, '\\[^\\]*$', '')
    ;Local $sCurrentDirName =StringRegExpReplace(_PathRemoveBackslash($sCurrentDirPath), '.*\\', '')
;~     ConsoleWrite(";~ - [1] Drive: " & $sDrive & @CRLF)
;~     ConsoleWrite(";~ - [2] ParentDir: " & $sParentDir & @CRLF)
;~     ConsoleWrite(";~ - [3] CurrentDir: " & $sCurrentDir & @CRLF)
;~     ConsoleWrite(";~ - [4] FileName NoExt: " & $sFileNameNoExt & @CRLF)
;~     ConsoleWrite(";~ - [5] Extension: " & $sExtension & @CRLF)
;~     ConsoleWrite(";~ - [6] FileName: " & $sFileName & @CRLF)
;~     ConsoleWrite(";~ - [7] PathParentDir: " & $sPathParentDir & @CRLF)
;~     ConsoleWrite(";~ - [8] PathCurrentDir: " & $sPathCurrentDir & @CRLF)
;~     ConsoleWrite(";~ - [9] PathFileName NoExt: " & $sPathFileNameNoExt & @CRLF)
    ;If (StringStripWS($sFileName, 8) == '') Then ConsoleWrite(";~ ! This path does not contain filenames and extensions!" & @CRLF)
    Switch $returnPart
        Case 1
            ;ConsoleWrite(";~ - [1] Drive: " & $sDrive & @CRLF)
            Return $sDrive
        Case 2
            ;ConsoleWrite(";~ - [2] ParentDir: " & $sParentDir & @CRLF)
            Return $sParentDir
        Case 3
            ;ConsoleWrite(";~ - [3] CurrentDir: " & $sCurrentDir & @CRLF)
            Return $sCurrentDir
        Case 4
            ;ConsoleWrite(";~ - [4] FileName NoExt: " & $sFileNameNoExt & @CRLF)
            Return $sFileNameNoExt
        Case 5
            ;ConsoleWrite(";~ - [5] Extension: " & $sExtension & @CRLF)
            Return $sExtension
        Case 6
            ;ConsoleWrite(";~ - [6] FileName: " & $sFileNameNoExt & $sExtension & @CRLF)
            Return $sFileName
        Case 7
            ;ConsoleWrite(";~ - [7] PathParentDir: " & $sDrive & $sParentDir & @CRLF)
            Return $sPathParentDir
        Case 8
            ;ConsoleWrite(";~ - [8] PathCurrentDir: " & $sDrive & $sParentDir & $sCurrentDir & @CRLF)
            Return $sPathCurrentDir
        Case 9
            ;ConsoleWrite(";~ - [9] PathFileName NoExt: " & $sDrive & $sParentDir & $sCurrentDir & $sFileNameNoExt & @CRLF)
            Return $sPathFileNameNoExt
        Case Else
            ;ConsoleWrite("! [" & $returnPart & "] PATH OUT : " & $sFilePath & @CRLF)
            Return $sFilePath
    EndSwitch
EndFunc   ;==>_PathGet_Part
; * -----:|
Func _SplitPath($sFilePath, ByRef $sDrive, ByRef $sParentDir, ByRef $sCurrentDir, ByRef $sFileNameNoExt, ByRef $sExtension, ByRef $sFileName, ByRef $sPathParentDir, ByRef $sPathCurrentDir, ByRef $sPathFileNameNoExt)
    ;ConsoleWrite(@CRLF & ";~ + PATH IN : " & $sFilePath & @CRLF)
    $sFilePath = _PathFix($sFilePath)
    _PathSplit_Ex($sFilePath, $sDrive, $sParentDir, $sCurrentDir, $sFileNameNoExt, $sExtension)
    ;Local $sCurrentDirPath= $sDrive&$sCurrentDir;StringRegExpReplace($aPathSplit, '\\[^\\]*$', '')
    ;Local $sCurrentDirName =StringRegExpReplace(_PathRemoveBackslash($sCurrentDirPath), '.*\\', '')
    $sFileName = $sFileNameNoExt & $sExtension
    $sPathParentDir = $sDrive & $sParentDir
    $sPathCurrentDir = $sDrive & $sParentDir & $sCurrentDir
    $sPathFileNameNoExt = $sDrive & $sParentDir & $sCurrentDir & $sFileNameNoExt
    Local $aSplitPath[10] = [$sDrive, $sParentDir, $sCurrentDir, $sFileNameNoExt, $sExtension, $sFileName, $sPathParentDir, $sPathCurrentDir, $sPathFileNameNoExt]
    Return $aSplitPath
EndFunc   ;==>_SplitPath
; * -----:|
Func _PathSplit_Ex($sFilePath, ByRef $sDrive, ByRef $sParentDir, ByRef $sCurrentDir, ByRef $sFileNameNoExt, ByRef $sExtension)
    $sFilePath = _PathFix($sFilePath)
    $sFilePath = StringRegExp($sFilePath & " ", "^((?:\\\\\?\\)*(\\\\[^\?\/\\]+|[A-Za-z]:)?(.*?[\/\\]+)?([^\/\\]*[\/\\])?[\/\\]*((?:[^\.\/\\]|(?(?=\.[^\/\\]*\.)\.))*)?([^\/\\]*))$", 1)
    $sDrive = (StringStripWS($sFilePath[1], 8) == "") ? "" : $sFilePath[1]
    $sFilePath[2] = StringRegExpReplace($sFilePath[2], "[\/\\]+\h*", "\" & StringLeft($sFilePath[2], 1))
    $sParentDir = (StringStripWS($sFilePath[2], 8) == "") ? "" : $sFilePath[2]
    $sCurrentDir = (StringStripWS($sFilePath[3], 8) == "") ? "" : $sFilePath[3]
    $sFileNameNoExt = (StringStripWS($sFilePath[4], 8) == "") ? "" : $sFilePath[4]
    $sExtension = (StringStripWS($sFilePath[5], 8) == "") ? "" : StringStripWS($sFilePath[5], 3)
    Return $sFilePath
EndFunc   ;==>_PathSplit_Ex
; * -----:|
Func _PathFix($sFilePath)
    $sFilePath = StringStripWS($sFilePath, 3)
    $sFilePath = StringReplace($sFilePath, "/", "\")
    While StringInStr($sFilePath, " \")
        $sFilePath = StringReplace($sFilePath, " /", "\")
    WEnd
    While StringInStr($sFilePath, "\ ")
        $sFilePath = StringReplace($sFilePath, "/ ", "\")
    WEnd
    If (FileExists($sFilePath) And StringInStr(FileGetAttrib($sFilePath), 'D')) Then
        $sFilePath = _PathRemove_Backslash($sFilePath) & "\"
    EndIf
    Return $sFilePath
EndFunc   ;==>_PathFix
; * -----:|
Func _PathRemove_Backslash($sPath)
    If StringRight($sPath, 1) == '\' Then
        ;$sPath = StringRegExpReplace($sPath, "\\$", "")
        ;$sPath = StringRegExpReplace($sPath, "(.*)(\\)\z", "$1")
        $sPath = StringTrimRight($sPath, 1)
    EndIf
    Return $sPath
EndFunc   ;==>_PathRemove_Backslash
; * -----:|
Func _IsFile($sPath)
    If (Not FileExists($sPath)) Then Return SetError(-1, 0, 0)
    If StringInStr(FileGetAttrib($sPath), 'D') <> 0 Then
        Return SetError(0, 0, 0)
    Else
        Return SetError(0, 0, 1)
    EndIf
EndFunc   ;==>_IsFile
; * -----:| Dao Van Trong - TRONG.LIVE

 

Edited by Trong

Regards,
 

Link to comment
Share on other sites

I do not speak Dutch, so I'm not sure, what exactly is your goal:

 

  • Copy over files from the source dir to the destination dir, overwrite existing files, ignore orphaned files in destination dir (files that once existed in the destination dir will remain there, even when they are removed from the source dir)
  • Keep the files in sync: Add new files to destination dir, update existing files in destination dir, delete files in destination dir, that do not exist any more in source dir

 

For both tasks I personally would use the build in windows tool robocopy.exe

  • for the first task use the switches " /s /e"
  • for the 2nd task use the switch " /mir"

Note that the exit code of robocopy is giving you information, what was going on for the last copy process.

https://learn.microsoft.com/en-us/troubleshoot/windows-server/backup-and-storage/return-codes-used-robocopy-utility
https://ss64.com/nt/robocopy-exit.html

Local $DirSrc="C:\temp\Test1"
Local $DirDst="C:\temp\Test2"
Local $Files="*.* "
$Logfile="C:\temp\Test1-to-Test2_robo_LOG.txt"
$Logging="/log:" & $Logfile & " /tee"
$Retry= " /r:0 /w:0"

$Params=$DirSrc & " " & $DirDst & " " & $Files & $Logging
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $Params = ' & $Params & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console

$result=ShellExecuteWait("robocopy.exe",$Params)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $result = ' & $result & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console


MsgBox(0,"done","Robocopy Exit Code: " & $result)

 

Let run robocopy twice, if the 2nd run is giving you the exitcode 0 then both folders are in sync.

 

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

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