Solved

DOS Compare File Name

Posted on 2013-01-08
2
351 Views
Last Modified: 2013-04-26
Hello,

Can someone help me,  

I’m having 2  files named the same way but having  the  date stamp and second stamp at the end ( YYYY-MM-DD HH-MM-SS)

Exemple : PDF Flash Color Daily Sales Flash2013-01-08 08.32.41.326.pdf  
    PDF Flash Color Daily Sales Flash2013-01-08 08.45.41.326.pdf



And I would like to ensure to copy only the most resent one. How can I do that ?

This need to be done in batch.

Thanks a lot
0
Comment
Question by:youpa
2 Comments
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
Since you have a sensible naming convention (that is, yyyymmdd instead of mmddyyyy), you can just use a regular directory sorting and pick the last one. The script below is in test mode and will only display the copy command; remove the uppercase ECHO in front of "copy" to run it for real:
@echo off
setlocal
set SourceFolder=C:\Source
set TargetFolder=C:\Target
set Prefix=PDF Flash Color Daily Sales Flash
for /f "delims=" %%a in ('dir /b /o:n "%SourceFolder%\%Prefix%*.pdf"') do set LatestFile=%%a
echo Latest file: %LatestFile%
ECHO copy "%SourceFolder%\%LatestFile%" "%TargetFolder%"

Open in new window

If the actual time stamps of the files are correct, you could use those, too, by changing "/o:n" to "/o:d" in line 6.
0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
Comment Utility
Here's a slightly different variation, that sorts the matching files in reverse order so that once it copies the latest one it leaves the loop. Small gain in performance, and if there really are just two files doesn't matter a lot, but wanted to share it just as a slight alternate approach.

@echo off
setlocal

set BaseDir=C:\Source
set DestDir=C:\Target
set Filename=PDF Flash Color Daily Sales Flash*.pdf

for /F "tokens=*" %%F in ('dir /B /O-N "%BaseDir%\%Filename%"') do (
  copy "%BaseDir%\%%~F" "%DestDir%"
  goto :CopyDone
)
:CopyDone

Open in new window

~bp
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now