Jump to content
Strawberry Orange Banana Lime Leaf Slate Sky Blueberry Grape Watermelon Chocolate Marble
Strawberry Orange Banana Lime Leaf Slate Sky Blueberry Grape Watermelon Chocolate Marble

MSFN is made available via donations, subscriptions and advertising revenue. The use of ad-blocking software hurts the site. Please disable ad-blocking software or set an exception for MSFN. Alternatively, register and become a site sponsor/subscriber and ads will be disabled automatically. 



Sign in to follow this  
sweept

Find newest file

Recommended Posts

sweept    0

Hi, I'm looking for a way to process two or more files that will SET (pick up) the newest file

for %%# in (%*) do set newest |@copy newest ..(command)

Example between: %~dpn1.log and a named one %~dp1Default.log

If neither Exist abort command..

Share this post


Link to post
Share on other sites

Guest   
Guest

Perhaps something like this...?

@ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR /F "delims=" %%# IN ('DIR /A-D /B /OD "*.log" 2^>NUL') DO SET "sNewest=%%#"

Edited by 5eraph

Share this post


Link to post
Share on other sites
Yzöwl    30

We need a little more information about the files you are using.

5eraph has assumed you are using the newest file of every file of a certain type in the same directory.

If you were copying that file then the following would probably suffice

XCOPY *.log X:\DestinationDir /D /Y

Share this post


Link to post
Share on other sites
Guest   
Guest

I made the assumption based on sweept's consistent references to %1; this indicates the files are in the same folder. Continuing on the logic, since I have a bit more time now...

@ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR /F "delims=" %%# IN ('DIR /A-D /B /OD "%~dp1*.log" 2^>NUL') DO SET "sNewest=%%#"
IF EXIST "%~dp1Default.log" (
IF /I "!sNewest!"=="%~n1.log" (
COPY "%~dpnx1"
(command)
) )

I have made two further assumptions:

  1. The desired code is either a packaged function or standalone script that takes only one parameter (%1).
  2. "%~dpn1.log" has the same timestamp as "%1".

Edited by 5eraph

Share this post


Link to post
Share on other sites
sweept    0

As to newest I meant Newer ;) soo Sorry I need to check my vocabulary sometime!!!

The subroutine needs to pick\Find which one is newer by the file's time-stamp. Setting the latest one for the command ..

Share this post


Link to post
Share on other sites
Guest   
Guest

Will there be only two files to compare?

How do "%~dpn1.log" and "%~dp1Default.log" fit into the picture?

  • Will there be a "%2"?
  • How are parameters (%1, %2, etc.) passed to the script: "drag-and-drop," or passed from another script?

Will the files to compare always be in the same directory?

Which command? As Yzöwl stated previously, a simple method may exist through that command.

Edited by 5eraph

Share this post


Link to post
Share on other sites
Yzöwl    30
The subroutine needs to pick\Find which one is newer by the file's time-stamp. Setting the latest one for the command ..

I think this answers a few questions!

You have created your own secret batch file which calls a subroutine, written by us, which is passed two parameters. The subroutine must set a variable or return for the most recently modified/accessed/created, (we haven't been told), in order that your secret batch file can continue on its own way without bothering us!

Just post the batch file you have and let us play with it as necessary!

Share this post


Link to post
Share on other sites
sweept    0

Thanks 5eraph I found what I needed now!

at first glance I couldn't figure whats achieved using a dir command ..That /od switch (cool)

that was one great vast example

Thanks again!!! & to youYzöwl &members viewing thanks for you's patience :)

Edited by sweept

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×