Jump to content

Powershell or .Bat script

Hi

 

I am looking to write a script that will delete all files in a folder that has a date last modified older that a set file.

The best way to match this file is by it name as the date will change based on the last transaction it processes.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

36 minutes ago, mmcbj5 said:

Hi

 

I am looking to write a script that will delete all files in a folder that has a date last modified older that a set file.

The best way to match this file is by it name as the date will change based on the last transaction it processes.

 

 

Yes you can make a PS script.

Link to comment
Share on other sites

Link to post
Share on other sites

The question is bit vague and I want some more information: let's say you have file01.txt which is Feb 02 2016 modified and a file02.txt which is Feb 03. How would you know which one is the original one and which one is the older one you need to delete.

You said match by name but you haven't really elaborate on that part

 

 

You can sort files in a folder with this command (from newest to oldest, if you wish to do the opposite then just omit -descending at the end and replace *path* with actual path to it or if you're already in the folder then remove it)

get-childitem *path* | sort-object -property lastwritetime -descending

Then you can cycle through each file with either foreach or a loop.

 

Finally use remove-item to delete the file.

Link to comment
Share on other sites

Link to post
Share on other sites

sorry for being vague

What i mean is you have  files 

"Last_Seg.log" Date Modified "11-05-2017" this is the master Log.

Other logs "transaction12345.log" Date Modified "15-05-2017" 

and the file to delete "transaction123.log" Date modified "10-05-2017".

What is am looking to do is only delete logs older that the "Last_Seg.log" and keep all one that are newer.

Link to comment
Share on other sites

Link to post
Share on other sites

On 5/29/2017 at 9:01 PM, mmcbj5 said:

sorry for being vague

What i mean is you have  files 

"Last_Seg.log" Date Modified "11-05-2017" this is the master Log.

Other logs "transaction12345.log" Date Modified "15-05-2017" 

and the file to delete "transaction123.log" Date modified "10-05-2017".

What is am looking to do is only delete logs older that the "Last_Seg.log" and keep all one that are newer.

How experienced you are in PS script?

But anyway

 

$masterfile = (get-item C:\master.log).lastwritetime

Get-ChildItem -Path C:\*.log | Where-Object {
    $_.LastWriteTime -lt $masterfile
} | Remove-Item;

replace C:\master.log and C:\*log with actual path

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×