Prepend file with date and delete files based on their age

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 ?

 

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.

[quote quote=164097]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.

[/quote]
Must rename-item be used to name the files to being with ?

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

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

[quote quote=164109]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
[/quote]

Interesting, what does the pipe mean ?

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

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' } 

@js

get-childitem | f1 * is not working ?

fl as in format-list