Solved

DOS Compare File Name

Posted on 2013-01-08
2
357 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Powershell Exchange mailboxsizes 3 57
xcopy using a text file to a folder 13 75
Sleep one hour while in loop 4 43
bash parse log file and split according to date 5 29
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

739 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