如何找到最后一个周日的日期,并把它保存在一个变量在Windows批处理文件 [英] How do I find last Sunday's date and save it in a variable in a Windows batch file

查看:142
本文介绍了如何找到最后一个周日的日期,并把它保存在一个变量在Windows批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要连接到FTP服务器并下载一个只在周日和周日的YYYY-MM-DD-HH-MM-SS创建附加到文件名的文件的脚本。我需要找到的最后一个星期日的日期(以今天的日期,我假设),并将其转换为YYYY-MM-DD(我不在乎的时候),所以我可以构建在我的FTP脚本文件名。我已经搜索了很多关于这一点,其他网站的线程,但我是那种在批量语法的新手。我不能对机器上的日期格式将运行该脚本的假设,但它会在同一个时区作为FTP服务器,它将至少是Windows 7上运行我想过的如何找到通过批量的最后一个周日的日期,但我读过有与PS脚本可移植性问题。任何帮助是极大的AP preciated。让我知道如果我需要提供更多的细节。谢谢!

I have a script that needs to connect to an ftp server and download a file that is only created on Sunday and Sunday's yyyy-mm-dd-hh-mm-ss is appended to the file name. I need to find the last Sunday's date (based on today's date, I assume) and convert it to yyyy-mm-dd (I don't care about the time) so I can construct the filename in my ftp script. I have searched a lot of threads on this and other sites, but I'm kind of a novice at batch syntax. I cannot make assumptions about the date format on the machine that will run this script, but it will be in the same timezone as the ftp server and it will be running at least Windows 7. I thought about using the PowerShell solution in HOW to find last SUNDAY DATE through batch but I've read there are issues with PS script portability. Any help is greatly appreciated. Let me know if I need to provide more detail. Thanks!

推荐答案

几年前,我写了一个批处理脚本发现昨天的日期。我做了它能够计算出昨天基于今天的日期。它考虑到30日或31日,甚至未来几年闰年结束帐户个月。顺便我写的预计日期为格式周三2016年2月24日或DDD MM / DD / YYYY,所以它可能不会对你有用。

A couple years ago I wrote a batch script to find yesterday's date. I made it able to calculate 'yesterday' based on today's date. It takes into account months ending on the 30th or 31st, and even the next few leap years. The way I wrote it expects the date to be in the format 'Wed 02/24/2016' or 'ddd MM/DD/YYYY', so it may not be useful to you.

正如我现在看它,它可能复杂得多,它需要和很可能使用一些清理,但它的工作为我的目的。你也许能够以某种方式修改它,使其找到的最后一个星期日,而不是昨天。

As I look at it now, it's probably more complicated than it needs to be and could probably use some cleanup, but it worked for my purposes. You might be able to modify it somehow to make it find last Sunday, instead of yesterday.

set yearCounter=0
set yyyy=%date:~10,4%
set mm=%date:~4,2%
set dd=%date:~7,2%

::use these to override the actual date values for testing
::set yyyy=xxxx
::set mm=xx
::set dd=xx

if %dd%==01 goto LDoM ::Last Day of Month

set DS=%yyyy%%mm%%dd%
set /A yesterday=%DS%-1
goto endyesterday

:LDoM
set /A lastyyyy=%yyyy%-%yearCounter%

if %yesterday:~4,2%==01 set lastmm=12& set lastdd=31& goto LDoY ::Last Day of Year
if %yesterday:~4,2%==02 set lastmm=01& set lastdd=31
if %yesterday:~4,2%==03 set lastmm=02& goto february
if %yesterday:~4,2%==04 set lastmm=03& set lastdd=31
if %yesterday:~4,2%==05 set lastmm=04& set lastdd=30
if %yesterday:~4,2%==06 set lastmm=05& set lastdd=31
if %yesterday:~4,2%==07 set lastmm=06& set lastdd=30
if %yesterday:~4,2%==08 set lastmm=07& set lastdd=31
if %yesterday:~4,2%==09 set lastmm=08& set lastdd=31
if %yesterday:~4,2%==10 set lastmm=09& set lastdd=30
if %yesterday:~4,2%==11 set lastmm=10& set lastdd=31
if %yesterday:~4,2%==12 set lastmm=11& set lastdd=30

set yesterday=%lastyyyy%%lastmm%%lastdd%
goto endYesterday

:february
set leapyear=n
set lastdd=28
if %yesterday:~0,4%==2016 set leapyear=y
if %yesterday:~0,4%==2020 set leapyear=y
if %yesterday:~0,4%==2024 set leapyear=y
if %yesterday:~0,4%==2028 set leapyear=y
if %leapyear%==y set lastdd=29

set yesterday=%lastyyyy%%lastmm%%lastdd%
goto endYesterday

:LDoY
set /A yearCounter=%yearCounter%+1
set /A lastyyyy=%yyyy%-%yearCounter%
set yesterday=%lastyyyy%%lastmm%%lastdd%

:endYesterday
@echo off
echo %yyyy%  %lastyyyy%
echo %mm%    %lastmm%
echo %dd%    %lastdd%
echo.
echo today     = %yyyy%%mm%%dd%
echo yesterday = %yesterday%

这篇关于如何找到最后一个周日的日期,并把它保存在一个变量在Windows批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆