Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FTP get and delete multiple files

Tags:

ftp

I have to get and delete multiple files via FTP so I wrote this script:

open ftp.myftpserver.org user pass cd folder lcd E:\localdir mget * mdel * bye

This works but is not safe since the folder is being fed from other sources and the mdel * step may delete files uploaded in the meanwhile.

I guess a solution could be moving the files remotely to a different folder, building a filelist at the beginning of the process, but i have no idea how to make it.

Is it possibile?

like image 813
ciquta Avatar asked Mar 18 '14 14:03

ciquta


1 Answers

FTR I followed the nice hint and I managed to succesfully made something working, maybe not elegant but works:

First step to get the file list:

getfilelist.bat


    open ftp.myserver.it
    myuser
    pass1234
    cd ftpfolder
    prompt n
    lcd E:\localdir
    ls *.??? filelist.txt
    bye

Second step to download and delete the above files

movefiles.bat


    @echo off

    setlocal enableextensions
    setlocal enabledelayedexpansion

    echo open ftp.myserver.it>>myscript
    echo user myuser pass1234>>myscript
    echo cd ftpfolder>>myscript
    echo prompt n>>myscript
    echo ascii>>myscript
    echo lcd E:\downloaddir>>myscript
    for /F "usebackq tokens=1,2* delims=," %%G IN ("E:\localdir\filelist.txt") DO ECHO rename %%G %%G_TMP>>myscript
    echo mget *_TMP>>myscript
    echo mdelete *_TMP>>myscript
    echo bye>>myscript
    ftp -n -s:myscript
    del filelist.txt
    del myscript
    e:
    cd E:\downloaddir
    ren *.???_TMP *.???

A bat file to recall the above steps:

    E:
    cd E:\localdir
    ftp -i -s:E:\localdir\getfilelist.bat
    E:\localdir\movefiles.bat

hope it helps

like image 84
ciquta Avatar answered Oct 01 '22 07:10

ciquta