FTP Script to delete files older than 7 days

Microsoft Windows 2003 server - 5 users
May 27, 2010 at 00:16:57
Specs: Windows 2003
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 #


See More: FTP Script to delete files older than 7 days

Report •


#1
May 27, 2010 at 21:36:03
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


Report •

#2
May 28, 2010 at 06:06:03
Hi nbrane,

thank you very much for your answer!
That looks super complex too me. I will try out your script


Report •

#3
August 4, 2010 at 08:19:29
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


Report •

Related Solutions


Ask Question