Question Locked

Copy newest file

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

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?

5 replies

by robo_dev

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

by robo_dev

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

This discussion is closed to new replies.