Handle new files after sync

Advertisement

halycon
Guest

Handle new files after sync

Hey guys,

i'm syncing a remote ftp dir to my local drive. My target is to handle all NEW synced files.

At the moment i'm using a batch file, which takes the "newest file" and does something to it:
for /f "delims=" %%a in ('dir /a-d /b /od *.zip') do set "newest=%%a"

The problem is: if there are e.g. two or three new files since the last sync, the batch only takes the newest file.

I thought about do xml logging and parse it to some python code or something to compare "before-sync <-> after-sync" and find out the filenames of the newest files. But i don't get it.

Is there any more easy way? Do you have any hint?

Thanks in advance...

Reply with quote

Advertisement

halycon
Guest

Thanks for your reply!
I just finished my python script for this problem and tested it succesfully.
Maybe somebody will be interested in such a thing in the future so i post it here: (it was my very first attempt to write python code! i hope it's not a shame :mrgreen:)

import shutil
import xml.etree.ElementTree as ET
import os

syncdir = "k:\\sync"                                                                          #these are some
updatedir = "k:\\"                                                                            #directories
workingdir= "d:\\bue_update\\"                                                                #i'm working with
winscp_lsxml = "d:\\bue_update\\winscp\\winscp.com /script=ls_xml.winscp /xmllog=xmllog.xml"  #this puts me a list of remote filenames into a xml
winscp_sync = "d:\\bue_update\\winscp\\winscp.com /script=sync_local.winscp"                  #this syncs so i can take the files
update_batch = updatedir+"\\ohnepause.bat"

os.system("connect_shares.bat")
os.system(winscp_lsxml)
os.system(winscp_sync)



newlog = ET.parse('xmllog.xml') #parses the fresh log from the os.system(winscp_lsxml) command
lastlog = ET.parse('last_xmllog.xml')   #parses the log from the lasttime os.system(winscp_lsxml) command

root = newlog.getroot()
namespace = "{http://winscp.net/schema/session/1.0}"
filenames = root.findall('{0}ls/{0}files/{0}file/{0}filename'.format(namespace))
liste = []


for namen in filenames:
    if "universal-lsb-rest-client" in namen.get('value'):     #i dont't want this file
        continue
    elif ".zip" in namen.get('value'):                        #put all *.zip filenames in my list
        liste.append(namen.get('value'))

for i in liste:
    print(i)

print("Anzahl Elemente xmllog",liste.__len__()) 

last_root = lastlog.getroot()
last_filenames = last_root.findall('{0}ls/{0}files/{0}file/{0}filename'.format(namespace))
last_liste = []


for namen in last_filenames:
    if ".zip" in namen.get('value'):
        last_liste.append(namen.get('value'))            #and make me a list of the lasttime log so i can compare

for i in last_liste:
    print(i)

print("Anzahl Elemente last_xmllog",last_liste.__len__())

print("Vergleiche Listen...")


for namen in liste:

    if namen not in last_liste:                                                             #here i compare the lists
        print(namen)
        cmd = workingdir + "7za.exe e " + syncdir + "\\" + namen+" -o"+updatedir+" -y"      #and extract all files
        print(cmd)
        os.system(cmd)

shutil.copy("xmllog.xml","last_xmllog.xml")                                        #save the fresh log for the next run

os.system(update_batch)
#os.system("disconnect_shares.bat")

Reply with quote

Advertisement

You can post new topics in this forum