Thursday, May 03, 2007

[ftp]利用windows系统ftp命令编写的BAT文件上传

在开发中往往需要将本地的程序上传到服务器,而且用惯了linux命令的人来说。在windows下就比较麻烦了。为了方便特编写一个BAT程序来帮助需要这样功能的人。如果用其他IDE工具自带的FTP功能也可以,^_^!
命令: todev.bat /index.asp 默认d:\cnweb为网站的根目录。
我的 www.lawcar.cn www.myitcar.com www.stockcar.cn www.lunwencar.com 都是靠这样的方法维护。
@IF "%1"=="" goto error_parm
@
IF "%2"=="" goto error_parm

@SETLOCAL
@
set server=192.168.10.199
@
set username=
@
set password=

@echo 是否上传 d:
\cnweb\%1\%2 到dev服务器!

@echo off
CHOICE
/C YN /M "确认请按 Y,否请按N退出"
if errorlevel 255 goto no
if errorlevel 2 goto no
if errorlevel 1 goto yes


:no
@echo
on
@echo 没有任何文件上传到DEV.
@
goto end

:yes

IF not EXIST d:\cnweb\%1\%2 goto error2

echo open %server%
> ftp.txt
echo user %username%
>> ftp.txt
echo %password%
>> ftp.txt
echo cd %
1 >> ftp.txt
echo bye
>> ftp.txt
ftp
-s:ftp.txt -n | find "550"
if not errorlevel 1 @goto error
rem del ftp.txt ftperror.txt

echo open %server%
> ftp.txt
echo user %username%
>> ftp.txt
echo %password%
>> ftp.txt
echo cd %
1 >> ftp.txt
echo put d:
\cnweb\%1\%2 >> ftp.txt
echo bye
>> ftp.txt
ftp
-s:ftp.txt -n | find "Invalid"
if not errorlevel 1 @goto error3
rem del ftp.txt ftperror.txt

@echo
on
@echo 上传DEV完成.
@
goto putok

:
error
@echo 服务器目录错误 %
1
rem del ftp.txt ftperror.txt
@goto end

:error2
@echo 本地目录错误 d:
\cnweb\%1
@del ftp.txt ftperror.txt
@
goto end

:error3
@echo 上传文件错误 d:
\cnweb\%1\%2
del ftp.txt ftperror.txt
@
goto end

:putok
@echo 上传成功 %
1 %2
@
rem del ftp.txt ftperror.txt
@goto end

:error_parm
@echo 参数错误,正确格式: todev 相对路径 文件名
@
goto end

:
end
@echo Bye.
@ENDLOCAL