B
B
batalish2020-10-13 09:25:48
cmd/bat
batalish, 2020-10-13 09:25:48

How to copy the name of the most recent file through the batch file?

Good afternoon! The task is to select the name of the most recent file in the directory and subdirectories, then put it in the logs. Did some digging and did something like this:

@echo **************************************************************>>C:\Windows\backup\last_file.log
@echo &date /t>>C:\Windows\backup\last_file.log
@echo &time /t>>C:\Windows\backup\last_file.log
for /f "delims=" %%i in ('dir C:\TEST\user\*.mac /B /A-D /OD /TA') do (set file=%%i )
echo %file% >> C:\Windows\backup\last_file.log


But the batch file does not display the name of the latest file in subdirectories, how can I fix this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
Fenrir89, 2020-10-13
@batalish

/O:D

H
hint000, 2020-10-13
@hint000

1. you forgot /S in the dir command
2. the main problem seems to be that dir only sorts within one subdirectory, so it will print the most recent in the last subdirectory, not the most recent across all subdirectories; this fact will complicate the implementation tenfold: dir should display the names along with the date-time, and then the whole sorting will have to be hand-coded, dividing the date into fields.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question