From bash script to Power shell ? Delete files based on their names and dates,but keep the latest ve

Author Message
reda01

  • Total Posts : 9
  • Scores: 0
  • Reward points : 0
  • Joined: 5/8/2008
  • Status: offline
From bash script to Power shell ? Delete files based on their names and dates,but keep th Thursday, May 08, 2008 7:43 PM (permalink)
0
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
 
#1
    SAPIENScripter

    • Total Posts : 283
    • Scores: 2
    • Reward points : 0
    • Joined: 11/1/2006
    • Location: SAPIEN Technologies
    • Status: offline
    RE: From bash script to Power shell ? Delete files based on their names and dates,but kee Sunday, June 01, 2008 3:13 PM (permalink)
    0
    In PowerShell, everything is an object so you don't need to parse any text really. Here's an example.  It finds all ps1 files in the test directory that start with te, then the results are sorted by LastWriteTime and then PowerShell selects the first X number of files minus one which should be the latest file.

    PS C:\> dir c:\test\te*.ps1 -OutVariable temp | sort LastWriteTime | Select -first (($temp.count)-1) | del -whatif

    I added the -whatif parameter which is a great sanity check.
    Jeffery Hicks
    Windows PowerShell MVP
    SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com

    Follow Me: http://www.twitter.com/JeffHicks
     
    #2

      Online Bookmarks Sharing: Share/Bookmark

      Jump to:

      Current active users

      There are 0 members and 1 guests.

      Icon Legend and Permission

      • 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
      • Read Message
      • Post New Thread
      • Reply to message
      • Post New Poll
      • Submit Vote
      • Post reward post
      • Delete my own posts
      • Delete my own threads
      • Rate post

      2000-2012 ASPPlayground.NET Forum Version 3.9