简体   繁体   English

windows批处理文件到zip然后ftp文件

[英]windows batch file to zip and then ftp file

I currently have to windows batch files the first takes all files in a directory ending .bak and adds them to a rar file with todays date in the name 我目前必须使用Windows批处理文件,首先将所有文件放在一个以.bak结尾的目录中,然后将它们添加到rar文件中,名称中包含今天的日期

the second one ftp the rar file to another server. 第二个将rar文件ftp到另一台服务器。

the first ones code is 第一个代码是

set path="C:\Program Files\WinRAR\";%path%
set date=%dd-mm-yyyy%
rar a D:\backups\dbs.rar -ri1 -mt2 -ag[dd-mm-yyyy] -m5 D:\backups\*.BAK
del D:\backups\*.BAK

which is called from inside sql server manager when its finished backing up the databases but could easily be run from a scheduled task 当它完成备份数据库时从sql server manager内部调用,但可以很容易地从计划任务运行

the seconds code is 秒代码是

@echo off
echo user Dbusser> ftpuploader.dat
echo ftppassword>> ftpuploader.dat
echo bin>> ftpuploader.dat
echo put %1>> ftpuploader.dat
echo quit>> ftpuploader.dat
ftp -n -s:ftpuploader.dat 127.0.0.1
del ftpuploader.dat

which I manually called from a cmd promt with the name of the rar file as an argument 我从cmd promt手动调用rar文件的名称作为参数

ftpuploader.bat d:\pathtorar\dbs[21-10-2013].rar

what I want to know is can anyone tell me how to either merge the two files so it automatically uploads the file once its created and finished it or automate the schedule task to that it changes the date bit in the name of the rar file every day 我想知道的是,任何人都可以告诉我如何合并这两个文件,以便一旦创建并完成它就自动上传文件,或者自动执行计划任务,以便每天更改rar文件名称中的日期位

thanks 谢谢

This requires XP Pro and higher to use Wmic. 这需要XP Pro和更高版本才能使用Wmic。

It sets the variable with the date, in a reliable manner, and uses that to create the RAR filename and also launch the FTP batch file. 它以可靠的方式使用日期设置变量,并使用它来创建RAR文件名并启动FTP批处理文件。

I changed this line if not errorlevel 1 del D:\\backups\\*.BAK which should protect the *.bak files from being deleted if the RAR archiving fails for any reason. if not errorlevel 1 del D:\\backups\\*.BAK我更改了这一行if not errorlevel 1 del D:\\backups\\*.BAK如果RAR归档由于任何原因失败,应该保护* .bak文件不被删除。

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"

set "datestamp=%DD%-%MM%-%YYYY%"

set path="C:\Program Files\WinRAR\";%path%
rar a D:\backups\dbs[%datestamp%].rar -ri1 -mt2 -m5 D:\backups\*.BAK
if not errorlevel 1 del D:\backups\*.BAK
ftpuploader.bat D:\backups\dbs[%datestamp%].rar

Few things that I don't like in your script: I have no idea what set date=%dd-mm-yyyy% is doing .Not sure when the *bak files are deleted.If you are trying to upload binary files you need to set also the ftp script in binary mode. 在我的脚本中我不喜欢的事情很少:我不知道什么set date=%dd-mm-yyyy%正在做什么。不确定何时删除* bak文件。如果您尝试上传二进制文件,则需要以二进制模式设置ftp脚本。 Here's one possible way to do this without temp files: 这是在没有临时文件的情况下执行此操作的一种可能方法:

!&cls&echo off&setlocal ENABLEDELAYEDEXPANSION
!goto :end_ftp
!rem :: ftp part
open server
Dbusser
ftppassword
prompt
binary
mput *.BAK
bye

:end_ftp
set path="C:\Program Files\WinRAR\";%path%

rem get _date in format DD-MM-YYYY 
Set /A "Jan=1,Feb=2,Mar=3,Apr=4,May=5,Jun=6,Jul=7,Aug=8,Sep=9,Oct=10,Nov=11,Dec=12"
Type Nul >"%TEMP%\~.ddf"
Makecab /D RptFileName="%TEMP%\~.rpt" /D InfFileName=Nul /F "%TEMP%\~.ddf" >Nul
Set /P "timestamp=" <"%TEMP%\~.rpt"
For /F "tokens=3-9 delims=: " %%a In ("%timestamp%") Do (
    Set /A "year=%%g,month=%%b,day=1%%c-100"
)
Del /Q "%TEMP%\~.*"
Echo year=%year% month=%month% day=%day%
set "_date=%day%-%month%-%year%"
rem date is ready to use

rar a D:\backups\dbs.rar -ri1 -mt2 -ag[%_date%] -m5 D:\backups\%_date%\*.BAK

pushd D:\backups\%_date%\*.BAK
ftp -s:%~dpsnx0
popd
del D:\backups\%_date%\*.BAK /Q /F 
RD /S /Q D:\backups\%_date%\

The idea is that you are creating a directory with date name (I suppose your date format is dd-mm-yyyy) , then starts the ftp in this directory and using prompt (to surpass the prompting) and then using mput to upload all files with .bak extension.So no parameters are needed for the ftp script.For here are more explanations for self-contained ftp scripts. 我的想法是你创建一个带有日期名称的目录(我想你的日期格式是dd-mm-yyyy),然后在这个目录中启动ftp并使用提示符(超过提示)然后使用mput上传所有文件使用.bak扩展名。因此ftp脚本不需要任何参数。以下是对自包含ftp脚本的更多解释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM