Score:0

Need help in Moving files to the backup directory except the last 10 files based on file NAME not date modified

gb flag


I have a situation where a server is creating journal files (MJ00001-MJ000x) and I want to move all the files to a backup directory but need to keep the last 10 in there to get the archive. I looked at robocopy and Powershell Move-Item but the problem is the date modified is always the current day as the server looks to them if they are in the directory (hence I can't use move keeping last 10 based on date modified). I would prefer a powershell method that can look at the filename and move all except the 10 highest numbered file as I can put that into a script to stop the server service and then restart after move.

Example file structure below:
Located in C:\Folder
mainfile.ext
MJ00001.ext
MJ00002.ext
MJ00003.ext
through to MJ00257.ext

In this example I want to move MJ0001.ext - MJ00247.ext to D:\Other_Folder\

Note that I want to keep the mainfile.ext in the directory and just the highest named MJ files.

Can anyone please help?

Score:0
in flag

That's actually pretty easy using the -SkipLast argument of Select-Object:

Get-ChildItem -Path "C:\Folder" -Exclude "mainfile.ext" | Sort-Object |
    Select-Object -SkipLast 10 |Move-Item -Destination "D:\Other_Folder\"
gb flag
I'll give that a go now and upvote if all works. Thanks.
gb flag
It doesn't seem to exclude the mainfile.ext file and moves that to the destination directory as well.
in flag
Then you have a typo somewhere. I tested this before posting it.
gb flag
Here is what i have tried `Get-Item "C:\temp\Source" | Get-ChildItem -Exclude "mainfile.txt" | Sort-Object | Select-Object -SkipLast 10 |Move-Item -Destination "C:\temp\Destination\" ` When i do a dir of c:\temp\source i have MJ000012.txt-MJ00021 (the last 10 files) but the original mainfile.txt is moved out as well.
gb flag
it's like exclude is not working. when i type the command section by section and tell it to `Get-Item "C:\temp\Source" | Get-ChildItem` i get all files in folder show up. then when i add -Exclude "mainfile.txt" it still shows the same output (i would expect mainfile.txt to not show in output)
in flag
Funny, now I get the same result as you. I could have sworn that did not happen yesterday. However, please try my modification. This definitely works for me.
gb flag
we came to teh same conclusion at the same time .. Get-Childitem -Path fixed it for me ... `Get-ChildItem -Path C:\temp\source -Exclude "mainfile.txt" | Sort-Object | Select-Object -SkipLast 10 |Move-Item -Destination C:\temp\Destination`
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.