how to pick up the duplicated files only from the greatest versions

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

how to pick up the duplicated files only from the greatest versions

#1 Post by goodywp » 13 Nov 2020 05:42

Hi all,

I have a task to pickup the duplicated files only from the greatest versions as below
From the BEFORE list, these two files, 500298010103.S3S.mockup, 500299010103.S3S.mockup occurred in the following versions folders, 0100, 0101, and 0102 ( I used italic to marked it out)

BEFORE
\T501-03568-0100\500298010103.S3S.mockup
\T501-03568-0100\500299010103.S3S.mockup
\T501-03568-0100\500300010103.S3S.mockup
\T501-03568-0100\500302010101.S3S.mockup
\T501-03568-0101\500298010103.S3S.mockup
\T501-03568-0101\500299010103.S3S.mockup
\T501-03568-0101\500300010104.S3S.mockup
\T501-03568-0101\500302010102.S3S.mockup
\T501-03568-0102\500298010103.S3S.mockup
\T501-03568-0102\500299010103.S3S.mockup
\T501-03568-0102\500300010105.S3S.mockup
\T501-03568-0102\500302010103.S3S.mockup


----------------------------------------------------------------------------------
AFTER pickup, only the greatest version of these two files have been picked up, like this case 0120, been picked up as below:

\T501-03568-0100\500300010103.S3S.mockup
\T501-03568-0100\500302010101.S3S.mockup
\T501-03568-0101\500300010104.S3S.mockup
\T501-03568-0101\500302010102.S3S.mockup
\T501-03568-0102\500298010103.S3S.mockup
\T501-03568-0102\500299010103.S3S.mockup
\T501-03568-0102\500300010105.S3S.mockup
\T501-03568-0102\500302010103.S3S.mockup

Another thing is that we do not know which two or three.. files are duplicated in the different versions, so the above is just a example, we can not hard code these two files in the scripts

Thanks

Eureka!
Posts: 136
Joined: 25 Jul 2019 18:25

Re: how to pick up the duplicated files only from the greatest versions

#2 Post by Eureka! » 13 Nov 2020 19:15

Assuming the structure of the part before the "\" is always the same (\Tnnn-nnnnn-nnnn\) :
(and assuming your list is in mockup.lst)

Code: Select all

@echo off
setlocal enabledelayedexpansion

for /f "usebackq delims=" %%I in (`sort mockup.lst ^| sort /R /+17`) do (

	if /i "!PREVIOUS!" NEQ "%%~nI" (
		echo %%I>> after.txt
		set "PREVIOUS=%%~nI"
	)
)

Output:

Code: Select all

\T501-03568-0102\500302010103.S3S.mockup
\T501-03568-0101\500302010102.S3S.mockup
\T501-03568-0100\500302010101.S3S.mockup
\T501-03568-0102\500300010105.S3S.mockup
\T501-03568-0101\500300010104.S3S.mockup
\T501-03568-0100\500300010103.S3S.mockup
\T501-03568-0102\500299010103.S3S.mockup
\T501-03568-0102\500298010103.S3S.mockup
Last edited by Eureka! on 14 Nov 2020 11:54, edited 1 time in total.

goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

Re: how to pick up the duplicated files only from the greatest versions

#3 Post by goodywp » 14 Nov 2020 08:47

Thanks Eureka!

I tried it and is working but not the greatest version, the least version instead. The results is showing that all duplicated files have been removed but not the greatest version as expected.

\T501-03568-0100\500298010103.S3S.mockup
\T501-03568-0100\500299010103.S3S.mockup
\T501-03568-0100\500300010103.S3S.mockup
\T501-03568-0101\500300010104.S3S.mockup
\T501-03568-0102\500300010105.S3S.mockup
\T501-03568-0100\500302010101.S3S.mockup
\T501-03568-0101\500302010102.S3S.mockup
\T501-03568-0102\500302010103.S3S.mockup

The expected results should looks like this

\T501-03568-0102\500298010103.S3S.mockup
\T501-03568-0102\500299010103.S3S.mockup
\T501-03568-0100\500300010103.S3S.mockup
\T501-03568-0101\500300010104.S3S.mockup
\T501-03568-0102\500300010105.S3S.mockup
\T501-03568-0100\500302010101.S3S.mockup
\T501-03568-0101\500302010102.S3S.mockup
\T501-03568-0102\500302010103.S3S.mockup

Eureka!
Posts: 136
Joined: 25 Jul 2019 18:25

Re: how to pick up the duplicated files only from the greatest versions

#4 Post by Eureka! » 14 Nov 2020 11:57

My bad! I accidentally added the /R option to the wrong sort (my original version worked fine).
Code and output above are updated.

goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

Re: how to pick up the duplicated files only from the greatest versions

#5 Post by goodywp » 15 Nov 2020 09:22

Thanks again Eureka! Yes worked as expected! Appreciated!

Just a quick question to you.
tasklist | sort (This will sort the list items in ascending order) This will give me the least version.
tasklist | sort /R (This will sort the list items in descending order) This will give me the greatest version.

/+n
Specifies the character number, n, to
begin each comparison. /+3 indicates that
each comparison should begin at the 3rd
character in each line. Lines with fewer
than n characters collate before other lines.
By default comparisons start at the first
character in each line.
So this /+17
\T501-03568-0100\, so this is the part of string to be sorted it out.

Post Reply