0

The scenario is like this. I need a previous day date (i.e. Today -1) as variable in a batch file. But if previous day is Sunday (i.e. script running on Monday) it should return the Saturday's date (i.e. Today -2). I have tried the below script but it doesnt seems to be working. Can anybody help please.

ECHO STARTING
FOR /F "tokens=* USEBACKQ" %%F IN (`powershell -Command "& {if ^(^([Int] ^(Get-Date^).DayOfWeek^) -eq 1^) {get-date^(^(get-date^).addDays^(-2^)^) -uformat "%%d.%%m.%%Y"} Else{get-date^(^(get-date^).addDays^(-1^)^) -uformat "%%d.%%m.%%Y"} }`) DO (SET var=%%F)

ECHO DATE CAPTURED IN VARIABLE IS %var%

I have tried following command directly at command promt and it is working properly.

powershell -Command "& {if (([Int] (Get-Date).DayOfWeek) -eq 1) {get-date((get-date).addDays(-2)) -uformat "%d.%m.%Y"} Else{get-date((get-date).addDays(-1)) -uformat "%d.%m.%Y"} }
Niraj Jaradi
  • 1
  • 1
  • 3
  • you could save it to a file &b then import it, OR set an `$env:Var` and read that, OR switch entirely to powershell since that is so much easier to understand ... [*grin*] – Lee_Dailey May 05 '20 at 13:36

1 Answers1

0

Save

if (((Get-Date).DayOfWeek.value__) -eq 1) {get-date((get-date).addDays(-2)) -uformat "%d.%m.%Y"} Else{get-date((get-date).addDays(-1)) -uformat "%d.%m.%Y"}

as a .ps1 file; for example C:\Batch\getvar.ps1

Then call this in your batch script with

FOR /F "tokens=* USEBACKQ" %%F IN (`powershell -ExecutionPolicy Bypass -File C:\Batch\getvar.ps1`) DO (SET var=%%F)
A J Wilson
  • 338
  • 2
  • 10