BAT脚本:将本地昨天生成的文件,上传到服务器
2011-10-18 11:15
393 查看
因为工作需要,平生第一次用Windows的Dos命令写脚本。
需求:本地有个软件,每天生成相应的Log文件,并保存在以日期时间为目录的文件夹下。然后需要在后一天,将前一天生成的Log文件及文件夹上传到一个日志服务器进行保存。
实现工具:bat脚本,Ncftp工具,计划任务
其中,因为系统默认的ftp命令行,不支持passive模式,而Port模式下,需要修改或开启Log生成端服务器的防火墙,需要最终改用网络上免费的小工具Ncftp,这个小工具比系统自带的强大了N倍,支持命令行,支持PASV模式也可以切换PORT模式,还支持断点续传,上传成功后删除本地文件等。
实现步骤:
1)转到脚本所在目录;
2)上传之前未能成功上传的文件;
3)取到今天需要上传的文件夹名称;
4)上传今天需要上传的文件夹及文件;
5)如果成功,删除本地文件,如果不成功,将文件夹移到备份目录;
6)删除空目录;
以下是具体的脚本内容(英文版系统):
-----------------------------------------------------------
@echo off
set dt=%date:~10,4%%date:~4,2%%date:~7,2%
set batpath=%~dp0
set dname=
rem Turn to batfile's path
%~d0
cd %batpath%
mkdir bak >nul 2>nul
rem Re-upload bak log directory and file to NMS Server
type %batpath%Evtlog.txt | find "Ftpfailed" >nul 2>nul
if %errorlevel% EQU 0 goto reupload
if %errorlevel% NEQ 0 goto reupend
:reupload
echo ----------%dt% Re-upload bak Log------------ 2>>%batpath%Evtlog.txt 1>&2
%BatPath%ncftpput -u administrator -p password -r 5 -z -R -DD 192.168.1.80 /192.168.1.53 %batpath%bak\* 2>>%batpath%Evtlog.txt 1>&2
if %errorlevel% EQU 0 goto cleanrelog
if %errorlevel% NEQ 0 goto reupfailed
:cleanrelog
cd.>%batpath%Evtlog.txt
for /f "tokens=*" %%a in ('dir /b /ad /s %batpath%bak\^|sort /r') do rd "%%a" /q 2>nul
goto reupend
:reupfailed
echo %dt%:Ftpfailed Reupload files the day before yesterday failed! 2>>%batpath%Evtlog.txt 1>&2
goto reupend
:reupend
rem Get the yesterday date
for /d %%i in (20*) do if %%i LSS %dt% set dname=%%i
if exist %dname% goto ncftp
if not exist %dname% goto :eof
:ncftp
rem Transfer yesterday log file and folder to NMS Server
echo ----------%dt% Ftp Log------------ 2>>%batpath%Evtlog.txt 1>&2
%BatPath%ncftpput -u administrator -p password -r 5 -z -R -DD 192.168.1.80 /192.168.1.53 %batpath%%dname% 2>>%batpath%Evtlog.txt 1>&2
if %errorlevel% EQU 11 goto session
if %errorlevel% EQU 10 goto library
if %errorlevel% EQU 9 goto error
if %errorlevel% EQU 8 goto usage
if %errorlevel% EQU 7 goto url
if %errorlevel% EQU 6 goto directorytimeout
if %errorlevel% EQU 5 goto directory
if %errorlevel% EQU 4 goto transfertimeout
if %errorlevel% EQU 3 goto transfer
if %errorlevel% EQU 2 goto connecttimeout
if %errorlevel% EQU 1 goto connect
if %errorlevel% EQU 0 goto success
:success
echo ncFtp:0Success. 2>>%batpath%Evtlog.txt 1>&2
goto exit
:connect
echo Ftpfailed:1Could not connect to remote host. 2>>%batpath%Evtlog.txt 1>&2
goto movefile
:connecttimeout
echo Ftpfailed:2Could not connect to remote host - timed out. 2>>%batpath%Evtlog.txt 1>&2
goto movefile
:transfer
echo Ftpfailed:3Transfer failed. 2>>%batpath%Evtlog.txt 1>&2
goto movefile
:transfertimeout
echo Ftpfailed:4Transfer failed - timed out. 2>>%batpath%Evtlog.txt 1>&2
goto movefile
:directory
echo Ftpfailed:5Directory change failed. 2>>%batpath%Evtlog.txt 1>&2
goto movefile
:directorytimeout
echo Ftpfailed:6Directory change failed - timed out. 2>>%batpath%Evtlog.txt 1>&2
goto movefile
:url
echo Ftpfailed:7Malformed URL. 2>>%batpath%Evtlog.txt 1>&2
goto movefile
:usage
echo Ftpfailed:8Usage error. 2>>%batpath%Evtlog.txt 1>&2
goto movefile
:error
echo Ftpfailed:9Error in login configuration file. 2>>%batpath%Evtlog.txt 1>&2
goto movefile
:library
echo Ftpfailed:10Library initialization failed. 2>>%batpath%Evtlog.txt 1>&2
goto movefile
:session
echo Ftpfailed:11Session initialization failed. 2>>%batpath%Evtlog.txt 1>&2
goto movefile
:movefile
rem Move yesterday log directory trees and files to the destination directory bak
move /y %batpath%%dname% %batpath%bak\%dname% 2>>%batpath%Evtlog.txt 1>&2
if %errorlevel% NEQ 0 echo Moveerror:A duplicate file name exists, or the file cannot be found. 2>>%batpath%Evtlog.txt 1>&2
if %errorlevel% EQU 0 echo Move finish! 2>>%batpath%Evtlog.txt 1>&2
goto exit
:exit
rd %batpath%%dname% /q >nul 2>nul
echo ----------%dt% Ftp Log------------ 2>>%batpath%Evtlog.txt 1>&2
-----------------------------------------------------------
需求:本地有个软件,每天生成相应的Log文件,并保存在以日期时间为目录的文件夹下。然后需要在后一天,将前一天生成的Log文件及文件夹上传到一个日志服务器进行保存。
实现工具:bat脚本,Ncftp工具,计划任务
其中,因为系统默认的ftp命令行,不支持passive模式,而Port模式下,需要修改或开启Log生成端服务器的防火墙,需要最终改用网络上免费的小工具Ncftp,这个小工具比系统自带的强大了N倍,支持命令行,支持PASV模式也可以切换PORT模式,还支持断点续传,上传成功后删除本地文件等。
实现步骤:
1)转到脚本所在目录;
2)上传之前未能成功上传的文件;
3)取到今天需要上传的文件夹名称;
4)上传今天需要上传的文件夹及文件;
5)如果成功,删除本地文件,如果不成功,将文件夹移到备份目录;
6)删除空目录;
以下是具体的脚本内容(英文版系统):
-----------------------------------------------------------
@echo off
set dt=%date:~10,4%%date:~4,2%%date:~7,2%
set batpath=%~dp0
set dname=
rem Turn to batfile's path
%~d0
cd %batpath%
mkdir bak >nul 2>nul
rem Re-upload bak log directory and file to NMS Server
type %batpath%Evtlog.txt | find "Ftpfailed" >nul 2>nul
if %errorlevel% EQU 0 goto reupload
if %errorlevel% NEQ 0 goto reupend
:reupload
echo ----------%dt% Re-upload bak Log------------ 2>>%batpath%Evtlog.txt 1>&2
%BatPath%ncftpput -u administrator -p password -r 5 -z -R -DD 192.168.1.80 /192.168.1.53 %batpath%bak\* 2>>%batpath%Evtlog.txt 1>&2
if %errorlevel% EQU 0 goto cleanrelog
if %errorlevel% NEQ 0 goto reupfailed
:cleanrelog
cd.>%batpath%Evtlog.txt
for /f "tokens=*" %%a in ('dir /b /ad /s %batpath%bak\^|sort /r') do rd "%%a" /q 2>nul
goto reupend
:reupfailed
echo %dt%:Ftpfailed Reupload files the day before yesterday failed! 2>>%batpath%Evtlog.txt 1>&2
goto reupend
:reupend
rem Get the yesterday date
for /d %%i in (20*) do if %%i LSS %dt% set dname=%%i
if exist %dname% goto ncftp
if not exist %dname% goto :eof
:ncftp
rem Transfer yesterday log file and folder to NMS Server
echo ----------%dt% Ftp Log------------ 2>>%batpath%Evtlog.txt 1>&2
%BatPath%ncftpput -u administrator -p password -r 5 -z -R -DD 192.168.1.80 /192.168.1.53 %batpath%%dname% 2>>%batpath%Evtlog.txt 1>&2
if %errorlevel% EQU 11 goto session
if %errorlevel% EQU 10 goto library
if %errorlevel% EQU 9 goto error
if %errorlevel% EQU 8 goto usage
if %errorlevel% EQU 7 goto url
if %errorlevel% EQU 6 goto directorytimeout
if %errorlevel% EQU 5 goto directory
if %errorlevel% EQU 4 goto transfertimeout
if %errorlevel% EQU 3 goto transfer
if %errorlevel% EQU 2 goto connecttimeout
if %errorlevel% EQU 1 goto connect
if %errorlevel% EQU 0 goto success
:success
echo ncFtp:0Success. 2>>%batpath%Evtlog.txt 1>&2
goto exit
:connect
echo Ftpfailed:1Could not connect to remote host. 2>>%batpath%Evtlog.txt 1>&2
goto movefile
:connecttimeout
echo Ftpfailed:2Could not connect to remote host - timed out. 2>>%batpath%Evtlog.txt 1>&2
goto movefile
:transfer
echo Ftpfailed:3Transfer failed. 2>>%batpath%Evtlog.txt 1>&2
goto movefile
:transfertimeout
echo Ftpfailed:4Transfer failed - timed out. 2>>%batpath%Evtlog.txt 1>&2
goto movefile
:directory
echo Ftpfailed:5Directory change failed. 2>>%batpath%Evtlog.txt 1>&2
goto movefile
:directorytimeout
echo Ftpfailed:6Directory change failed - timed out. 2>>%batpath%Evtlog.txt 1>&2
goto movefile
:url
echo Ftpfailed:7Malformed URL. 2>>%batpath%Evtlog.txt 1>&2
goto movefile
:usage
echo Ftpfailed:8Usage error. 2>>%batpath%Evtlog.txt 1>&2
goto movefile
:error
echo Ftpfailed:9Error in login configuration file. 2>>%batpath%Evtlog.txt 1>&2
goto movefile
:library
echo Ftpfailed:10Library initialization failed. 2>>%batpath%Evtlog.txt 1>&2
goto movefile
:session
echo Ftpfailed:11Session initialization failed. 2>>%batpath%Evtlog.txt 1>&2
goto movefile
:movefile
rem Move yesterday log directory trees and files to the destination directory bak
move /y %batpath%%dname% %batpath%bak\%dname% 2>>%batpath%Evtlog.txt 1>&2
if %errorlevel% NEQ 0 echo Moveerror:A duplicate file name exists, or the file cannot be found. 2>>%batpath%Evtlog.txt 1>&2
if %errorlevel% EQU 0 echo Move finish! 2>>%batpath%Evtlog.txt 1>&2
goto exit
:exit
rd %batpath%%dname% /q >nul 2>nul
echo ----------%dt% Ftp Log------------ 2>>%batpath%Evtlog.txt 1>&2
-----------------------------------------------------------
相关文章推荐
- LoadRunner 脚本实现本地文件上传到服务器
- Windows下通过bat脚本实现自动上传文件到ftp服务器
- 实现自动上传文件到ftp服务器的BAT脚本分享
- LoadRunner 脚本实现本地文件上传到服务器
- mysql定期备份数据并压缩上传到ftp服务器的bat文件脚本,恢复建议使用SQLyog enterprise
- bat脚本使本地通过ftp和服务器传输文件
- 实现自动上传文件到ftp服务器的BAT脚本分享
- bat脚本自动上传文件到Linux FTP服务器上
- linux下的ssh工具之,本地上传到linux服务器and Linux服务器文件另存为本地。非sftp工具。
- 上传文件到服务器,从服务器下载文件到本地
- 定时上传文件到服务器脚本
- linux自动备份文件 并上传到远程服务器 脚本实现
- 将本地文件上传到指定的服务器(HttpWebRequest方法)
- shell脚本选择LOG里面特定的行,生成新文件并rsync上传
- JavaScript进阶(九)JS实现本地文件上传至阿里云服务器
- 通过复制-粘贴的方式将文件从本地上传到服务器上
- 本地文件上传到服务器 php代码
- java调用svnkit工具类上传本地文件到svn服务器
- ftp上传文件,本地安装了,服务器上也需要在也安装一个ftp
- 手机默认录制视频生成mp4文件,现在准备上传到服务器,网上有什么好的开源压缩框架吗?不压缩文件太大