Computing Staff
  • 1

FTP Script To Delete Files Older Than 7 Days

  • 1

Hello everyone, I try to wirte an batch script which shall delete files older than 7 days from an FTP Server.

I have started with some example script. Which I was able to find here:

REM to get the current date:
for /f “tokens=1,2,3 delims=/ ” %%a in (‘DATE /T’) do set current date=%%c%%b%%a

REM Login to FTP server and delete all files which are mentioned in the .txt file, should be replaced to with the “older than X days function” Can someone help?

@(ECHO o 127.0.0.1
ECHO user admin
ECHO admin
ECHO cd /test123
FOR /F %%a IN (delete-files.txt) DO ECHO delete %%a
ECHO quit) > #
ftp -n -d -s:#
del #

Share

2 Answers

  1. I would approach it from a three-tiered “trident”:
    1) collect filenames (incl. filedates) form nix server, via ftp
    2) date-filter using vbs to create list of qualifying files
    a) batchfile to deliver filenames & dates to vbs
    b) vbs writes out qualifying files to # (ftp script)
    3) ftp (2nd session) using the batch-generated script to delete the files
    @echo off & setlocal enabledelayedexpansion
    #1: run ftp using script A2 to logon, change dir as needed, “dir”, quit. > logfile
    ( something like: ftp /s A2 > logfile)
    #2:
    set dd=%date:~4%
    call :xx > A1
    (echo user
    echo passw
    echo cd /public
    :: step #2b
    for /f “tokens=*” %%a in (‘cscript daz.vbs ^<A1’) do echo del %%a
    echo quit)>#
    :: #3: re-run ftp using the above generated script
    ftp /s #
    goto :eof
    :xx
    :: step #2a
    :: set skips as needed, depending on a bunch a stuff
    for /f “skip=15 tokens=6-9” %%a in (logfile) do (
    if “%%c” equ “” goto :eof
    set yr=%%c
    if “!yr:~2,1!” equ “:” set yr=
    echo %%a %%b !yr!,%dd%,%%d
    )
    ::—— end batch

    ‘—– begin vbs, step #2b subsection F article 4z
    ‘daz.vbs
    set cin=wscript.stdin
    set cout=wscript.stdout
    set xout=wscript.stderr
    dim y,x,z
    x=”a”
    do while x<>””
    x=cin.readline
    y=split(x,”,”)
    fil=y(2)
    test=datediff(“d”,y(0),y(1))
    if test> 7 then cout.writeline(fil)
    loop

    • 0
  2. Hi there,
    I have adjusted the script a little bit, but now the console
    window remains black and nothing happend. The “daz.vbs” is still untouched. What could be wrong? Thank you.

    @echo off & setlocal enabledelayedexpansion
    @(ECHO o 127.0.0.1
    ECHO user admin
    ECHO admin
    ECHO cd /test123
    set dd=%date:~4%
    :: step #2b
    for /f “tokens=*” %%a in (‘cscript daz.vbs’) do echo del %%a
    echo quit)>#
    :: #3: re-run ftp using the above generated script
    ftp /s #
    goto :eof
    :xx
    :: step #2a
    :: set skips as needed, depending on a bunch a stuff
    for /f “skip=15 tokens=6-9” %%a in (logfile.txt) do (
    if “%%c” equ “” goto :eof
    set yr=%%c
    if “!yr:~2,1!” equ “:” set yr=
    echo %%a %%b !yr!,%dd%,%%d
    )
    ::—— end batch

    • 0