Prepend file with date and delete files based on their age

Welcome Forums General PowerShell Q&A Prepend file with date and delete files based on their age

This topic contains 7 replies, has 2 voices, and was last updated by

js
 
Participant
2 months, 1 week ago.

  • Author
    Posts
  • #164079

    Participant
    Topics: 6
    Replies: 22
    Points: 78
    Rank: Member

    I need a little help, I want to append at the beginning of a file the date as such in square brackets [mm/dd/year] then whatever file name, in this case the program appends to the date.  Second purge a directory for the last X, X being how many files I want to keep based on the date which was appended to the beginning of the file as mentioned in square brackets.

    What commands and so forth must I look into for this ?

     

  • #164097
    js

    Participant
    Topics: 25
    Replies: 678
    Points: 1,629
    Helping Hand
    Rank: Community Hero

    You can use rename-item to rename the files. Square brackets can be hard to deal with, because in powershell they can be wildcards. Actually if you date them "yyyymmdd', it should be easier to sort them and delete the ones you want. Get-childitem, sort-object, remove-item. Or you can just delete by the lastwritetime fileinfo property, and not bother renaming them.

  • #164106

    Participant
    Topics: 6
    Replies: 22
    Points: 78
    Rank: Member

    You can use rename-item to rename the files. Square brackets can be hard to deal with, because in powershell they can be wildcards. Actually if you date them "yyyymmdd', it should be easier to sort them and delete the ones you want. Get-childitem, sort-object, remove-item. Or you can just delete by the lastwritetime fileinfo property, and not bother renaming them.

    Must rename-item be used to name the files to being with ?

  • #164109
    js

    Participant
    Topics: 25
    Replies: 678
    Points: 1,629
    Helping Hand
    Rank: Community Hero

    No. And do you know you can list recent files like this?

    get-childitem | where lastwritetime -gt '7/1/2019'
    
    
    
    		
    	
  • #165760

    Participant
    Topics: 6
    Replies: 22
    Points: 78
    Rank: Member

    No. And do you know you can list recent files like this?

     

    1
    2
    get-childitem | where lastwritetime -gt '7/1/2019'
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

     

    1
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    Interesting, what does the pipe mean ?

    I was hoping this would work Get-ChildItem -Path C:\3 ! -gt "7/3/2019"

  • #165793
    js

    Participant
    Topics: 25
    Replies: 678
    Points: 1,629
    Helping Hand
    Rank: Community Hero
    get-childitem | fl *
    

    Will show you all the properties. One of them is lastwritetime, which is a [datetime] type. So you can pipe the object to where-object and compare it with a string that can be converted to a [datetime].

    Another way to write it is this:

    get-childitem | where-object { $_.lastwritetime -gt [datetime]'1/1/2019' } 
    
  • #165892

    Participant
    Topics: 6
    Replies: 22
    Points: 78
    Rank: Member

    @js

    get-childitem | f1 * is not working ?

    • #165898
      js

      Participant
      Topics: 25
      Replies: 678
      Points: 1,629
      Helping Hand
      Rank: Community Hero

      fl as in format-list

You must be logged in to reply to this topic.