中国DOS联盟论坛

中国DOS联盟

-- 联合DOS 推动DOS 发展DOS --

联盟域名:www.cn-dos.net  论坛域名:www.cn-dos.net/forum
DOS,代表着自由开放与发展,我们努力起来,学习FreeDOS和Linux的自由开放与GNU精神,共同创造和发展美好的自由与GNU GPL世界吧!

游客:  注册 | 登录 | 命令行 | 会员 | 搜索 | 上传 | 帮助 »
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [原创]一个定时执行ftp任务的脚本
作者:
标题: [原创]一个定时执行ftp任务的脚本 上一主题 | 下一主题
genteman
初级用户





积分 90
发帖 43
注册 2007-10-31
状态 离线
『楼 主』:  [原创]一个定时执行ftp任务的脚本


::main.bat - 每隔固定时间从ftp服务器上取文件保存在本地
::genteman - 2010-06-19 - CMD@WinXP Pro
::contact amdaround@163.com
rem @echo off & setlocal
title 每隔固定时间从ftp服务器上取文件保存在本地

if [%1]==[/start] echo 正在创建任务 & call "%~dp0init.bat%" & exit /b
if [%1]==[/stop] echo 正在删除任务 & call "%~dp0task.bat" & exit /b

echo 用法:"%~n0 {/start|/stop}"
echo.
echo /start     创建任务
echo /stop      删除任务

pause & exit /b

rem @echo off
setlocal enabledelayedexpansion

:: 捕捉抛出错误并处理
if [%1]==[:error] goto :error
if not exist "%~dp0ftp.ini" %0 :error 1 "%~dp0ftp.ini 不存在"

:: 读取配置文件

for /f "tokens=1,* delims==" %%i in ('findstr "[^=]$" "%~dp0ftp.ini"') do (
set "str=%%j"
echo %%i | findstr /c:"ftp server ip" >nul && set "ftp_server_ip=!str: =!"
echo %%i | findstr /c:"port" >nul && set "port=!str!"
echo %%i | findstr /c:"username" >nul && set "user=!str!"
echo %%i | findstr /c:"password" >nul && set "password=!str!"
echo %%i | findstr /c:"remote directory path" >nul && set "rdp=!str!"
echo %%i | findstr /c:"local directory path" >nul && set "ldp=!str!"
echo %%i | findstr /c:"interval time" >nul && set "interval_time=!str: =!"
)

:: 检查配置是否已设置
if not defined ftp_server_ip %0 :error 2 "IP地址未设置"
if not defined interval_time set interval_time=1
if not defined user set "user=anonymous" & set "password=amdaround@163.com"
if not defined port set port=21
if not defined rdp %0 :error 2 "远程目录未设置"
if not defined ldp %0 :error 2 "本地目录未设置"

:: 检查配置合法性
echo %ftp_server_ip% | findstr "^[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*.$" >nul
if not errorlevel 1 (
for /f "delims=. tokens=1-4" %%i in ("%ftp_server_ip%") do (
set i_1=%%i
set i_2=%%j
set i_3=%%k
set i_4=%%l
)
) else (
%0 :error 3 "IP: %ftp_server_ip% 无效,请重新设置"
)

for /f "delims== tokens=1,*" %%a in ('set i_') do (
set num=%%b
if not "!num:~3!"=="" %0 :error 3 "IP: %ftp_server_ip% 无效,请重新设置"
if !num! GTR 255 %0 :error 3 "IP: %ftp_server_ip% 无效,请重新设置"
)

rem if not exist "%ldp%\nul" %0 :error 4 "目录 %ldp% 不存在"

:: 生成ftp脚本文件
type nul >"%~dp0list.txt"
echo user %user%>>"%~dp0list.txt"
echo %password%>>"%~dp0list.txt"
echo status>>"%~dp0list.txt"
echo lcd "%~dp0">>"%~dp0list.txt"
echo dir "%rdp%" filelist.txt>>"%~dp0list.txt"
echo quit>>"%~dp0list.txt"


type nul >"%~dp0run.bat"
echo ftp -s:"%~dp0list.txt" %ftp_server_ip% %port% ^| findstr /i /c:"not connect" ^>nul ^&^& echo ^%%date:~0,10^%% ^%%time:~0,8^%% server %ftp_server_ip% not connected, please check ip, port, username, password ^>^>"%~dp0ftp.log">>"%~dp0run.bat"

echo.>>"%~dp0run.bat"
echo if exist "%~dp0filelist.txt" (>>"%~dp0run.bat"
echo for /f "tokens=6" %%%%i in ("%~dp0filelist.txt") do (>>"%~dp0run.bat"
echo for /f "tokens=3" %%%%m in ('dir %%%%j ^^^| findstr "%%%%j>nul"') do (>>"%~dp0run.bat"
echo.>>"%~dp0run.bat"
echo if "%%%%m" LSS "%%%%i" echo %%%%j^>^>"%~dp0getfilelist.txt">>"%~dp0run.bat"
echo.>>"%~dp0run.bat"
echo )>>"%~dp0run.bat"
echo )>>"%~dp0run.bat"
echo del /f "%~dp0filelist.txt">>"%~dp0run.bat"
echo )>>"%~dp0run.bat"

echo.>>"%~dp0run.bat"

echo if exist "%~dp0getfilelist.txt" (>>"%~dp0run.bat"
echo for /f %%%%i in ("%~dp0getfilelist.txt") do (>>"%~dp0run.bat"
echo set filestr=!filestr! %%%%i>>"%~dp0run.bat"
echo )>>"%~dp0run.bat"

echo del /f "%~dp0getfilelist.txt">>"%~dp0run.bat"
echo type nul^>"%~dp0mget.txt">>"%~dp0run.bat"
echo echo user %user%^>^>"%~dp0mget.txt">>"%~dp0run.bat"
echo echo %password%^>^>"%~dp0mget.txt">>"%~dp0run.bat"
echo echo status^>^>"%~dp0mget.txt">>"%~dp0run.bat"
echo echo lcd "%ldp%"^>^>"%~dp0mget.txt">>"%~dp0run.bat"
echo echo cd "%rdp%"^>^>"%~dp0mget.txt">>"%~dp0run.bat"
echo if not "%%filestr%%"=="" mget %%filestr%%^>^>"%~dp0mget.txt">>"%~dp0run.bat"
echo echo quit^>^>"%~dp0mget.txt">>"%~dp0run.bat"

echo.>>"%~dp0run.bat"

echo ftp -i -s:"%~dp0mget.txt" %ftp_server_ip% %port% ^| findstr /i /c:"not connect" ^>nul ^&^& echo ^%%date:~0,10^%% ^%%time:~0,8^%% server %ftp_server_ip% not connected, please check ip, port, username, password^>^>"%~dp0ftp.log">>"%~dp0run.bat"
echo )>>"%~dp0run.bat"


:: 创建任务
schtasks /query | findstr "autoftp" >nul
if errorlevel 1 schtasks /create /sc hourly /mo %interval_time% /tn "autoftp" /tr "%~dp0run.bat" /ru "system"
goto :EOF

:error - 错误处理
echo.
echo error %2: %3 >>"%~dp0ftp.log"
exit /b %2

@echo off
schtasks /query | findstr "autoftp" >nul && schtasks /delete /tn "autoftp" /f
[ Last edited by genteman on 2010-6-21 at 15:36 ]

附件 1: mget.zip (2010-6-21 15:37, 3.16 K,下载次数: 19)
2010-6-20 23:35
查看资料  发送邮件  发短消息 网志   编辑帖子  回复  引用回复

请注意:您目前尚未注册或登录,请您注册登录以使用论坛的各项功能,例如发表和回复帖子等。


可打印版本 | 推荐给朋友 | 订阅主题 | 收藏主题



论坛跳转: