如何在Windows命令中获取以前的工作日(通过Powershell) [英] how to get previous working day in windows command (via powershell)

查看:193
本文介绍了如何在Windows命令中获取以前的工作日(通过Powershell)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在dos批处理文件中获取上一个工作日,因为要处理的文件的文件名中的日期为yyyymmddddmmmyy格式.

I need to get the previous working day in a dos batch file, as the files to handle has the date in the filenames, under yyyymmdd and ddmmmyy format.

根据执行获取第二天日期的操作,这似乎在纯dos命令中相当复杂.

It seems, according to Dos to get next day date , this is rather complex in pure dos command.

所以我转向在dos中调用powershell.到目前为止,我到这里为止:

So I turned to calling powershell in dos. so far I get till here:

FOR /f "usebackq" %%i IN (`PowerShell $date ^= Get-Date^; $date.ToString^('yyyyMMdd'^)`) DO SET yyyymmdd=%%i
FOR /f "usebackq" %%i IN (`PowerShell $date ^= Get-Date^; $date.ToString^('ddMMMyy'^)`)  DO SET ddmmmyy=%%
FOR /f "usebackq" %%i IN (`PowerShell $date ^= Get-Date^; $date^=$date.AddDays^(-1^)^; $date.ToString^('yyyyMMdd'^)`) DO SET yyyymmddEOD=%%i 
FOR /f "usebackq" %%i IN (`PowerShell $date ^= Get-Date^; $date^=$date.AddDays^(-1^)^; $date.ToString^('ddMMMyy'^)`)  DO SET ddmmmyyEOD=%% 

这样做,yyyymmddddmmmyy是今天的日期,而yyyymmddEODddmmmyyEOD是前一天.

by doing so, yyyymmdd and ddmmmyy is today's date, while yyyymmddEOD and ddmmmyyEOD is the day before.

但是,如果我需要获得上一个工作日",该怎么办?

However, what shall I do, if i need to get the previous "working day"?

工作日,最好的解决方案可能是

By working day, the best solution would probably like Excel WorkDay function, WORKDAY(start_date, days, [holidays]), but for now, for the sake of simplicity, let's just take Monday to Friday as Working Day. So that if today is Tuesday, then the previous working day is Monday, while if today is Monday, then the previous working day is previous Friday.

有关如何获取下一个工作日"的示例,请访问> Powershell高尔夫:下一项业务day ,但它似乎在powershell界面下运行.我尝试过但未成功将其转换为dos批处理文件.

There's an example on how to get the "next working day" at Powershell Golf: Next business day , however, it seems run under powershell interface. I tried but did not succeed turn it into a dos batch file.

推荐答案

这可能更具可读性.您可以根据需要进行调整.不需要转义字符.

This might be a bit more readable. You can adjust as you need. No escaping characters are necessary.

@echo off & setlocal enabledelayedexpansion
set days=-0 -1 -3
set formats=yyyyMMdd ddMMMyy
set ps=[DateTime]::Now.AddDays(%%b).ToString('%%a')

REM Get today, yesterday, and three days ago in two date formats
for %%a in (%formats%) do (
    for %%b in (%days%) do (
        for /f %%p in ('"powershell %ps%"') do set date-%%a%%b=%%p
    )
)

REM If today is Monday, subtract 3 for EOD, else subtract 1
for /f %%a in ('"powershell [DateTime]::Now.ToString('ddd')"') do (
    if "%%a"=="Mon" (set sub=3) else (set sub=1)
)

REM Change the variable names if you want
set yyyymmdd=%date-yyyymmdd-0%
set ddmmmyy=%date-ddmmmyy-0%
set yyyymmddEOD=!date-yyyymmdd-%sub%!
set ddmmmyyEOD=!date-ddmmmyy-%sub%!

REM Output your variables
set yyyymmdd
set ddmmmyy

输出(今天,10月12日,星期三):

Output (for today, Wed, Oct 12):

yyyymmdd=20161012
yyyymmddEOD=20161011
ddmmmyy=12Oct16
ddmmmyyEOD=11Oct16

编辑

这是一种更简单的方法.经过一夜的睡眠,对我来说似乎更明显. =)我没有替换我上面写的内容,因为它会使您的评论无效,但是我认为它可能仍然有用,所以我添加了它.

Here is an even simpler way of doing it. It seemed more obvious to me after a night of sleep. =) I didn't replace what I wrote above because it would have invalidated your comment, but I thought it might be useful still, so I added it.

@echo off

REM If today is Monday, subtract 3 for EOD, else subtract 1
if %date:~0,3%==Mon (set EOD=-3) else (set EOD=-1)

REM Get today and last EOB in two date formats
for %%a in (yyyyMMdd ddMMMyy) do (
    for /f %%p in ('"powershell [DateTime]::Now.ToString('%%a')"') do set %%a=%%p
    for /f %%p in ('"powershell [DateTime]::Now.AddDays(%EOD%).ToString('%%a')"') do set %%aEOD=%%p
)

REM Output your variables
set yyyymmdd
set ddmmmyy

这篇关于如何在Windows命令中获取以前的工作日(通过Powershell)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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