Solved

How would i do a complete folder compare and copy difference in autoit

Posted on 2014-01-03
7
1,028 Views
Last Modified: 2014-01-06
I have a folder out on a networkshare lets call it c
i want to copy all of its contents over to a computer i'm settings up and ONLY copy files to the computer that are not there or different on the networkshare.

so lets say i have c:\backup.bat  on the networkshare
i want it to be the exact same on the new computer.

the exact same structure and and subdirectory structure should apply

i have something like this already in vb but it doesn't check for existance or differences.

'Copy Folder Structure
        bkwrk.ReportProgress(0, "0|0|Getting Folder Structure (mis/" & sMachine & ")...")
        iMaxCnt = My.Computer.FileSystem.GetDirectories("\\mis\mis3\machines\" & sMachine & "\c\", FileIO.SearchOption.SearchAllSubDirectories, "*.*").Count
        For Each strFolders In My.Computer.FileSystem.GetDirectories("\\mis\mis3\machines\" & sMachine & "\c\", FileIO.SearchOption.SearchAllSubDirectories, "*.*")
            iCurCnt = iCurCnt + 1
            sDestination = "C:\" & Mid(strFolders, InStr(strFolders, "\C\") + 3, strFolders.Length - InStr(strFolders, "\C\"))
            bkwrk.ReportProgress(iCurCnt / iMaxCnt * 100, iCurCnt & "|" & iMaxCnt & "|Creating Folder: " & sDestination) 'Strings.Left(strFolders, 10) & "..." & Strings.Right(strFolders, 15))
            My.Computer.FileSystem.CopyDirectory(strFolders, sDestination, True)
        Next
        'Copy Program Files
        bkwrk.ReportProgress(0, "0|0|Getting Files (mis/" & sMachine & ")...")
        iCurCnt = 0
        iMaxCnt = My.Computer.FileSystem.GetFiles("\\mis\mis3\machines\" & sMachine & "\c\", FileIO.SearchOption.SearchAllSubDirectories, "*.*").Count
        For Each StrFiles In My.Computer.FileSystem.GetFiles("\\mis\mis3\twmachines\" & sMachine & "\c\", FileIO.SearchOption.SearchAllSubDirectories, "*.*")
            iCurCnt = iCurCnt + 1
            sDestination = "C:\" & Mid(StrFiles, InStr(StrFiles, "\C\") + 3, StrFiles.Length - InStr(StrFiles, "\C\"))
            bkwrk.ReportProgress(iCurCnt / iMaxCnt * 100, iCurCnt & "|" & iMaxCnt & "|Copying File: " & sDestination) 'Strings.Left(StrFiles, 10) & "..." & Strings.Right(StrFiles, 15))
            My.Computer.FileSystem.CopyFile(StrFiles, sDestination, True)
        Next

Open in new window

0
Comment
Question by:bbimis
  • 3
  • 2
  • 2
7 Comments
 
LVL 19

Expert Comment

by:strivoli
ID: 39755757
Why not using RoboCopy instead?
0
 
LVL 29

Expert Comment

by:matrixnz
ID: 39755883
If you have the latest version of AutoIT use the _FileListToArrayRec function (see AutoIT Help File for more info), I've used this quite extensively in the past.  Here is an example of the code (note not tested).

#include <file.au3>
;~ Get System Drive Letter for %SystemDrive% Enviroment Variable of local machine
$_DIR_SYSDRIVE = EnvGet('SystemDrive')
;~ Get Server Share Name to copy contents from
$_DIR_NETDRIVE = '\\mis\mis3\machines\' & @ComputerName & '\c'

;~ Create an Array (files/folders), including subdirectories, use relative path name i.e. Folder\Filename.exe
$_ARR_SYSDRIVE = _FileListToArrayRec($_DIR_NETDRIVE, '*', 0, 1, 0, 1)
;~ Create an Array (files/folders), including subdirectories, use full path name i.e. \\mis\mis3\machines\ComputerName\c\Folder\Filename.exe
	;~ Exit if no array is created from the shared network
	If @error = 1 Then Exit
$_ARR_NETDRIVE = _FileListToArrayRec($_DIR_NETDRIVE, '*', 0, 1, 0, 2)
	;~ Exit if no array is created from the shared network
	If @error = 1 Then Exit

;~ Go through the full folder list and check that it doesn't exist on the local machine if not found copy the file and create the directory if necessary.
For $x = 1 To $_ARR_NETDRIVE[0]
	If StringInStr($_ARR_NETDRIVE[$x], $_ARR_SYSDRIVE[$x]) And FileExists($_DIR_SYSDRIVE & '\' & $_ARR_SYSDRIVE[$x]) = 0 Then
		FileCopy($_ARR_NETDRIVE[$x], $_DIR_SYSDRIVE & '\' & $_ARR_SYSDRIVE[$x], 8)
	EndIf
Next

Open in new window

0
 

Author Comment

by:bbimis
ID: 39759175
matrixnz,
that makes sense and all but how would i check to see if it is new. not so much of exist thats a given.
I was thinking something like this maybe ?
RunWait(@ComSpec & " /c " & 'xcopy "' & '\\fileserv\e\apps\c\' & '" "' & 'c:\' & '" /D /E /C /R

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 19

Assisted Solution

by:strivoli
strivoli earned 50 total points
ID: 39759294
Please consider RoboCopy.
0
 

Author Comment

by:bbimis
ID: 39759317
stivoli,
i was looking at robocopy but i don't really understand how to make it check path1 against path2 and copy accordingly.

What my goal is to take and copy all the files on the share \\share\c to the root of the local machine
and only copy files that do not exist or are different on the \\share\c

thanks!
0
 
LVL 29

Accepted Solution

by:
matrixnz earned 450 total points
ID: 39759664
Probably something like the following, please note I modified the original to only use one array, which makes it more robust.  Hope it makes sense.
#include <file.au3>
;~ Get System Drive Letter for %SystemDrive% Enviroment Variable of local machine
$_DIR_SYSDRIVE = EnvGet('SystemDrive')
;~ Get Server Share Name to copy contents from
$_DIR_NETDRIVE = '\\mis\mis3\machines\' & @ComputerName & '\c'

;~ Create an Array (files/folders), including subdirectories, use relative path name i.e. Folder\Filename.exe
$_ARR_NETDRIVE = _FileListToArrayRec($_DIR_NETDRIVE, '*', 0, 1, 0, 1)
	;~ Exit if no array is created from the shared network
	If @error = 1 Then Exit

;~ Go through the full folder list and check that it doesn't exist on the local machine if not found copy the file and create the directory if necessary.
For $x = 1 To $_ARR_NETDRIVE[0]
	;~ If File does not exist then copy the file and create directory if required
	If FileExists($_DIR_SYSDRIVE & '\' & $_ARR_NETDRIVE[$x]) = 0 Then
		FileCopy($_DIR_NETDRIVE & '\' & $_ARR_NETDRIVE[$x], $_DIR_SYSDRIVE & '\' & $_ARR_SYSDRIVE[$x], 8)
	Else
		;~ The File Exists so compare the time stamp of Network file against local file and if greater, copy the file overwriting the existing file.
		If FileGetTime($_DIR_NETDRIVE & '\' & $_ARR_NETDRIVE[$x], 0, 1) > FileGetTime($_DIR_SYSDRIVE & '\' & $_ARR_NETDRIVE[$x]) Then
			FileCopy($_DIR_NETDRIVE & '\' & $_ARR_NETDRIVE[$x], $_DIR_SYSDRIVE & '\' & $_ARR_SYSDRIVE[$x], 9)
		EndIf
	EndIf
Next

Open in new window

0
 

Author Closing Comment

by:bbimis
ID: 39759697
i gave points to matrixnz because his is what i was seeking. i gave 50 for the effort to the other member. Thanks!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Running Access application from Task Scheduler 6 46
Apps blocked by Java 9 79
Problem to open text file 11 105
Problem to cell option 1 28
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
A short article about a problem I had getting the GPS LocationListener working.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now