I am trying to find most recent sub-folder within a directory only if that directory exists. My code does not give me any output when I use FOR
loop within IF
statement. Here is my code -
IF EXIST "D:\MyDirectory" (
FOR /F "delims=" %%i IN ('dir "D:\MyDirectory" /b /ad-h /t:c /od') DO SET abc=%%i
echo Most recent subfolder: %abc%
) ELSE (
echo Directory not found
)
My code works fine separately
1. To check separately if the directory exists,
IF EXIST "D:\MyDirectory" (
echo Directory exists
) ELSE (
echo Directory does not exists
)
2. To find the most recent folder,
FOR /F "delims=" %%i IN ('dir "D:\MyDirectory" /b /ad-h /t:c /od')
DO SET abc=%%i
echo Most recent subfolder: %abc%
Basically I get correct output of the most recent folder when I don't combine the FOR
loop and the IF
statement. What I am observing here is, the code echos blank output for the variable 'abc' when we use FOR
loop within IF
statement. My question is, doesn't batch script allow us to use FOR
loop within an IF statement? If so, is there any workaround I could do here to achieve the desired outcome?
Aucun commentaire:
Enregistrer un commentaire