0

I'm working with a nested if statement to delete directories if they meet a certain criteria. This is part of a for loop to check all directories in a given location. Variable %JDcalc% represents the difference between today's julian day and the directory's julian day. Variable %fdate% is the directory name. The errorlevel is deteremined by the following:

echo.%fdate% | findstr /C:"OE"

This nested if statement works:

if %errorlevel% EQU 0 (if %JDcalc% GEQ 31 (echo DELETE) else (echo KEEP)) else (if %JDcalc% GEQ 14 echo DELETE)

As soon as I switch echo DELETE to rd %fdate% the batch script outputs, "The system cannot find the specified file."

if %errorlevel% EQU 0 (if %JDcalc% GEQ 31 (rd %fdate%) else (echo KEEP)) else (if %JDcalc% GEQ 14 rd %fdate%)

If I use a simpler if statement like if %JDcalc% GEQ 14 (rd %fdate%) it works fine.

EDIT: Below is the script in question.

@echo off


cd /d C:\Batch\Maximus\Dailies

:TODAYS_JULIAN_DAY
    set /a JD=2458939
        ::a calculation that determines this number is usually here
        ::above is the julian day for 3.30.2020



:FOR_LOOP_FOLDER_NAME
    for /f "delims=" %%a in ('dir /a:d /b') do (
        SETLOCAL EnableDelayedExpansion
        set fdate=%%a
            ::fdate is set to the date in the directory name
            ::with a naming convention of yymmdd

        call :SUBROUTINE
        ENDLOCAL
        )
    GOTO END_BATCH



:SUBROUTINE
    set folyy=%fdate:~0,2%
    set folmm=%fdate:~2,2%
    set foldd=%fdate:~4,2%

    set /a "folyy=1000020%folyy% %%10000,folmm=100%folmm% %% 100,foldd=100%foldd% %% 100"
    set /a folJD=foldd-32075+1461*(folyy+4800+(folmm-14)/12)/4+367*(folmm-2-(folmm-14)/12*12)/12-3*((folyy+4900+(folmm-14)/12)/100)/4
    set /a JDcalc=JD-folJD



    echo.%fdate% | findstr /C:"OE"

    if %errorlevel% EQU 0 (if %JDcalc% GEQ 31 (rd %fdate%) else (echo KEEP)) else (if %JDcalc% GEQ 14 rd %fdate%)

    GOTO :eof



:END_BATCH

ENDLOCAL
  • 2
    If the variables are being calculated inside a FOR command block, then you need to use Delayed Expansion. Please read [Variables are not behaving as expected](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028). As it stands your question does not provide a [mcve]. – Squashman Mar 27 '20 at 19:35
  • Delayed expansion is on. for /f "delims=" %%a in ('dir /a:d /b') do ( SETLOCAL EnableDelayedExpansion set fdate=%%a call :SUBROUTINE ENDLOCAL ) The if statement is under the :subroutine label. – soliverevilos Mar 27 '20 at 20:02
  • 1
    As I said in my previous you need to provide a [mcve]. If you are not going to show us enough code to reproduce the problem nobody is going to be able to help you. – Squashman Mar 27 '20 at 20:18
  • My apologies, I've updated the original post to provide a minimal reproducible example. It's trimmed down a bit, but I'm not certain if I could've simplified further. – soliverevilos Mar 30 '20 at 12:23

0 Answers0