BAT脚本:将本地昨天生成的文件,上传到服务器

因为工作需要,平生第一次用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

 

 

 

-----------------------------------------------------------

 

 

你可能感兴趣的:(变量,脚本,时间,ftp,bat)