Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Delete files having the same prefix but keep one

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,59998
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Delete files having the same prefix but keep one
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Delete files having the same prefix but keep one - 5/8/2008 3:20:53 AM   
  reda01

 

Posts: 3
Score: 0
Joined: 5/8/2008
Status: offline
Hello !

I wrote a batch script to to be run into a folder where I have many log files generated automatically but they have these name format : *-*-*-*.*
I would like to delete all files having the SAME first delimiter ex a1-*-*-.* BUT keep the latest version (the most recent file). Example, I could find in my folder these files :

a1-dd-ff-rr.log
a1-ff-rr-ww.log
a1-87-43-ws.log
a1-re-ee-ww.log

a2-ww-11-lk.log
a2-uz-21-ws.log
.
.
.


q-wq-we-tf.*
q-ew-ws-we.*
..
.

po-we-we-ed.log
po-87-pl-lo.log
po-ws-ed-rf.log
po-ws-aa-qq.log

So the idea was to filter first the beginning of each file and put them in a list, sort that list by date, delete all but keep the most recent one. In the batch script below, it does the work but only for the first category (a1) it doesn’t loop the second (a2), the third (q),,,,,, here is the script :

==================
@echo off
setlocal enabledelayedexpansion

set Repertoire=c:\test
set NombreDeFichiersALaisser=2

if not exist "%Repertoire%" (
echo Le repertoire %Repertoire% n''existe pas
echo Mettez à jour le script et réssayer
echo.
echo Appuyer sur une touche pour continuer ....
pause>nul
)

cd /d "%Repertoire%"
set compteur=0
FOR /f "tokens=1,2,3,4 delims=-" %%I in ('dir /b *-*-*-*.*') do call :Imprimer %%I
call :SupprimerDoublons RecupEnTete.txt

:Imprimer
echo %1>>RecupEnTete.txt
rem del %1 /f >nul
exit /b



:SupprimerDoublons
@echo off > SortieSansDoublons.txt
if %1'==' echo which file? && goto :eof
if not exist %1 echo %1 not found && goto :eof
for /f "tokens=* delims= " %%a in (%1) do (
find "%%a" < SortieSansDoublons.txt > nul
if errorlevel 1 echo %%a>>SortieSansDoublons.txt
)

@echo off
for /f %%I in (SortieSansDoublons.txt) do (
for %%A in (%%I-*-*-*.*) do (call process %%A)

)
set errorlevel=0
goto :eof

process
set /a compteur+=1
if %compteur% leq %NombreDeFichiersALaisser% exit /b
echo %1>>FichiersSupprimesLe%date:~0,2%%date:~3,2%%date:~6,4%.txt
rem del %1 /f >nul
exit /b

==========================

I was able to do in bash script :

#!/bin/bash
# get a list of prefixes by splitting the filenames on the "-" and making that a list of unique entries
for PREFIX in `ls | awk -F- '{print $1}' | uniq` ; do
# Loop through the prefixes and while we have more than one of that type remove the one with the oldest timestamp
while [ `ls $PREFIX-*|wc -l` -gt 1 ] ; do
rm `ls -tr $PREFIX-* | head -1`
done
done
------------

Would it be possible to do it in VBScript or any windows language ? Or help me to correct my batch script,

Thank you,

Reda
 
 
Post #: 1
 
 RE: Delete files having the same prefix but keep one - 5/8/2008 4:57:11 AM   
  ebgreen


Posts: 4408
Score: 29
Joined: 7/12/2005
Status: offline
You could do it in VBScript. You could also do it in batch but it would be more convoluted. If you want to get Windows Powershell, it would be the closest to the bash solution you have.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to reda01)
 
 
Post #: 2
 
 RE: Delete files having the same prefix but keep one - 5/8/2008 6:11:14 AM   
  reda01

 

Posts: 3
Score: 0
Joined: 5/8/2008
Status: offline
Hi ebgreen,
Thanks for your reply, would you have a similar example in VBScript ? I had a look to do it in PowerShell, but there is no equivalent to uniq command,... and the batch script I posted is closer to the solution, just I don't understand why this :

"
for /f %%I in (SortieSansDoublons.txt) do (
for %%A in (%%I-*-*-*.*) do (call process %%A)

"

Doesn't do the right job for all the files, it stopped when the first prefix is sorted,.. and it does delete all the other remaining files ??

Any ideas ?

Thanks,

Reda

(in reply to ebgreen)
 
 
Post #: 3
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Delete files having the same prefix but keep one Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts