![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Want to use remove-item but save names to log file of files delete 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? |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Want to use remove-item but save names to log file of files delete 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 Maxer wrote: Quote: > 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? |
My System Specs![]() |
| | #3 (permalink) |
| | RE: Want to use remove-item but save names to log file of files delete 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 } "Maxer" wrote: Quote: > 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? |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Want to use remove-item but save names to log file of files delete But using Tee-Object would have the same concerns about the assumption that all files will be successfully deleted. - Larry PaulChavez wrote: Quote: > 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 > } > > "Maxer" wrote: > Quote: >> 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? |
My System Specs![]() |
| | #5 (permalink) |
| | RE: Want to use remove-item but save names to log file of files de 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" wrote: Quote: > 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 > } > > "Maxer" wrote: > Quote: > > 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? |
My System Specs![]() |
| | #6 (permalink) |
| | RE: Want to use remove-item but save names to log file of files de Yes, I realized after submission that this won't audit what was actually removed, just what was attempted. "RichS [MVP]" wrote: Quote: > 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 > -- |
My System Specs![]() |
| | #7 (permalink) |
| | Re: Want to use remove-item but save names to log file of files delete 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" <Maxer@newsgroup> wrote in message news:42ECBA18-0A6B-4DA1-8CC8-535A4391A5E8@newsgroup Quote: > 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? |
My System Specs![]() |
| | #8 (permalink) |
| | Re: Want to use remove-item but save names to log file of files delete Did my post detailing that option get posted? - Larry Martin Zugec wrote: Quote: > 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" <Maxer@newsgroup> wrote in message > news:42ECBA18-0A6B-4DA1-8CC8-535A4391A5E8@newsgroup Quote: >> 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? |
My System Specs![]() |
| | #9 (permalink) |
| | Re: Want to use remove-item but save names to log file of files delete Never mind, I see it at Want to use remove-item but save names to log file of files delete 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: Quote: > Did my post detailing that option get posted? > > Martin Zugec wrote: Quote: >> 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 >> >> >> "Maxer" <Maxer@newsgroup> wrote in message >> news:42ECBA18-0A6B-4DA1-8CC8-535A4391A5E8@newsgroup Quote: >>> 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? |
My System Specs![]() |
| | #10 (permalink) |
| | Re: Want to use remove-item but save names to log file of files delete Ooops, sorry, I completely missed that post We were thinking the same way![]() Martin "Larry__Weiss" <lfw@newsgroup> wrote in message news:ewEB7OKYKHA.844@newsgroup Quote: > Did my post detailing that option get posted? > > - Larry > > > Martin Zugec wrote: Quote: >> 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" <Maxer@newsgroup> wrote in message >> news:42ECBA18-0A6B-4DA1-8CC8-535A4391A5E8@newsgroup Quote: >>> 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? |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Delete files based on dup names | PowerShell | |||
delete uninstalled item from programs-file | General Discussion | |||
| unable to delete Names of files after deleted files. | Vista mail | |||
| How delete previous downloaded file names | Vista file management | |||
| old files won't delete, says item not found...any ideas? | Vista file management | |||