Forum Discussion
What is wrong with this script?
Hello,
I am struggling to find a fix for this script. Can anyone help please?
Script's logic:
IF day of run is a Monday THEN
YYYYMMDD = Friday before -- i.e. 3 days earlier
ELSE IF day of run is a Tuesday, Wednesday, Thursday, Friday, Saturday, or Sunday THEN
YYYYMMDD = day before -- i.e. 1 day earlier
END IF
The error I am seeing the CMD windows:
>if 2 EQU 1 (set "days_to_subtract=3" ) else if 2 GTR 2 (set "days_to_subtract=1" ) .AddDays(-)') was unexpected at this time
---
setlocal
for /F %%i in ('wmic path win32_localtime get dayofweek ^| findstr /r "[0-9]"') do set day=%%i
if %day% equ 1 (
set "days_to_subtract=3"
) else if %day% gtr 2 (
set "days_to_subtract=1"
)
for /F %%i in ('powershell Get-date -format "yyyyMMdd" -date (Get-date).AddDays(-%days_to_subtract%)') do set "previous_date=%%i"
echo %previous_date
Hi ikhan543
The following simple PowerShell script will return a date based on your script logic:
$dayOfWeek = (Get-Date).DayOfWeek $previousDate = if ($dayOfWeek -eq 'Monday') { (Get-Date).AddDays(-3).ToString('yyyyMMdd') } else { (Get-Date).AddDays(-1).ToString('yyyyMMdd') } $previousDate
2 Replies
- kevkellyBrass Contributor
Hi ikhan543
The following simple PowerShell script will return a date based on your script logic:
$dayOfWeek = (Get-Date).DayOfWeek $previousDate = if ($dayOfWeek -eq 'Monday') { (Get-Date).AddDays(-3).ToString('yyyyMMdd') } else { (Get-Date).AddDays(-1).ToString('yyyyMMdd') } $previousDate
- ikhan543Copper ContributorThank you Kevkelly. Nothing like getting a response from a human 🙂
The script I had was generated by ChatGPT as I don't know Windows scripting and needed a quick solution.