get only last file of same type

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
darioit
Posts: 230
Joined: 02 Aug 2010 05:25

get only last file of same type

#1 Post by darioit » 27 Apr 2018 04:08

Hello everybody,
a new problem at least difficult for me

In a directory I have different file, I must add each one for same name at 3th position in a new file one and delete it after copy.
This is a script and it works fine

Code: Select all

for /F %%a in ('dir %folder%\*.DDDDDDDD.* /b/a-d/od') do type "%folder%\%%a" >> %OUTPUT%\DDDDDDDD && del /Q %folder%\%%a 
The problem is when I have more file with same name at 1th position, in this case I want copy only last file arrived and delete others same with 1st name

Example list file:
AAAAAAAA.BBBBBBBB.CCCCCCCC.03024
EEEEEEEE.BBBBBBBB.CCCCCCCC.02052
FFFFFFFF.BBBBBBBB.DDDDDDDD.00155
EEEEEEEE.BBBBBBBB.DDDDDDDD.13099
EEEEEEEE.BBBBBBBB.DDDDDDDD.13100
EEEEEEEE.BBBBBBBB.DDDDDDDD.13101
EEEEEEEE.BBBBBBBB.DDDDDDDD.13102

Example output file in %OUTPUT% folder, must contain only this FFFFFFFF.BBBBBBBB.DDDDDDDD.00155 and this file EEEEEEEE.BBBBBBBB.DDDDDDDD.13102 put in this file DDDDDDDD with type command

If I add

Code: Select all

t:c
to dir command I can select last file but of all file, not only same 1st type of file

Suggestion?
Thank you in advance

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: get only last file of same type

#2 Post by darioit » 30 Apr 2018 08:41

I wrote this code, but works only if a file are ordered before first dot.
How can I order first qualifier name before dot (EEEEEEEE)?

Code: Select all

@echo off
setlocal EnabledelayedExpansion

set file1=""
set file2=""

for /f "tokens=1,2,3,4 delims=." %%a in ('dir *.DDDDDDDD.* /b/a-d/o-d') do (
		set file1=%%a.%%b.%%c
		if !file1! equ !file2! del /q !file2!.%%d 
		set file2=!file1!
	)

goto:eof

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: get only last file of same type

#3 Post by aGerman » 30 Apr 2018 10:30

Has the fourth token always the highest number for the most recent file?

Steffen

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: get only last file of same type

#4 Post by darioit » 30 Apr 2018 12:14

Yes at 99,99% the last 5 digit are sequential and higher is last file received, maybe is easier find the last file using this value

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: get only last file of same type

#5 Post by aGerman » 30 Apr 2018 13:48

Give that a go

Code: Select all

@echo off &setlocal EnableDelayedExpansion
set "string=DDDDDDDD"

for /f "delims==" %%i in ('2^>nul set max_') do set "%%i="
for /f "delims=" %%i in ('dir /a-d /o-d /b ^| findstr /rbc:"[^\.][^\.]*\.[^\.][^\.]*\.%string%\.[0-9][0-9][0-9][0-9][0-9]"') do (
  for /f "tokens=1-4 delims=." %%j in ("%%i") do (
    if "%%m" gtr "!max_%%j_%%k!" set "max_%%j_%%k=%%m"
  )
)

for /f "tokens=2-4 delims=_=" %%i in ('2^>nul set max_') do (
  for /f "delims=" %%l in (
    'dir /a-d /o-d /b ^| findstr /rbc:"%%i\.%%j\.%string%\.[0-9][0-9][0-9][0-9][0-9]" ^| findstr /vbc:"%%i.%%j.%string%.%%k"'
  ) do ECHO del "%%l"
  ECHO type "%%i.%%j.%string%.%%k" ^>^>"%string%"
)
PAUSE
Whether or not the code works does highly depend on your real file names. If this code displays the right commands then remove the ECHOs and PAUSE and change ^>^> to >>.

Steffen

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: get only last file of same type

#6 Post by darioit » 01 May 2018 23:38

this is exactly what I had in mind, put each data in a array to get last file and, after read the array and delete or keep data,many thanks it works very very well !!

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: get only last file of same type

#7 Post by darioit » 02 May 2018 05:46

Just a quick question, what's is this

Code: Select all

^>^>"%string%" 


it type file content on screen not in a file destination

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: get only last file of same type

#8 Post by aGerman » 02 May 2018 09:44

Reading is fun :wink:
aGerman wrote:
30 Apr 2018 13:48
... and change ^>^> to >>.
Steffen

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: get only last file of same type

#9 Post by darioit » 03 May 2018 02:23

you're right, I miss it !!
Thank you

Post Reply