P
P
ProNoob2017-06-20 10:56:00
cmd/bat
ProNoob, 2017-06-20 10:56:00

How to find the newest file via bat?

Hello, you need to use bat to determine the newest file or the last modified one (in my case there will be no difference)
I googled such a command that shows the latest file, but did not figure out how it works.
for /f %%i in ('dir /b /T:A /A:-D /O:-D *.xls')

Answer the question

In order to leave comments, you need to log in

3 answer(s)
L
longclaps, 2017-06-20
@longclaps

dir /o:-d
and read the first line. Do you want the last
dir /o:d

R
res2001, 2017-06-20
@res2001

Your example is not a finished command. In the final form it will look something like this:

set "filename="
for /f %%i in ('dir /b /T:A /A:-D /O:-D *.xls') do (
set "filename=%%~i"
goto:finloop
)
:finloop
echo.%filename%

Everything is built on the dir command's ability to sort files using the /o and /t options. And then the first line of the dir output is simply taken, saved to a variable, and at the end the variable is displayed on the screen.

Y
Yaroslav, 2019-11-12
@MadRic

Good afternoon.
part of this topic. There is a folder, there are different Excel files in it, the task is to copy the most recent by date of update, copy it to another folder and add a name so that a story is created.
There is a working setup, but it takes the correct month, but does not take into account the date and time, i.e. if there are files from the 4th and 10, then it will copy the 4th.
Where is the mistake:

@ECHO off
setlocal
CHCP 65001 
 
SET EXT=.xlsx
SET FOLDER=G:\localuser\Documents\13_Warehouse_report\Slots\test\
SET BACKUP_FOLDER=G:\localuser\Documents\13_Warehouse_report\Slots\
SET DATE=%date:~6,4%%date:~3,2%%date:~0,2%_%time:~0,2%%time:~3,2%%time:~6,2%

for /F "delims=" %%A in ('DIR "%FOLDER%*%EXT%" /T:A /A:-D /B') DO SET NEW_FILE=%%~nA

copy "%FOLDER%%NEW_FILE%%EXT%" "%BACKUP_FOLDER%%NEW_FILE%_%DATE%%EXT%"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question