I have a process that creates a subfolder every day. The name of the folder is today’s date, for example, 2023-10-19
If todays date is a public holiday I want to delete the folder with today’s date as the name. I have tried the following code, but it fails, and to be honest, I have no idea why. It’s not my code and I dont understand it at all.
Thanks in advance.
@echo off
setlocal enabledelayedexpansion
REM Set the folder path where the "test" folder is located
set "parentFolder=L:\test"
REM Set the folder name to delete (same as the date)
set "folderToDelete=%date:~10%-%date:~4,2%-%date:~7,2%"
REM Set the list of target dates (format: YYYY-MM-DD)
set "targetDates=2023-10-19"
REM Check if today's date exists in the target dates list
if not "%targetDates%"=="%targetDates: % %g" (
for %%D in (%targetDates%) do (
if "%%D"=="%folderToDelete%" (
REM Delete the folder within the "test" folder
set "folderPath=%parentFolder%\test\%folderToDelete%"
if exist "!folderPath!" (
rd /s /q "!folderPath!"
echo Folder "!folderPath!" deleted.
)
goto :end
)
)
)
Any help greatly appreciated.
I’m not sure what
%targetDates: % %g
is supposed to be, but that’s definitely the wrong number of percent signs.I recommend reading the answers on question Time is set incorrectly after midnight and use the ROBOCOPY solution for getting current date in format
yyyy-MM-dd
. The question cannot be answer because of the code is not a minimal, reproducible example. The format of the dynamic variable DATE depends on configured country/region of the used account. The environment variabletargetDates:
with a colon and a space at end of the variable name is not defined at all in the code. Is%g
a loop variable reference with a missing%
and loop?