1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Automatically downloading updates

Discussion in 'T&T - Scripting' started by nchernoff, Jun 4, 2008.

  1. nchernoff

    nchernoff Administrator
    Staff Member

    Joined:
    May 16, 2008
    Messages:
    42
    Likes Received:
    2
    Migrated From JP Software Wiki

    Please note that current versions of JP Software products all have update built into the programs so this is not needed, but it does provide a useful technique.

    4update.btm

    This batch synchronizes a download directory with the jpsoft ftp site.

    It is equivalent to

    1. rmdir /s mymirror
    2. copy /su ftp mymirror

    Only doing a copy will not remove the files or directories deleted on the ftp.

    • Copy the batch the name is not significant.
    • Change the location of the local directory in the batch file: variable localMirror.
    • Run the batch no parameters needed.

    Overview

    Copy all the updated files from the ftp site.

    Write a list of the files in the local directory into a file removing the directory information

    Append the name of all the files at the ftp site to this list removing the ftp directory information and changing the / into \.

    This should make the local names and the ftp names identical.

    Sort the list.

    Read the list line by line:
    • Duplicate names implies that the file in the local directory is up to date.
    • If a name occurs only once then either the file or directory was deleted from the ftp or the transfer has failed.
    • If a file or a directory exists locally, delete it, otherwise the update must have failed, post a message and continue.
    Code:
    :: 4update.btm
    :: mirror jpsoft ftp download directory
    :: ---------------------------------------------------------------- ::
    :: set the variable localMirror to the name of your local directory ::
    :: ---------------------------------------------------------------- ::
    @echo off
    setlocal
    
    :: name of your mirror directory
    set localMirror=C:\Download\jpsoft\ftp\
    
    :: -- this line allows editor colorizer to work for a global
    :! tmp temp
    
    :::  set variables
    ::!  curr:			filename in mirror
    ::!  f1:			unique temp file
    ::!  f2:			unique temp file
    ::!  fn:			unique temp file used to normalize mirror path name
    ::!  ftp:			file name on the ftp
    ::!  line:			filename in the list
    ::!  localmirror:	mirror directory name
    ::!  prev:			file name in the list
    ::!  t:				path for temp or tmp directory
    ::!  temp: 			global
    ::!  tmp:  			global
    
    
    set ftp=jpsoft.com
    
    :: Normalize directory
    if not direxist %localMirror mkdir %localMirror
    set fn=%@unique[%localMirror]
    set localMirror=%@path[%fn]
    del /q %fn
    
    :: Create a permanent connection
    iftp /N ftp://anonymous:pwd@%ftp
    
    :: copy all the files newer then the current files (including empty directories)
    copy /fzsgu "ftp:*.*" %localMirror
    set t=%tmp
    if (%t) == () set t=%temp
    set f1=%@unique[%t]
    :: list all the files in the backup without the directory information
    pdir /s /(@right[-%@len[%localMirror],*]) %localMirror > %f1
    :: append all the files in the ftp without the directory information
    :: replace / with \
    :: all the file names in both set should be identical
    pdir /s /(@replace[/,\,%%@right[-4,*]]) "ftp:*.*" >> %f1
    
    set f2=%@unique[%t]
    :: order so that duplicate names follow each other
    sort %f1 > %f2
    
    :: append a dummy end of file simplify testing the last file
    echo + >> %f2
    
    set prev=
    
    do line in @%f2
    	iff "%prev" == ""  then
    		:: eof when expecting a new name we are done
    		if "%line" == "+" leave
    	else
    		iff "%line" == "%prev" then
    			set line=
    		else
    			set curr=%@quote[%localMirror%%prev]
    			set ftp=ftp:/%@replace[\,/,%prev]
    			iff exist %curr .and. exist %ftp then
    				:: Once in a while pdir seems to miss files
    				:: maybe others are accessing them
    				echo %prev not properly reported by pdir
    			elseiff direxist %curr then 
    				:: directory removed from the ftp site
    				rmdir /sq %curr
    				echo remove dir %curr
    			elseiff exist %curr then
    				:: file removed from the ftp site
    				del /q %curr
    				echo delete file %curr
    			else
    				:: should have transferred this file 
    				iff isdir %ftp .and. %@files[%ftp,-d] == 0 then
    					:: empty directories are not transferred
    					mkdir %curr
    				else
    					echo %curr not received
    				endiff
    			endiff
    		endiff
    	endiff
    	set prev=%line
    enddo
    
    iftp /c
    
    :: clean up
    del /q %f1 %f2
     

Share This Page