Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Want to use remove-item but save names to log file of files delete

39 views
Skip to first unread message

Maxer

unread,
Nov 6, 2009, 11:10:12 AM11/6/09
to
So I want to search a directory, locate all files except those in the exclude
statement, and then remove them.

HOWEVER, I want to write a list of the files removed to a log file. (the
log file is the file being excluded).

IDEALLY this log file would be CSV so I could come back 6 months from now,
open it in Excel and sort and filter to get an idea of what was deleted.

(The is to delete files that are not removed by an app running on IIS, it
usually deletes its junk files, but sometimes not so every 24 hours I'm
purging stuff that's older than 24 hours, but I want to log what I purge to
pass it to the devs so they can see how much stuff their app is missing).


So... how can I do this?

Here is what I have now:
-----------------------
function Remove-OldFiles($folder, $logFile, $filter, $age)
{
Get-ChildItem $folder* -Include $filter -Exclude $logFile |
? { $_.LastWriteTime -le (Get-Date).AddMinutes(-$age) } |Remove-Item
-WhatIf
}
-----------------------

I am using minutes for testing purposes.

Anyway, how can I grab that list of deleted items and write it to a log?

Do I need to collect it into a variable FIRST, and then delete and then
write that same var to a log?

If I do that though, what if the delete fails, say the file is opened, etc...?

So ideally I'd want to hook onto the remove-item statement itself and use
its output.

Also, my ideal format for the csv log would be:

CurrentDateTime, FileName, FolderName, FilesLastWriteTime

So I'm thinking I'll have to use a forloop to construct this?
Then build each line as it goes?

Suggestions?

RichS [MVP]

unread,
Nov 6, 2009, 12:36:01 PM11/6/09
to
Capturing this output proved awkward as it is written directly to the screen
rather than being output on the pipeline.

One idea that did work was to capture the output as a transcription

start-transcript -Path c:\test\log.txt -Append
Get-ChildItem -Path c:\test -Exclude log.txt | Remove-Item -WhatIf
Get-ChildItem -Path c:\test -Exclude log.txt | Remove-Item -verbose
stop-transcript

You can always use a different file and then reformat into your log file as
required

--
Richard Siddaway
All scripts are supplied "as is" and with no warranty
PowerShell MVP
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk

Larry__Weiss

unread,
Nov 6, 2009, 1:12:46 PM11/6/09
to

You could save a Get-ChildItem list before and after the deletes and save off
the difference. Here's a simplified example (you would substitute your removing
logic and the exact appended text to $logfile could be customized from the
output of compare-object to suit your preferences)

PS C:> dir >xo # just to make sure file xo exists for this example
PS C:> $logfile = "mylogfile.txt" # just for my example

PS C:> $before = Get-ChildItem # before
PS C:> remove-Item xo # substitute your own logic to remove files
PS C:> $after = Get-ChildItem # after
PS C:> compare-object $before $after | ? { $_.SideIndicator -eq "<=" } | %
{$_.InputObject} >>$logfile # substitute your own text to append to $logfile

PS C:> gc $logfile

Directory: C:\Documents and Settings\Larry\My Documents\WindowsPowerShell

Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 11/6/2009 11:53 AM 18968 xo


- Larry

PaulChavez

unread,
Nov 6, 2009, 1:56:02 PM11/6/09
to
You can use Tee-Object. here's an example using your function:

function Remove-OldFiles($folder, $logFile, $filter, $age, $deletelog)


{
Get-ChildItem $folder* -Include $filter -Exclude $logFile |
? { $_.LastWriteTime -le (Get-Date).AddMinutes(-$age) } |

Tee-Object -var filesToDelete | Remove-Item -WhatIf

$filesToDelete |
select CurrentDateTime, FileName, FolderName, FilesLastWriteTime |
export-csv $deletelog

Larry__Weiss

unread,
Nov 6, 2009, 2:40:43 PM11/6/09
to
But using Tee-Object would have the same concerns about the assumption that all
files will be successfully deleted.

- Larry

RichS [MVP]

unread,
Nov 6, 2009, 3:21:02 PM11/6/09
to
Paul

This records the files that are passed by the where filter not the files
that are actually deleted. Remove-Item won't delete a file if its in use by
another process for instance even though it shows up in the listing.

The sucesses and failures need to be recorded I think so that it is known
what actually happens


--
Richard Siddaway
All scripts are supplied "as is" and with no warranty
PowerShell MVP
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk

PaulChavez

unread,
Nov 6, 2009, 3:26:02 PM11/6/09
to
Yes, I realized after submission that this won't audit what was actually
removed, just what was attempted.

Martin Zugec

unread,
Nov 8, 2009, 8:55:51 AM11/8/09
to

Hi Maxer,

just a quick idea. What you could do is to use Compare-Object:
[Array]$Before = Get-ChildItem -Recurse -Path "..."
... # Delete files\folders here
[Array]$After = Get-ChildItem -Recurse -Path "..."
$Diff = Compare-Object $Before $After

Martin

"Maxer" <Ma...@discussions.microsoft.com> wrote in message
news:42ECBA18-0A6B-4DA1...@microsoft.com...

Larry__Weiss

unread,
Nov 8, 2009, 1:46:01 PM11/8/09
to
Did my post detailing that option get posted?

- Larry

Larry__Weiss

unread,
Nov 8, 2009, 2:19:44 PM11/8/09
to
Never mind, I see it at
http://www.vistax64.com/powershell/259250-want-use-remove-item-but-save-names-log-file-files-delete.html

It turns out that neither your code or mine account for the possibility of an
empty set from Get-ChildItem, but I think the basic idea is sound.

- Larry


Larry__Weiss wrote:
> Did my post detailing that option get posted?
>

> Martin Zugec wrote:
>> Hi Maxer,
>>
>> just a quick idea. What you could do is to use Compare-Object:
>> [Array]$Before = Get-ChildItem -Recurse -Path "..."
>> ... # Delete files\folders here
>> [Array]$After = Get-ChildItem -Recurse -Path "..."
>> $Diff = Compare-Object $Before $After
>>
>>

Martin Zugec

unread,
Nov 8, 2009, 2:48:02 PM11/8/09
to

Ooops, sorry, I completely missed that post :( We were thinking the same way
;)

Martin

"Larry__Weiss" <l...@airmail.net> wrote in message
news:ewEB7OK...@TK2MSFTNGP05.phx.gbl...

0 new messages