Solved

DOS Compare File Name

Posted on 2013-01-08
2
355 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 84

Expert Comment

by:oBdA
ID: 38755914
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 54

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 38756767
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

820 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