Question

Locked

Copy newest file

By cmm8907 ·
I need a bat file that will copy only the newest file in a directory. I currently have this:
FOR /F "delims=" %%I IN ('DIR C:\source*.* /B /O:-D') DO COPY "%%~fI" "C:\destination"

This will sort all files in the directory and then copy them all in order of modified or created date starting with the newest one.

Basically I need it to stop after it copies that first file.

Any suggestions?

This conversation is currently closed to new comments.

5 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Answers

Collapse -

How about checking if the file already exists in the destination?

by robo_dev In reply to Copy newest file

IF EXIST ....

Or you could drop a placeholder file in the directory that sets a variable so your batch file knows it's been there.

You can also flip the archive-bit in the files...that's why it's there.

http://www.melbpc.org.au/pcupdate/9100/9107article7.htm

this batch file is from site above:

ECHO OFF
C:
CD\PCW
ED %1
:START
CLS
ECHO DO YOU WISH TO BACK UP THE *.TXT
ECHO AND *.DOC FILES ONTO DRIVE A:
QUESTION
IF NOT ERRORLEVEL 1 GOTO NOBACKUP
ATTRIB C:\PCW\*.TXT +A
ATTRIB C:\PCW\*.DOC +A
CLS
:BACKUP
ECHO IS THERE A DISC TO RECEIVE THE DATA IN DRIVE A:
QUESTION
IF NOT ERRORLEVEL 1 GOTO START
XCOPY C:\PCW\*.TXT A: /M
IF NOT ERRORLEVEL 1 GOTO DOC
GOTO BACKUP
:DOC
XCOPY C:\PCW\*.DOC A: /M
IF NOT ERRORLEVEL I GOTO NOBACKUP
GOTO BACKUP
:NOBACKUP
CLS
ECHO HAVE A NICE DAY, DAVE

Collapse -

Agree with robodev

by markp24 In reply to Copy newest file

Nice batch file Robo_dev!

Collapse -

Reponse To Answer

by robo_dev In reply to Agree with robodev

Ummm...I just (ahem) copied that from the web site listed, but if you're sending cash, I'll take it :)

Collapse -

Reponse To Answer

by markp24 In reply to Agree with robodev

LOL too funny

Back to Software Forum
5 total posts (Page 1 of 1)  

Software Forums