pstanton
pstanton

Reputation: 36640

How to list files with a timestamp greater than x

I am trying to create a list of file names whereby the LastModified timestamp is greater than a set value.

I think, I am very close with the following:

@echo off
setlocal DisableDelayedExpansion

REM delete existing output file
for %%f in (_modified) do if exist %%f.txt del %%f.txt

REM hard code timstamp threshold
set lastTime=20140801000000
echo "lastTime=%lastTime%"

REM set folder to CWD
set "folder=%CD:~2%"
echo "folder=%folder%"

REM search files in folder with extension 'jpg'
FOR /F "skip=1 tokens=1*" %%a in (
        '"wmic datafile where (path='%folder:\=\\%\\' AND Extension='jpg') get 'File Name',LastModified"'
        ) do (
    FOR /F "delims=." %%B in ("%%b") do (
        if %%B LEQ %lastTime% (echo "%%B LEQ1") else (echo "%%B GEQ1")
        if %%B GEQ %lastTime% (echo "%%B GEQ2") else (echo "%%B LEQ2") 
        if %%B GEQ %lastTime% (
            echo %%B %%a>> _modified.txt))
)

However, for a folder which contains 2 jpg files, one of which has a last modified of 2010-02-22 and the other 2014-08-14 the script prints:

"lastTime=20140801000000"
"folder=\test"
"20100222210624 LEQ1"
"20100222210624 GEQ2"
"20140814155354 LEQ1"
"20140814155354 GEQ2"

And the output file contains both file names instead of the expected result of just one.

Obviously the LEQ/GEQ comparison is not working as I expect, but I'm not sure what is incorrect.

Upvotes: 1

Views: 180

Answers (2)

Ilya
Ilya

Reputation: 4689

(it is not answer, but I can't add so long comment)

Could you check, that LEQ/GEQ works correctly in simple case (without any loops and file operations)? I.e. could you run following code?

@echo off
set a=20100222210624
set b=20140814155354
set lastTime=20140801000000        
if %a% LEQ %lastTime% (echo "%a% LEQ1") else (echo "%a% GEQ1")
if %b% GEQ %lastTime% (echo "%b% GEQ2") else (echo "%b% LEQ2") 

On my side it prints following result (as expected):

"20100222210624 LEQ1"
"20140814155354 GEQ2"

If it is the same on your side, we should search the problem somewhere else.

Also here you can find something about comparation:

IF only parses numbers when one of the compare-op operators (EQU, NEQ, LSS, LEQ, GTR, GEQ) is used. The == comparison operator always results in a string comparison.

This is an important difference because if you compare numbers as strings it can lead to unexpected results: "2" will be greater than "19" and "026" will be greater than "26".

Correct numeric comparison: IF 2 GEQ 15 echo "bigger"

Using parenthesis or quotes will force a string comparison: IF (2) GEQ (15) echo "bigger" IF "2" GEQ "15" echo "bigger"

(also, here in the accepted answer you can find another way to compare dates of files (i.e. you can create temporary file for such comparations))

Upvotes: 0

Magoo
Magoo

Reputation: 79983

I'd suggest you use

if "%%B" LEQ "%lastTime%"

or

if x%%B LEQ x%lastTime%

which will force alphabetical-mode comparison. I'd suggest that cmd is attempting to compare the values as INT32s and the values are converted to gobbledegook as they're evaluted using cmd's range limits

Upvotes: 2

Related Questions