There are literally hundreds/thousands of questions just here on SO.
I suggest you use a PowerShell one-liner for this, which you can call from a batch file as follows:
@echo off
for /f "usebackq delims=" %%A in (`
powershell -NoP -C "'{0:yyyyMMdd}' -f (Get-Date).AddDays(-1)"
`) do set YESTERDAY=%%A
%YESTERDAY%
will then contain 20190824
when invoked on 25 August 2019, for instance.
A slightly longer variant, incorporating both today
and yesterday
in only one PowerShell invocation.
:: Q:\Test\2019\04\26\SO_55862158.cmd
@echo off
for /f "usebackq delims=" %%A in (`
powershell -NoP -C "'yesterday={0:yyyyMMdd}' -f (Get-Date).AddDays(-1);'today={0:yyyyMMdd}' -f (Get-Date)"
`) do set "%%A"
The PowerShell part issues two lines
yesterday=20190824
today=20190825
which are parsed by the for /f
and set as environment variables yesterday
/today
respectivly.