George Samuel Raju
George Samuel Raju

Reputation: 11

Batch Script: Identifying the latest file with incrementing file extension

Can someone help me with handling the specific scenario explained below using windows batch script.

We have a shared folder, which is used by a third party application tool (OBIEE) as a version controlled folder. In this folder the tool creates backup files with incrementing extension numbers.

For example: If we have a code file Projects.rpd, the first time we checkout this file, the tool creates Projects.000 as a backup file. The next time it will be Projects.001, then Projects.002 and so on.

In the batch script, I need to get the file name of the latest backup file. In this example, Projects.002

I cannot go for the file with the latest timestamp, since the latest backup (Projects.002), the latest code file (Projects.rpd) and another control file will always have the same timestamp.

How can I identify the latest backup file in this scenario?

Upvotes: 1

Views: 205

Answers (2)

Aacini
Aacini

Reputation: 67216

Just insert this line in your Batch file:

for /L %%i in (0,1,9) do for %%a in (Projects.%%i*) do set "latestBackup=%%a"

Upvotes: 0

Magoo
Magoo

Reputation: 80033

@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "latest="
FOR /f %%a IN ('dir /b /a-d /on "%sourcedir%\projects.*"') DO (
 IF /i "%%~xa" leq ".A" SET "latest=%%~xa"
)
ECHO %latest%
SET "latest=%latest:~1%"
ECHO latest is "%latest%"
GOTO :EOF

You would need to change the setting of sourcedir to suit your circumstances.

It would have been really helpful to post the name of the "other control file." I'm forced to assume it's called report.somethingelse where somethingelse is alphabetic.

Upvotes: 1

Related Questions