U
U
unk1nD0002021-12-02 14:34:21
cmd/bat
unk1nD000, 2021-12-02 14:34:21

How to copy files by modification date using bat?

Advise how to write a bat file that:
from %from% copies to %to% and %to2% the last file by date of change
, then to %to% and %from% leaves 5 files newer by date of change (that is, deletes files older than 5- th)
in %to2% leaves the most recent file by date of modification

My knowledge in this craft is limited to the study of the current day (I have never come into contact with this before), so I would appreciate a detailed answer with comments and examples of working code.
I would also like to know how to write a similar script using PowerShell?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2021-12-02
@unk1nD000

Deployed will not work - there is no Windows at hand. I can give direction.
You can get a list of files sorted by modification date using the command: dir /b /a-d /od *
The /od key is responsible for sorting, you can get reverse sorting, then you need to specify the /od key. The /ad switch removes directories from the output (by default, dir also displays directories).
This command should be pushed into a loop, something like this:

for /f "tokens=* delims=" %%a in ('dir /od ...') do (
  echo %%a
<тут команды тела цикла>
)

In the loop, just at the first iteration, copy the file to to and to2 and exit the loop (goto).
For further actions, similar cycles through the corresponding directories, only skip the first 5 iterations, delete the files on the rest. You can skip 5 iterations in the loop either with a counter in the loop body, or there is a skip loop option.
Approximately the scheme should already be clear.
Work out the steps of the command one at a time, and then cram all the steps into one batch file.
For details on how to use commands, see:
dir /?
for /?
set /?
goto /?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question