How To Search For Files, Compare Date Created, Apply Action

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Posts: 1
Joined: 07 Oct 2020 10:26

How To Search For Files, Compare Date Created, Apply Action

#1 Post by inevit4ble » 07 Oct 2020 10:29


We use this script to help us pull images out of folders in a directory from 1 camera, copy it to the parent folder and rename it to that of the folder name from which it came.

This is necessary as we have 170 different cameras taking multiple pictures, we manually place them into folders with a chosen name and do this up to 100 times in a day. Its for 3D scanning for vfx. At the end of the day, we run the script to get the main front image of what we scanned from each folder so we can see what we've done.


Currently, it does its job but it needs improving. The camera (A_LDB) we want the image from outputs 3 images into the folder so the script therefore outputs all 3 images from the folders. We then go and delete 2 of the 3 images and keep the 1 we want. The 1 we want is the first of the 3 (ie. oldest file)

What code can I add to this script so that it finds the oldest images and only copies that one OR How do you have it delete the 2nd and 3rd image automatically after copying them (less efficient but acceptable)

Code: Select all

@echo off


set counter=0

for /r %%a in (".\*") do for %%b in ("%%~dpa\.") do (

    set filename=%%~na

    echo.!filename! | findstr /C:"A_LDB" 1>nul

    if errorlevel 1 (

    rem DO NOTHING  

    ) ELSE (

        set /a counter=counter+1

        set parent=%%~nxb

        set type=%%~xa

        copy "%%a" !cd!\!parent!"_cs"!counter!!type!






I would appreciate any advise. I have been searching for a few days on a solution but I can't get anything to work as my understanding is very limited.

Thank you kind sirs!

Posts: 1864
Joined: 23 Jun 2013 06:15
Location: Germany

Re: How To Search For Files, Compare Date Created, Apply Action

#2 Post by penpen » 07 Oct 2020 12:08

You might use the commands "for/f" and "dir", untested for the actual directory (i don't know your involved directory structures):

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
for /f "tokens=* delims=" %%a in ('dir /B /A:-D /T:C') do (
	echo Oldest file: "%%~a"
goto :eof

Post Reply