1

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.

2 Answers 2

2
@ECHO OFF
for /F %%i in ('dir D:\IncrementalCompile\deploy\*.xlsx /B /OD /A-D') do set arg1=%%i
ECHO "%arg1%"
Sign up to request clarification or add additional context in comments.

4 Comments

Its showing error: "The system could not find the file specified."
@selbie, you should add "delims=" to the code as well otherwise file names with spaces will get split apart.
@priyabratamohanty, it is a lot easier to copy and paste the error message from the console window then taking a screen shot and linking to it. Please do this going forward.
Below is the error:The system cannot find the path specified. "" Press any key to continue . . .
0

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

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

6 Comments

What does the year have to do with anything? I know people who still program in Fortran, Assembler and PL/1 because it is the best language for the tasks they need to accomplish.
But this does not work on cmd. Can i invoke the powershell into a batch script.
Of course you can. Even if script execution is disabled, it can be forced with some simple option.
@Squashman Exactly because the best language for task definition changes with time ;)
@ddbug : How can i able to invoke the powershell into a batch script.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.