How to get the previous working day in the windows command (via powershell)

I need to get the previous working day in the dos batch file, since the files for processing have a date in the file names in the yyyymmddand format ddmmmyy.

It seems, according to Dos, to get the date of the next day is quite difficult in a clean dos command.

So, I turned to calling powershell in dos. still i get 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=%% 

doing this, yyyymmddand ddmmmyy- today's date, yyyymmddEODand ddmmmyyEOD- the day before.

However, what should I do if I need to get the previous “business day”?

During the working day the best solution would probably be Excel WorkDay function , WORKDAY(start_date, days, [holidays])but now, for the sake of simplicity, let's just Monday to Friday as a working day. So, if today is Tuesday, then the previous working day is Monday, and if today is Monday, then the previous working day is on the previous Friday.

Here's an example of how to get the “next business day” on Powershell Golf: the next business day , however, it seems to start under the powershell interface. I tried, but could not turn it into a dos batch file.

+4
source share
2 answers

. , . .

@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

( , , . 12):

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

. . =) , , , , , .

@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
+1

script, , :

 for /f "usebackq" %%i in (`PowerShell $date ^= Get-Date^; $date.AddDays^(-1^)^; $date.getWeekDay^(^)^;`) DO ( 
SET weekday=%%i
Goto outer
)
:outer
set weekday=%weekday:,=%
"set weekend="
if "x%weekday%"=="xSunday" set weekend=1
if "x%weekday%"=="xSaturday" set weekend=1
if defined weekend (
echo Friday
) Else (
echo %weekday%
)
pause

""

EDIT: , , :

for /f "usebackq" %%i in (`PowerShell $date ^= Get-Date^; $date.AddDays^(-1^)^; $date.getWeekDay^(^)^;`) DO ( 
SET weekday=%%i
Goto:outer
)
:outer
set weekday=%weekday:,=%
if "x%weekday%"=="xSaturday" (
    for /f "usebackq" %%i in (`PowerShell $date ^= Get-Date^; $date.AddDays^(-2^)^; $date.toString^('yyyyMMdd'^)^;`) DO ( 
    SET weekday=%%i
    )
Goto:echoWD
) Else (
 if "x%weekday%"=="xSunday" (
        for /f "usebackq" %%i in (`PowerShell $date ^= Get-Date^; $date.AddDays^(-3^)^; $date.toString^('yyyyMMdd'^)^;`) DO ( 
    SET weekday=%%i
    )
 Goto:echoWD
 ) Else (
     for /f "usebackq" %%i in (`PowerShell $date ^= Get-Date^; $date.AddDays^(-1^)^; $date.ToString^('yyyyMMdd'^)^;`) DO ( 
     SET weekday=%%i
         )
 Goto:echoWD
     )
)
:echoWD
echo %weekday%
pause

: , xSunday xSonntag , .

, , ;)

+1

Source: https://habr.com/ru/post/1657449/


All Articles