powershell - Getting most recent objects in a folder excluding certain strings -
i have folders contain files, sake of question, named follows:
a-001.txt a-002.txt b-001.txt b-002.txt d-001.txt d-002.txt
now using powershell order these files top of list recent file in folder:
d-002.txt b-002.txt a-001.txt a-002.txt b-001.txt d-001.txt
edit: store top x
recent number of files variable. however, want ignore starts a
if have 1 begins a
in array still ensure end x
files recent. i.e. above, want end below if x 4
.
d-002.txt b-002.txt a-001.txt b-001.txt
this simple example, folders dealing contain 1000s of files - more complex naming conventions logic same. how can handle in powershell?
removing logic other sort-object
, select-object
criteria have addressed nowadays following.
get-childitem $somepath | select-object *,@{label="prefix";expression={(($_.name) -split "-",2)[0]}} | group-object prefix | foreach-object{ $_.group | select-object -first 1 -property fullname }
what happens here add together property output of get-childitem
called "prefix". now, criteria might more complicated given sample assumed files beingness grouped contents of name before first "-". take every file name , build prefix based on that. magic comes group-object
grouping items , select first one. in case newest x amount. allow me know if having problem integrating this.
aside grouping logic sorting not need exists before select-object
in our illustration above.
fyi other readers
there issues op's actual info since above code didnt work exactly. worked out in chat , using same logic able address ops concern. test info in question , reply work intended.
powershell
No comments:
Post a Comment