Page 1 of 1

get only last file of same type

Posted: 27 Apr 2018 04:08
by darioit
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

Re: get only last file of same type

Posted: 30 Apr 2018 08:41
by darioit
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

Re: get only last file of same type

Posted: 30 Apr 2018 10:30
by aGerman
Has the fourth token always the highest number for the most recent file?

Steffen

Re: get only last file of same type

Posted: 30 Apr 2018 12:14
by darioit
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

Re: get only last file of same type

Posted: 30 Apr 2018 13:48
by aGerman
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

Re: get only last file of same type

Posted: 01 May 2018 23:38
by darioit
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 !!

Re: get only last file of same type

Posted: 02 May 2018 05:46
by darioit
Just a quick question, what's is this

Code: Select all

^>^>"%string%" 


it type file content on screen not in a file destination

Re: get only last file of same type

Posted: 02 May 2018 09:44
by aGerman
Reading is fun :wink:
aGerman wrote:
30 Apr 2018 13:48
... and change ^>^> to >>.
Steffen

Re: get only last file of same type

Posted: 03 May 2018 02:23
by darioit
you're right, I miss it !!
Thank you