How do I write a Windows batch script to copy the newest file from a directory?

batch-file, forfiles

Solution

Windows shell, one liner:

FOR /F "delims=" %%I IN ('DIR *.* /A-D /B /O:-D') DO COPY "%%I" <<NewDir>> & EXIT

Problem

I need to copy the newest file in a directory to a new location. So far I've found resources on the forfiles command, a date-related question here, and another related question. I'm just having a bit of trouble putting the pieces together! How do I copy the newest file in that directory to a new place?

Original source

Related problems