1

I am trying to write a Windows batch file to find the most recent *.CPP file in a directory.

To find the most recent file in the current directory, this works:

@echo off
for /F "delims=" %%f in ('dir   /b /od /a-d "%*" ') do set File=%%f
echo %*\%File%
pause

But it only shows me the most recent file of the directory

I want to focus only on *.CPP files, among all of the files in the directory.

If it's not possible perhaps a method of excluding *.TXT, *.BAT and *.EXE files from the DIR command, *(because it's mainly those 3 kinds of files that always have more recent modified date compared to my cpp files.

Compo
  • 36,585
  • 5
  • 27
  • 39
hong lang
  • 23
  • 5
  • 2
    You know, what `%*` means? ([hint](https://ss64.com/nt/syntax-args.html)) – Stephan Jan 05 '22 at 22:01
  • hi Stephan, thanks again for your help, no i didn't really know what is %*, i like trying to code by try&fail, and mainly check other codes already exist and adjust to what I need, but have to admit that sometimes I don't know exactly what I am doing but it works :) but thanks for ss64, it's exactly the kind of website i was looking for to improve my CMD skills – hong lang Jan 05 '22 at 23:18
  • 2
    When you are learning from scratch anyway, I'd recommend Powershell instead. It's the successor of `cmd` and a designed, consistent language, while `cmd` "somehow" evolved from [CPM](https://en.wikipedia.org/wiki/CP/M) from the nineteen-seventies, still struggling to stay at pace with the explosive development of computers, while trying to stay as backward compatible as possible. (I'm sure you can imagine, those two goals contradict each other). If you like to fumble, use `cmd`, if you want serious programming, use *anything* other `;)` – Stephan Jan 06 '22 at 08:19
  • 2
    To be clear: `cmd` isn't bad and you can do a lot of awesome things with it, but it has some serious limitations. For example, it's really easy to work with *files* (as you have seen), while it's awkward to work with the *contents* of files (as you soon *will* see). – Stephan Jan 06 '22 at 08:19
  • Thanks a lot Stephan for all your advices. So far for my needs CMD is enough, but if later I need to do something more complex yes I will consider other languages. Not sure how to express that in english as it's not my mother language, but I enjoy to code in the same language that CPU can understands, even if it's more complex or have less functions, but I feel it's more powerful. When I need to use for example Java, I have less the hapiness and feeling to write some code. Maye I am the same as people still like to speak latin, to feel more near from our roots. – hong lang Jan 06 '22 at 11:00

2 Answers2

1

oh, i notice a very useful comment from Stephan from my previous question here few days ago : search CMD on internet search engines instead of DOS

and miracle my duckduckgo found this : Get newest file in directory with specific extension

so here is the answer ! (credit to Mofi)

FOR /F "eol=| delims=" %%I IN ('DIR *.CPP /A-D /B /O-D /TW 2^>nul') DO (
SET NewestFile=%%I
GOTO FoundFile
)
ECHO No *.CPP file found!
GOTO :EOF

:FoundFile
ECHO Newest *.CPP file is: %NewestFile%

pause

if someone needs the same code to find other specific newest file in directory just change the three CPP arguments by what you need

I just realize that the information I need is not only to ECHO of the name of the most recent CPP file in the directory, but also its date, if I find how to do I will update it here, if not help is still needed here ^^ EDIT: ok found it again thanks to this ♥awesome website♥

FOR /F "eol=| delims=" %%I IN ('DIR *.CPP /A-D /B /O-D /TW 2^>nul') DO (
SET NewestFile=%%I
SET NewestFileDate=%%~tI
GOTO FoundFile
)
ECHO No *.CPP file found!
GOTO :EOF

:FoundFile
ECHO Newest *.CPP file is: %NewestFile% %NewestFileDate%

pause

CMD batch files are very interesting!!! but also very time consuming to know how to write them correctly when we are novice ^^

hong lang
  • 23
  • 5
1

Here is another way to accomplish this in a Windows batch-file run by cmd. Change *.txt to *.cpp or whatever you like. If you are on a supported Windows system, PowerShell is available.

@powershell -NoLogo -NoProfile -Command ^
    Get-ChildItem -File -Filter *.txt ^| ^
        Sort-Object LastWriteTime -Descending ^| ^
        Select-Object -First 1 -Property Name,LastWriteTime ^| ^
        ForEach-Object { \"Newest .txt file is $($_.Name) at $($_.LastWriteTime)\" }

Example usage:

21:05:48.58  C:\Users\lit\Documents
C:>\src\t\mostrecentfile.bat
Newest .txt file is developer-onboarding.txt at 09/07/2021 10:34:40
lit
  • 14,456
  • 10
  • 65
  • 119
  • Thanks, it's always interesting to see how different code languages can give the same results. If one day I need to use powershell I will first begin to compare your few lines with my few cmd lines to begin to understand how it works, thanks. – hong lang Jan 06 '22 at 11:05