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?
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
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
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
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
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
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
"Larry__Weiss" <l...@airmail.net> wrote in message
news:ewEB7OK...@TK2MSFTNGP05.phx.gbl...