LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-08-2008, 10:07 AM   #1
Reda01
LQ Newbie
 
Registered: May 2008
Posts: 8

Rep: Reputation: 0
Batch script remove old files based on their names and keep on for each version


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 rocess %%A)

)
set errorlevel=0
goto :eof

rocess
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
=============================

Thank you very much in advance.
 
Old 05-08-2008, 11:41 AM   #2
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
The below script should do what you want.

THE SCRIPT WILL REMOVE FILES FROM YOUR FILESYSTEM!!!

I suggest you run it in a test directory first.

Code:
#!/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
HTH

Forrest

Last edited by forrestt; 05-08-2008 at 11:45 AM.
 
Old 05-08-2008, 11:45 AM   #3
Reda01
LQ Newbie
 
Registered: May 2008
Posts: 8

Original Poster
Rep: Reputation: 0
Hi forrestt,

Thank you for your reply, well done. However I am trying to automate this task for Windows, I need to use VB, PowerShell, or batch to do it. Could you help, please.
 
Old 05-08-2008, 11:56 AM   #4
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
Sorry. I just assumed that since this was posted in a Linux forum on a Linux Community website that it was a question of how to do it in Linux. Unfortunately my Windows programming days are so long ago and so short lived that I really can't be of much help. The above script WILL work on Windows if you are able to install Cygwin on your system. Other than that you can ask one of the moderators to move this into the Programming forum on this site. You might get better luck there.

HTH

Forrest
 
Old 05-08-2008, 12:02 PM   #5
Reda01
LQ Newbie
 
Registered: May 2008
Posts: 8

Original Poster
Rep: Reputation: 0
Ok, I'll do, Thanks for your help.
 
Old 05-08-2008, 02:36 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Moved: This thread is more suitable in <PROGRAMMING> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 05-08-2008, 03:35 PM   #7
Reda01
LQ Newbie
 
Registered: May 2008
Posts: 8

Original Poster
Rep: Reputation: 0
Hi Guys,

Any ideas ?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script to remove old files based on date WindozBytes Linux - General 12 06-04-2012 02:21 AM
Batch Script to rename files... jamie_barrow Linux - Newbie 16 06-14-2009 02:26 PM
HELP! Script to RENAME/REMOVE ( ) From File NAMES!! xberetta21 Linux - Newbie 4 01-29-2008 02:10 PM
Help with script to batch edit text files OnoTadaki Programming 5 10-15-2007 03:44 PM
Got a script to rename a batch of files? jamie_barrow Linux - General 1 08-08-2003 07:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration