Sign in to follow this  
Followers 0
ClosetDweller

Way to compare 2 arrays and sync to single directory?

5 posts in this topic

Hey guys. Im struggling a little here and searching the forums hasnt yielded anything on point

What I am currently doing is:

via inetget connecting to an FTP server and based on a list of constant variables to filenames ($file1 = 'actual filename')

Based on an If Statement:

If FileExists($localpath & $cleanconfig) Then

FileWriteLine ($localpath & $storefile, $cleanconfig & " Already Existed")

Filewriteline ($localpath & $storefile, "********************")

Else

FileWriteLine ($localpath & $storefile, $cleanconfig & " DID NOT Exist and Download began@" & @HOUR & ":" & @MIN)

Local $hDownload = InetGet($path & $cleanconfig, $localpath & "\" & $cleanconfig, 1, 1)

Do

Until InetGetInfo($hDownload, 2) ; Check if the download is complete.

Local $nBytes = InetGetInfo($hDownload, 0)

FileWriteLine ($localpath & $storefile, $cleanconfig & "Completed Downloading **" & $nbytes & " KB** @" & @HOUR & ":" & @MIN)

Filewriteline ($localpath & $storefile, "********************")

InetClose($hDownload) ; Close the handle to release resources.InetGet ($path, $cleanconfig, @ScriptDir $localpath & $cleanconfig)

EndIf

After writing that result to a log file, it will either go get it (if not already there) or simply note "already existed"

At the end of the script, it then reconnects and drops the log file on the ftp server with the results:

$upload = _Ftp_FilePut($Conn, $localpath & $storefile, $remotedir & $storefile)

$Ftpc = _FTP_Close($Open)

This works like a champ based on constants. Problem is, I need to it compare local files in an array to the remote directory files in an array and essentially grab anything in the remote directory that doesnt already exist on the local directory and POSSIBLY delete anything in the local

What I have so far is:

#include <FTPEx.au3>

#include <Array.au3>

#include <File.au3>

Local $server = 'myserver'

Local $username = 'myuser'

Local $pass = 'mypass'

Local $lfile = 'my\local\dir'

Local $rfile = '/my/remote/dir/'

Local $Open = _FTP_Open('MyFTP Control')

Local $Conn = _FTP_Connect($Open, $server, $username, $pass)

Local $h_Handle

Local $dirset = _FTP_DirSetCurrent($Conn, $rfile)

Local $aFile = _FTP_ListToArrayEx($Conn, 0)

Local $Ftpc = _FTP_Close($Open)

Local $FileList = _FileListToArray($lfile)

If @error = 1 Then

MsgBox(0, "", "Some or all files are missing")

Exit

EndIf

If @error = 4 Then

MsgBox(0, "", "Some or all files are missing")

Exit

EndIf

_ArrayDisplay($FileList, "LocalFiles")

_ArrayDisplay($aFile, "Remote Files")

Which nicely displays what I have in the array. Now to find a way to compare the 2 and make the $afile authoritative and download missing files to the local directory. Pointers?

Share this post


Link to post
Share on other sites



Sorry about the second post but I dont appear to be able to edit the previous one.

:Update

I have managed to make the file compare to the existing directory.

Im still not really "comparing" as much as I am searching the local directory for something matching an array element.

New problem now tho.

The way I have it written, its still a finite number of elements (13), how can I change this so that it loops and continues to download regardless of the number of elements existing in the array?

Local $Open = _FTP_Open('MyFTP Control')
Local $Conn = _FTP_Connect($Open, $fserver, $fusername, $fpass)

Local $h_Handle
Local $dirset = _FTP_DirSetCurrent($Conn, $rfile)
Local $aFile = _FTP_ListToArray($Conn, 0)

Local $Ftpc = _FTP_Close($Open)

;filecheck if $file1 exists do nothing
;filecheck if $file1 not exist (get)
DirCreate ($localpath)
FileOpen ($localpath & $storefile)
Filewriteline ($localpath & $storefile, @Mon & "-" & @MDAY & "-" & @YEAR & " at " & @HOUR & ":" & @MIN)
Filewriteline ($localpath & $storefile, "********************")
If FileExists($localpath & $afile[4]) Then
FileWriteLine ($localpath & $storefile, $afile[4] & " Already Existed")
Filewriteline ($localpath & $storefile, "********************")
Else
FileWriteLine ($localpath & $storefile, $afile[4] & " DID NOT Exist and Download began@" & @HOUR & ":" & @MIN)
Local $hDownload = InetGet($path & $afile[4], $localpath & "" & $afile[4], 1, 1)
Do
Until InetGetInfo($hDownload, 2) ; Check if the download is complete.
Local $nBytes = InetGetInfo($hDownload, 0)
FileWriteLine ($localpath & $storefile, $afile[4] & "Completed Downloading **" & $nbytes & " KB** @" & @HOUR & ":" & @MIN)
Filewriteline ($localpath & $storefile, "********************")
InetClose($hDownload) ; Close the handle to release resources.InetGet ($path, $afile[4], @ScriptDir $localpath & $afile[4])
EndIf

If FileExists($localpath & $afile[5]) Then
FileWriteLine ($localpath & $storefile, $afile[5] & " Already Existed")
Filewriteline ($localpath & $storefile, "********************")
Else
FileWriteLine ($localpath & $storefile, $afile[5] & " Did NOT Exist and Download began@" & @HOUR & ":" & @MIN)
Local $hDownload = InetGet($path & $afile[5], $localpath & "" & $afile[5], 1, 1)
Do
Until InetGetInfo($hDownload, 2) ; Check if the download is complete.
Local $nBytes = InetGetInfo($hDownload, 0)
FileWriteLine ($localpath & $storefile, $afile[5] & " Completed Downloading **" & $nbytes & " KB** @" & @HOUR & ":" & @MIN)
Filewriteline ($localpath & $storefile, "********************")
InetClose($hDownload) ; Close the handle to release resources.InetGet ($path, $afile[4], @ScriptDir $localpath & $afile[4])
EndIf

If FileExists($localpath & $afile[6]) Then
FileWriteLine ($localpath & $storefile, $afile[6] & " Already Existed")
Filewriteline ($localpath & $storefile, "********************")
Else
FileWriteLine ($localpath & $storefile, $afile[6] & " Did NOT Exist and Download began@" & @HOUR & ":" & @MIN)
Local $hDownload = InetGet($path & $afile[6], $localpath & "" & $afile[6], 1, 1)
Do
Until InetGetInfo($hDownload, 2) ; Check if the download is complete.
Local $nBytes = InetGetInfo($hDownload, 0)
FileWriteLine ($localpath & $storefile, $afile[6] & " Completed Downloading **" & $nbytes & " KB** @" & @HOUR & ":" & @MIN)
Filewriteline ($localpath & $storefile, "********************")
InetClose($hDownload) ; Close the handle to release resources.InetGet ($path, $afile[4], @ScriptDir $localpath & $afile[4])
EndIf

If FileExists($localpath & $afile[3]) Then
FileWriteLine ($localpath & $storefile, $afile[3] & " Already Existed")
Filewriteline ($localpath & $storefile, "********************")
Else
FileWriteLine ($localpath & $storefile, $afile[3] & " Did NOT Exist and Download began@" & @HOUR & ":" & @MIN)
Local $hDownload = InetGet($path & $afile[3], $localpath & "" & $afile[3], 1, 1)
Do
Until InetGetInfo($hDownload, 2) ; Check if the download is complete.
Local $nBytes = InetGetInfo($hDownload, 0)
FileWriteLine ($localpath & $storefile, $afile[3] & " Completed Downloading **" & $nbytes & " KB** @" & @HOUR & ":" & @MIN)
Filewriteline ($localpath & $storefile, "********************")
InetClose($hDownload) ; Close the handle to release resources.InetGet ($path, $afile[4], @ScriptDir $localpath & $afile[4])
EndIf

If FileExists($localpath & $afile[8]) Then
FileWriteLine ($localpath & $storefile, $afile[8] & " Already Existed")
Filewriteline ($localpath & $storefile, "********************")
Else
FileWriteLine ($localpath & $storefile, $afile[8] & " Did NOT Exist and Download began@" & @HOUR & ":" & @MIN)
Local $hDownload = InetGet($path & $afile[8], $localpath & "" & $afile[8], 1, 1)
Do
Until InetGetInfo($hDownload, 2) ; Check if the download is complete.
Local $nBytes = InetGetInfo($hDownload, 0)
FileWriteLine ($localpath & $storefile, $afile[8] & " Completed Downloading **" & $nbytes & " KB** @" & @HOUR & ":" & @MIN)
Filewriteline ($localpath & $storefile, "********************")
InetClose($hDownload) ; Close the handle to release resources.InetGet ($path, $afile[4], @ScriptDir $localpath & $afile[4])
EndIf

If FileExists($localpath & $afile[9]) Then
FileWriteLine ($localpath & $storefile, $afile[9] & " Already Existed")
Filewriteline ($localpath & $storefile, "********************")
Else
FileWriteLine ($localpath & $storefile, $afile[9] & " Did NOT Exist and Download began@" & @HOUR & ":" & @MIN)
Local $hDownload = InetGet($path & $afile[9], $localpath & "" & $afile[9], 1, 1)
Do
Until InetGetInfo($hDownload, 2) ; Check if the download is complete.
Local $nBytes = InetGetInfo($hDownload, 0)
FileWriteLine ($localpath & $storefile, $afile[9] & " Completed Downloading **" & $nbytes & " KB** @" & @HOUR & ":" & @MIN)
Filewriteline ($localpath & $storefile, "********************")
InetClose($hDownload) ; Close the handle to release resources.InetGet ($path, $afile[4], @ScriptDir $localpath & $afile[4])
EndIf

If FileExists($localpath & $afile[11]) Then
FileWriteLine ($localpath & $storefile, $afile[11] & " Already Existed")
Filewriteline ($localpath & $storefile, "********************")
Else
FileWriteLine ($localpath & $storefile, $afile[11] & " Did NOT Exist and Download began@" & @HOUR & ":" & @MIN)
Local $hDownload = InetGet($path & $afile[11], $localpath & "" & $afile[11], 1, 1)
Do
Until InetGetInfo($hDownload, 2) ; Check if the download is complete.
Local $nBytes = InetGetInfo($hDownload, 0)
FileWriteLine ($localpath & $storefile, $afile[11] & " Completed Downloading **" & $nbytes & " KB** @" & @HOUR & ":" & @MIN)
Filewriteline ($localpath & $storefile, "********************")
InetClose($hDownload) ; Close the handle to release resources.InetGet ($path, $afile[4], @ScriptDir $localpath & $afile[4])
EndIf
FileClose($localpath & $storefile)

If FileExists($localpath & $afile[10]) Then
FileWriteLine ($localpath & $storefile, $afile[10] & " Already Existed")
Filewriteline ($localpath & $storefile, "********************")
Else
FileWriteLine ($localpath & $storefile, $afile[10] & " Did NOT Exist and Download began@" & @HOUR & ":" & @MIN)
Local $hDownload = InetGet($path & $afile[10], $localpath & "" & $afile[10], 1, 1)
Do
Until InetGetInfo($hDownload, 2) ; Check if the download is complete.
Local $nBytes = InetGetInfo($hDownload, 0)
FileWriteLine ($localpath & $storefile, $afile[10] & " Completed Downloading **" & $nbytes & " KB** @" & @HOUR & ":" & @MIN)
Filewriteline ($localpath & $storefile, "********************")
InetClose($hDownload) ; Close the handle to release resources.InetGet ($path, $afile[4], @ScriptDir $localpath & $afile[4])
EndIf

If FileExists($localpath & $afile[13]) Then
FileWriteLine ($localpath & $storefile, $afile[13] & " Already Existed")
Filewriteline ($localpath & $storefile, "********************")
Else
FileWriteLine ($localpath & $storefile, $afile[13] & " Did NOT Exist and Download began@" & @HOUR & ":" & @MIN)
Local $hDownload = InetGet($path & $afile[13], $localpath & "" & $afile[13], 1, 1)
Do
Until InetGetInfo($hDownload, 2) ; Check if the download is complete.
Local $nBytes = InetGetInfo($hDownload, 0)
FileWriteLine ($localpath & $storefile, $afile[13] & " Completed Downloading **" & $nbytes & " KB** @" & @HOUR & ":" & @MIN)
Filewriteline ($localpath & $storefile, "********************")
InetClose($hDownload) ; Close the handle to release resources.InetGet ($path, $afile[4], @ScriptDir $localpath & $afile[4])
EndIf


$Open = _FTP_Open('MyFTP Control')
$Conn = _FTP_Connect($Open, $fserver, $fusername, $fpass, 1)

$upload = _Ftp_FilePut($Conn, $localpath & $storefile, "/public/BigOBeta/9.1 Production Release Folder/stores/" & $storefile)
$Ftpc = _FTP_Close($Open)

Share this post


Link to post
Share on other sites

How about something along these lines? I am not able to test this code, sorry. I am just writing it from how I think it would work. Maybe it will give you some ideas.

#include <FTPEx.au3>
#include <array.au3>

$server = "yourwebsite.com"
$username = "user_name"
$pass = "password"

; Connect to your website and get a list of file to an array called $List1
$Open = _FTP_Open('MyFTP Control')
$Conn = _FTP_Connect($Open, $server, $username, $pass)
$List1 = _FTP_ListToArray($Conn, 2)

; Get a list of your local files to an array called $List2
$List2 = _FileListToArray("your_path")

; Take your $List2 array size and resize your $List1 to be able to add those files to it
$List1_size = UBound($List1)
$List2_size = UBound($List2)
ReDim($List1, $List1_size + $List2_size)

; Add $List2 array to $List1 array
For $i = 1 to $List2_size Step 1
$List1[$i + $List2_size - 1] = $List2[$i - 1]
Next

; Show the differences between the two arrays
$Differences = _ArrayUnique($List2)
_ArrayDisplay($Differences)

RAID Calculator | Software Installer

The truth has been suppressed since the dawn of time.

Share this post


Link to post
Share on other sites

I just realized you can use _ArrayConcatenate to combine the two arrays together. That would be easier than my resizing and copying $List2 to $List1.


RAID Calculator | Software Installer

The truth has been suppressed since the dawn of time.

Share this post


Link to post
Share on other sites

I just realized you can use _ArrayConcatenate to combine the two arrays together. That would be easier than my resizing and copying $List2 to $List1.

I actually can combine them with _ArrayConcatenate but what I need to do is a literal side by side so that if $file1 exists in local directory array but not in remote directory array, it would delete it from the local directory. As well as if a $file1 exists in the remote directory array but NOT in the local array, it would proceed to download it.

However....I have a use for that function in another project I am working on. Thank You!!!!

Share this post


Link to post
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
Sign in to follow this  
Followers 0