How to code a batch file to copy and rename the most recently dated file?

Posted by david.murtagh.keltie.com on Stack Overflow See other posts from Stack Overflow or by david.murtagh.keltie.com
Published on 2010-06-17T16:14:26Z Indexed on 2010/06/17 16:43 UTC
Read the original article Hit count: 298

Filed under:
|
|

I'm trying to code a batch file to copy only the most recently dated file in a given folder to another directory on the local machine, and simultaneously rename it as it does.

I've found a very similar question here

http://stackoverflow.com/questions/97371/batch-script-to-copy-newest-file

and have managed to cobble together the below code from other forums too, but have hit a brick wall as it only results in the batch file itself being copied to the destination folder. It doesn't matter to me where the batch file itself sits in order for this to run.

The source folder is C:! BATCH and the destination folder is C:\DROP

The code is below, apologies if this is a glaringly obvious answer but it's literally the first foray into coding batch files for me... Thanks!

@echo off

setLocal EnableDelayedExpansion

pushd C:\! BATCH

for /f "tokens=* delims= " %%G in ('dir/b/od') do (set newest=%%G)

copy "!newest!" C:\DROP\

PAUSE

© Stack Overflow or respective owner

Related posts about batch

Related posts about source-code