header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Delete files based on their names and dates,but keep the latest version
Last Post 10 May 2008 05:01 PM by Reda01. 8 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
Reda01User is Offline
New Member
New Member
Posts:19
Avatar

--
09 May 2008 03:01 PM  

Hello !

I wrote a bash 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

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 bash script below, it does the work fine, I am trying to do the Same thing in PowerShell for windows, Do you know the equivalent to the command UNIQ ?
Any idea how to do it in PS ? Thanks



Here is the bash script :
# 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

 

Any suggestions will be highly appreciated,

 

halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:334
Avatar

--
09 May 2008 04:11 PM  
here's your delimiter part:

dir | % { $_.name.split("-") }

uniq ~ select-object -unique
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
Reda01User is Offline
New Member
New Member
Posts:19
Avatar

--
09 May 2008 04:29 PM  
Thank you for your reply,

How compare the first part before the first "-" for each file and delete all and keep the latest version ?

I tried this commadn # dir *-*-*-*.* | % { $_.name.split("-") } | select first 1

But didin't do the job ?

Thanks
ShayUser is Offline
Basic Member
Basic Member
Posts:272
Avatar

--
10 May 2008 12:53 AM  

 

 

Try this:


dir a1-*-*-.*  | group {$_.name.split("-")[0]} | foreach {
 $_.group | sort lastWriteTime -desc | foreach {$file=0} { if ($file -gt 0) { remove-item $_.fullname -force -whatIf}; $file++}
}

 

If it works,  remove -whatIf

-Shay

Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
Reda01User is Offline
New Member
New Member
Posts:19
Avatar

--
10 May 2008 11:31 AM  
Hi,
I removed -WhatIf from the code, but no files have been deleted, nothing happened. I also wanted to list all the files having this name format *-*-*-*.* not only a1-*-*.*.*

dir *-*-*-.* | group {$_.name.split("-")Ύ]} | foreach {
$_.group | sort lastWriteTime -desc | foreach {$file=0} { if ($file -gt 0) { remove-item $_.fullname -force }; $file++}
}

Does the command "remove-item $_.fullname" remove really the files or we need something like remove-item $_.group[0] ?

Thank you.
ShayUser is Offline
Basic Member
Basic Member
Posts:272
Avatar

--
10 May 2008 02:34 PM  

 

Actually it should be:

dir *-*-*-.*  | group {$_.name.split("-")[0]]} | foreach {
 $_.group | sort lastWriteTime -desc | foreach {$file=0} { if ($file -gt 0) { remove-item $_.fullname -force -whatIf}; $file++}
}

This command groups all file names by their first characters (until the - sign). It sorts each group of files, collection, to have the last version at the top and then removes all files except the last updated file.

The "remove-item $.fullname" should delete the files.

Do you get any output generated by the -whatIf parameter. It should say which file would have been deleted.

 


---
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com

Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
Reda01User is Offline
New Member
New Member
Posts:19
Avatar

--
10 May 2008 03:15 PM  
Hi Shay,

I run again the command with and without -whatIf, I didn't get any output, and no files have been deleted ?

Your command looks fine, no errors, logic, ... just it doesn't do anything even I have this list when I run " dir *-*-*-*.* " :

-a--- 05.05.2008 23:36 3120479 a1-a2-a3-a4.log
-a--- 14.04.2008 23:14 3120473 a1-b1-c1-d1.log
-a--- 19.11.2007 16:09 218080 a1-b10-c10-d10.log
-a--- 14.04.2008 23:14 3120473 a1-b3-c3-d3.log
-a--- 14.04.2008 23:14 3120473 a1-b5-c5-d5.log
-a--- 10.11.2005 14:03 192 a1-b6-c6-d6.log
-a--- 15.10.2007 11:18 74 a1-b7-c7-d7.log
-a--- 25.01.2005 10:37 0 a1-b9-c9-d9.log
-a--- 12.12.2006 11:03 155 a2-b-c-d.log
-a--- 20.04.2008 20:20 682 a2-b11-c11-d11.log
-a--- 20.04.2008 20:26 696 a2-b12-c12-d12.log
-a--- 20.04.2008 21:56 814 a2-b13-c13-d13.log
-a--- 09.11.2005 16:54 4512 a2-b2-c2-d2.log
-a--- 15.10.2007 12:27 2 a2-b4-c4-d4.log
-a--- 15.10.2007 11:18 74 a2-b8-c8-d8.log
-ar-- 02.06.2004 12:30 49152 a3-b13-c1-d13.log
-a--- 26.12.2007 16:05 262144 a3-b13-c13-d13.log
-ar-- 05.08.2005 18:49 159744 a3-b13er-c13-d13.log
-ar-- 23.01.2003 22:29 15122 a4-b13-c13-13.log
-ar-- 05.08.2005 15:05 204800 a4-b13-c13-d13w.log
-ar-- 24.05.2003 00:46 15122 a4-b13-c13-gest.log
-ar-- 29.06.2004 00:56 45056 a5-b13-c13-d13.log
-ar-- 14.02.2005 18:55 422 a5-b13-c13-d131.log
-ar-- 05.09.2001 19:24 344923 a5-b13-c13-d133.log
-ar-- 07.10.2005 17:44 8439 a6-b13-c13-d13.log
-ar-- 07.10.2005 17:45 4088 a6-b13-c1333-d13.log
-ar-- 07.10.2005 17:49 22495 a6-Re-adm-eJ.log

Are we missing something in this command ?

Thank you.

ShayUser is Offline
Basic Member
Basic Member
Posts:272
Avatar

--
10 May 2008 04:10 PM  

Here is the output I get after grouping the files:

PS 43> dir *-*-*-*.* | group {$_.name.split("-")[0]}

Count Name Group
----- ---- -----
8 a1 {D:\scripts\temp\a1-a2-a3-a4.log, D...
7 a2 {D:\scripts\temp\a2-b-c-d.log, D:\s...
3 a3 {D:\scripts\temp\a3-b13-c1-d13.log,...
3 a4 {D:\scripts\temp\a4-b13-c13-13.log,...
3 a5 {D:\scripts\temp\a5-b13-c13-d13.log...
3 a6 {D:\scripts\temp\a6-b13-c13-d13.log...

And here is the output with the -whatIf parameter:

What if: Performing operation "Remove File" on Target "a1-b3-c3-d3.log".
What if: Performing operation "Remove File" on Target "a1-b5-c5-d5.log".
What if: Performing operation "Remove File" on Target "a1-b1-c1-d1.log".
What if: Performing operation "Remove File" on Target "a1-b10-c10-d10.log".
What if: Performing operation "Remove File" on Target "a1-b7-c7-d7.log".
What if: Performing operation "Remove File" on Target "a1-b6-c6-d6.log".
What if: Performing operation "Remove File" on Target "a1-b9-c9-d9.log".
What if: Performing operation "Remove File" on Target "a2-b12-c12-d12.log".
What if: Performing operation "Remove File" on Target "a2-b11-c11-d11.log".
What if: Performing operation "Remove File" on Target "a2-b4-c4-d4.log".
What if: Performing operation "Remove File" on Target "a2-b8-c8-d8.log".
What if: Performing operation "Remove File" on Target "a2-b-c-d.log".
What if: Performing operation "Remove File" on Target "a2-b2-c2-d2.log".
What if: Performing operation "Remove File" on Target "a3-b13er-c13-d13.log".
What if: Performing operation "Remove File" on Target "a3-b13-c1-d13.log".
What if: Performing operation "Remove File" on Target "a4-b13-c13-gest.log".
What if: Performing operation "Remove File" on Target "a4-b13-c13-13.log".
What if: Performing operation "Remove File" on Target "a5-b13-c13-d13.log".
What if: Performing operation "Remove File" on Target "a5-b13-c13-d133.log".
What if: Performing operation "Remove File" on Target "a6-b13-c1333-d13.log".
What if: Performing operation "Remove File" on Target "a6-b13-c13-d13.log".

he complete code:


dir *-*-*-*.* | group {$_.name.split("-")[0]} | foreach {
$_.group | sort lastWriteTime -desc | foreach {$file=0} { if ($file -gt 0) { remove-item $_.fullname -force -whatIf}; $file++}
}

For some reason the split() first item (e.g. [ 0 ]) is not displayed correctly (e.g. Ύ]])


---
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
Reda01User is Offline
New Member
New Member
Posts:19
Avatar

--
10 May 2008 05:01 PM  
Hi Shay,

Bravo. It works now.
Thank you very much for your help.
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer