windowsfor-loopbatch-filecmddelayedvariableexpansion

Issue with *For loop* and *delayed expansion*


This batch is to create a report which outputs the latest file of a folder. Main purpose is to see the date of generated files inside directories. To also easily check whether the target directory is update or not.

Issue :
Change the output to get the three latest files instead, also got stuck here with the For Loop inside another For Loop, I'm also using delayed expansion.

Try to use delayed expansion variable inside FOR loop (!forOPT!)

FOR ... (
    FOR /F "!forOPT!" %%a IN ('dir "!path2check!" /A-D /OD /t:w ^| FIND "/"') DO ( )
)

I would like to use variable "forOPT" in an another For Loop as to replace the "skip=10 tokens=1-5" but since For Loop doesn't like delayed expansion (!forOPT!), I'm stuck.

Edit :

Issue is now solved by the suggestion and advice of Mofi. Thank you Mofi.

As for the answer to show how, also had been made by Jeb. So thank you too Jeb.

The solution to my regarding issue is to use a CALL function instead of nesting FOR loops.

Please see my answer for the final code with also some tweaks and additional requirement to accomplish my needs.

@if (@CodeSection == @Batch) @then
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET "path2check=NULLLL"
SET "TargetDIR=%~dp0"
SET "curPATH=%~F0"
SET "newSize1=0"
SET JScall=Cscript //nologo //E:JScript !curPATH!
FOR /f "usebackq delims=" %%I IN (destPath.txt) DO (
SET "path2check=%%I"
SET cnt=0
SET totalSkip=1
FOR %%A IN ("!path2check!\*") DO SET /a cnt+=1
SET /a totalSkip=!cnt!-3

SET "forOPT=skip=!totalSkip! tokens=1-5"

ECHO !forOPT! ^<^<^< shows option
ECHO.

ECHO ---------------------

FOR /F "!forOPT!" %%a IN ('dir "!path2check!" /A-D /OD /t:w ^| FIND "/"') DO (
    SET "Date1=%%a"
    SET "Time1=%%b"
    SET "Time2=%%c"
    SET "Size1=%%d"
    SET "Filename1=%%e"
    SET newSize1=!size1:,=!
    IF !newSize1! LEQ 1000 (
      SET "newSize2=!newSize1! Bytes"
    )
    IF !newSize1! GEQ 1000 (
      FOR /F %%a IN ('!JScall! "!newSize1!/1024"') DO SET "newSize2=%%a"
      FOR /F "delims=." %%z IN ("!newSize2!") DO SET "newSize2=%%z KB"
    )
    IF !newSize1! GEQ 1000000 (
      FOR /F %%a IN ('!JScall! "!newSize1!/1024/1024"') DO SET  "newSize2=%%a"
      FOR /F "delims=." %%z IN ("!newSize2!") DO SET "newSize2=%%z MB"
    )
    ECHO !Filename1! - !newSize2! - !Time1!!Time2! - !Date1!
    )
    ECHO - EOR - !path2check!
    ECHO.
)
GOTO :EOF

@end
WScript.ECHO(eval(WScript.Arguments.Unnamed.Item(0)));

The report should also look like below. (The three latest file of each directory)

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Most recently file of path : E:\

Date: dd/mm/yyyy hh:mmPM   |   Size: xxx MB  |   Filename: abc.abc
Date: dd/mm/yyyy hh:mmPM   |   Size: xxx MB  |   Filename: abc.abc
Date: dd/mm/yyyy hh:mmPM   |   Size: xxx MB  |   Filename: abc.abc

                   -   EOR   -

This batch file was also combined and mixed together from many sites on both StackOverflow (<Thank you) and SS64 (<Thank you).

Even in explaining I'm not that good, but you can ask and I can clarify. :)

If the title need to be edited please let me know, this also may help others with similar problems.

Thank you Stack Overflow, Mofi and Jeb.


Solution

  • With help and suggestion of Mofi and Jeb this is what I came up with.