How to find the newest file between several files

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

How to find the newest file between several files

Post by MigrationUser »

20 May 2014 12:46
boushta

let's say that i have the following files

d:\user\delete.cmd
d:\user2\ren.cmd
d:\user3\rename.cmd
d:\user\batch\ren.cmd
c:\windows\copy.cmd
c:\.....................\anybatch.cmd
e:\.....................\anyfile
x:\.....................\...........

how can i determine the newest file between them all and finally delete the others old file

----------------------------

#2 20 May 2014 14:00
foxidrive

This is untested and should keep the newest file in a folder tree of files in the current directory.

Code: Select all

:: Robocopy - Latest modified file in folder tree in UTC and sorted yyyy.mm.dd hh.mm.ss
@echo off

    set "folder=%cd%"

    for /f "tokens=1,2,*" %%a in (
        'robocopy "%folder%" "%folder%" "*" /s /is /nocopy /nc /ns /ts /fp /np /ndl /njh /njs /xjd /r:0 /w:0 /l ^| sort /r '
    ) do set "latest=%%c" & set "d=%%a" & set "t=%%b" & goto :done
    :done

    echo lastest file is: %d% %t% UTC   %latest%

    for %%f in ("%latest%") do echo lastest file is: %%~tf    Local %%~ff

:: Robocopy - deletes files in folder tree and keeps the latest one
@echo off

    set "folder=%cd%"

    for /f "skip=1 tokens=1,2,*" %%a in (
        'robocopy "%folder%" "%folder%" "*" /s /is /nocopy /nc /ns /ts /fp /np /ndl /njh /njs /xjd /r:0 /w:0 /l ^| sort /r '
    ) do del "%%c" 
pause
----------------------------

#3 20 May 2014 14:48

boushta

what i really want is to run each file against each other not againt folder
something like that

Code: Select all

for %%A in (   
        "d:\user\delete.cmd" 
        "d:\user2\ren.cmd 
        "d:\user3\rename.cmd" 
        "d:\user\batch\ren.cmd" 
        "c:\windows\copy.cmd"
        ) do for %%G in (
        "d:\user\delete.cmd" 
        "d:\user2\ren.cmd 
        "d:\user3\rename.cmd" 
        "d:\user\batch\ren.cmd" 
        "c:\windows\copy.cmd"
        ) do call :getnewer %%A "%%~G"

echo file newer is %newer%
goto :eof
:getnewer
::this does not work
if "~t1" gtr %~t2 set "newer=%~1" else set "newer=%~2"
goto :eof
but i think your approach should work by creating a directory and copying those file to this directory

md "d:\test dirctory"
set "folder=d:\test dirctory"

----------------------------

#4 21 May 2014 14:55
bluesxman


I'm not sure if you're explaining your requirements very well (I certainly am struggling to understand what you're trying to achieve). But taking you at your word, Something like this, perhaps (assumes dd/mm/yyyy date format... For mm/dd/yyyy format, change %%c/%%b/%%a to %%c/%%a/%%b)

Code: Select all

@echo off
set fileList="d:\user\delete.cmd" "d:\user2\ren.cmd" "d:\user3\rename.cmd" "d:\user\batch\ren.cmd" "c:\windows\copy.cmd"
(for %%x in (%fileList%) do for /f "tokens=1-5* delims=/: " %%a in ("%%~tx %%~fx") do set /p "=%%c/%%b/%%a %%d:%%e %%f" <nul & echo:) | sort /r > "%temp%\tempfile"

for /f "usebackq skip=1 tokens=*" %%a in ("%temp%\tempfile") do echo del "%%~a"
Only partially tested.

Last edited by bluesxman (22 May 2014 08:08)

cmd | *sh | ruby | chef

----------------------------

#5 13 Jun 2014 17:57
boushta

nice approach bluesxman ,i didn't test it yet but this should work

----------------------------

#6 14 Jun 2014 20:26
boushta

thanks it worked
i changed a little bit your code and it worked just fine and this approach is great when working
with files larger in size so i don't have to copy all the files to a root diretory and run foxidrive code against it
with bluesxman code i have only to sort no copy needed and it is even greater when there is 2 or more files with the same name
so i do not have to create nested subdirectories.

Code: Select all

set fileList="d:\user\delete.cmd" "d:\user2\ren.cmd" "d:\user3\rename.cmd" "d:\user\batch\ren.cmd" "c:\windows\copy.cmd"
(for %%x in (%fileList%) do for /f "tokens=1-5* delims=/: " %%a in ("%%~tx %%~fx") do set /p "=%%c/%%b/%%a %%d:%%e %%f" <nul & echo:) > "%temp%\tempfile.txt"

type  "%temp%\tempfile.txt" | sort /r > "%temp%\sortedfile.txt"
for /f "usebackq  tokens=1-2* skip=1" %%a in ("%temp%\sortedfile.txt") do echo del "%%~c"

related: Delete All but Last N Log Files
Post Reply