priyabrata mohanty
priyabrata mohanty

Reputation: 77

Get the newest file in any folder using batch script

I have a folder which contains text and excel files. I want the newest or most newly modified excel file . I need to use that file name as an argument to some other program.

I am using below code:

@ECHO OFF
SET FILE="D:\IncrementalCompile\deploy\*.xlsx"
SET cmd="for /F %%i in ("%FILE%") do @echo %%~ni"
FOR /F "tokens=*" %%a in ('%cmd%') do SET arg1=%%a
ECHO "%arg1%"

i am able to do this using Python. But my need is to use a batch program. But this is not giving me the right result. Please help or suggest. Thanks.

Upvotes: 1

Views: 1232

Answers (2)

ddbug
ddbug

Reputation: 1539

We are now in year 2018, and here is a powershell one-liner:

dir *.xls | sort-object -property lastwritetime -descending | select-object -first 1

Upvotes: 0

selbie
selbie

Reputation: 104494

@ECHO OFF
for /F %%i in ('dir D:\IncrementalCompile\deploy\*.xlsx /B /OD /A-D') do set arg1=%%i
ECHO "%arg1%"

Upvotes: 2

Related Questions