简体   繁体   English

Windows批处理文件和FTP日期戳

[英]Windows Batch File and an FTP Datestamp

I am struggling to grasp the concept of writing batch files. 我正在努力掌握编写批处理文件的概念。 I am hoping to schedule a .bat file to download a file with a timestamp on a daily basis. 我希望安排一个.bat文件来每天下载带有时间戳的文件。 Setting up the scheduled task etc. is straight forward, my problem arises when I try execute the code to take the fixed part of the filename and append a datestamp to identify the appropiate file. 设置计划任务等很简单,当我尝试执行代码以获取文件名的固定部分并附加一个日期戳以标识适当的文件时,就会出现我的问题。

From the research I have done I still cannot get my code to pick up yesterday date in a YYYYMMDD format. 从我完成的研究中,我仍然无法获取我的代码以YYYYMMDD格式提取昨天的日期。 eg. 例如。 today is the 15th of August, I would like my batch to identify the file with 20130814. My exisitng (static) ftp batch code is 今天是8月15日,我希望我的批次用20130814标识文件。exisitng(静态)ftp批次代码是

option confirm off
option batch abort
open Ftp_name
lcd "U:\XXXX\YYYY\ZZZZ"

get "LoanSummary__Daily.xlsx"

Whereas I would like the batch to consider.. 而我希望该批次考虑。

get "LoanSummary__Daily_" & YYYYMMDD & ".xlsx"

Thanks. 谢谢。

I don't think you can dynamically build file names within an FTP script. 我认为您无法在FTP脚本中动态建立文件名。 But you can dynamically build the ftp script with a batch file prior to invoking it. 但是,您可以在调用之前使用批处理文件动态构建ftp脚本。

The simplest way to do this is to create an ftp script template that has the variable portion(s) represented as an environment variable with delayed expansion. 最简单的方法是创建一个ftp脚本模板,该模板的变量部分表示为具有延迟扩展的环境变量。 For example !yesterday! 例如, !yesterday! could refer to an environment variable that gets expanded into yesterday's date. 可以引用扩展到昨天日期的环境变量。 A simple FOR /F loop reads the template, and writes each line to a new ftp script file. 一个简单的FOR / F循环读取模板,并将每一行写入新的ftp脚本文件。 The variables are automatically expanded in the process as long as delayed expansion is enabled. 只要启用了延迟扩展,变量就会在过程中自动扩展。

Getting yesterday's date in a Windows batch file is a non-trivial excercise. 在Windows批处理文件中获取昨天的日期并非易事。 There have been many methods posted on SO as well as other sites. 在SO以及其他站点上已经发布了许多方法。 Most methods have various limitations, the most common of which is susceptability to problems due to differences in locale date formatting. 大多数方法都有各种局限性,其中最常见的是由于语言环境日期格式的差异而容易出现问题。 I use a hybrid batch/JScript utility called getTimestamp.bat that will work on any Windows platform from XP onward, regardless of the locale. 我使用了一个名为getTimestamp.bat的混合批处理/ JScript实用程序,该实用程序可以在XP及更高版本的任何Windows平台上运行,无论使用哪种语言环境。 It is pure script, so no .exe download is needed. 它是纯脚本,因此无需下载.exe。 getTimestamp.bat is avaiable here . getTimestamp.bat在这里可用

Assuming getTimestamp.bat is in you current directory, or better yet, somewhere within your PATH, then the following should work. 假设getTimestamp.bat位于您当前的目录中,或者更好的是位于PATH中的某个位置,则应该可以进行以下操作。

getYesterday.ftp.template getYesterday.ftp.template

option confirm off
option batch abort
open Ftp_name
lcd "U:\XXXX\YYYY\ZZZZ"

get "LoanSummary__Daily_!yesterday!.xlsx"

getYesterday.bat getYesterday.bat

@echo off
setlocal enableDelayedExpansion

:: Get yesterday's date in YYYYMMDD format
call getTimestamp -od -1 -f {yyyy}{mm}{dd} -r yesterday

:: Create a temporary ftp script that uses the "yesterday" date
>temp.ftp (for /f "delims=" %%L in (getYesterday.ftp.template) do echo %%L)

:: Now simply invoke your ftp client using the temp script, something like
ftp -s:temp.ftp ...

:: Delete the temporary ftp script
del temp.ftp

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

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