Topic "Handle new files after sync"

Author Message
halycon

Guest


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:
Code:
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...
Advertisements
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25034
Location: Prague, Czechia
I do not think there's any easier solution.

But I believe that if you take this PowerShell example and just modify FileTransferred function to do what you need, it should be pretty easy:
https://winscp.net/eng/docs/library_session_synchronizedirectories#powershell
_________________
Martin Prikryl
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 Mr. Green)

Code:

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")


martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25034
Location: Prague, Czechia
Thanks for sharing your solution.
_________________
Martin Prikryl
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License