Using forfiles to delete files, Need report first


  1. Posts : 4
    Windows 10
       #1

    Using forfiles to delete files, Need report first


    I would like to use the following to delete file older thatn 180 days in a temp folder:

    ForFiles /p “C:\temp”/s /d -180 /c “cmd /c del /q @file”

    Before i run it i would like to see a report showing the files that will be deleted and total space freed up.

    Any thoughts? Maybe easier to do this in powershell?

    All help appeciated!!

    Thank you!
      My Computer


  2. Posts : 9,792
    Mac OS Catalina
       #2

    Just use File Explorer.
      My Computer


  3. Posts : 4
    Windows 10
    Thread Starter
       #3

    Not that simple. the temp folder houses a couple TB of data, and this process will evetully be automated.
      My Computer


  4. Posts : 6,856
    22H2 64 Bit Pro
       #4

    It's pointless. Temp files are no longer required. Any older then 24 hours can be deleted. There is no point in keeping older temp files.
      My Computer


  5. Posts : 4
    Windows 10
    Thread Starter
       #5

    C:\Temp was a bad example..... Apologies. I have a directory where i have my designers save thier files. To keep things tidy i intend on keeping the file age to a max of 180 days. I want to run a purge script every sunday to acomplish this task, or simply enable file age in windows. Before I run it I would like to generate a report listing what will be deleted and the total size that will be freed up...

    - - - Updated - - -

    I figured out what i needed. Now just need to clean up excel. thx for the replies.

    forfiles /p "C:\randomfilepath" /s /m . /D -180 /C "cmd /c echo @file @fsize" >>C:\users\user\documents\Deleted.csv
      My Computer


  6. Posts : 787
    Windows 7
       #6

    PowerShell makes this task easier.
    Code:
    $Path = 'C:\Users\GARLIN\Downloads\Multi-boot'
    $Days = 180
    
    $Today = Get-Date
    $CutoffDate = $Today.AddDays(-$Days)
    
    $List = @(Get-ChildItem $Path -File -Recurse | Where-Object { $_.LastWriteTime -lt $CutoffDate } | foreach {
        [PSCustomObject]@{
            Days = (New-TimeSpan -Start $_.LastWriteTime -End $Today).Days
            Bytes = $_.Length
            Filename = $_.FullName
        }
        $TotalSize += $_.Length
    })
    
    switch ($TotalSize) {
        { $TotalSize -ge 1GB }  { $ReportedSize = ('{0} GB' -f ($TotalSize/1GB).tostring("#.##")) }
        { $TotalSize -ge 1MB }  { $ReportedSize = ('{0} MB' -f ($TotalSize/1MB).tostring("#.##")) }
        { $TotalSize -ge 1024 } { $ReportedSize = ('{0} KB' -f ($TotalSize/1024).tostring("#.#")) }
        default { $ReportedSize = ('{0} bytes' -f $TotalSize) }
    }
    
    '{0} Files, {1} total' -f $List.Count, $ReportedSize
    $List | Format-Table -AutoSize
    Code:
    S C:\Users\GARLIN\Downloads> .\Age.ps1
    5 Files, 7.2 KB total
    
    Days Bytes Filename                                              
    ---- ----- --------                                              
     725  5857 C:\Users\GARLIN\Downloads\Multi-boot\autounattend.xml 
     725   268 C:\Users\GARLIN\Downloads\Multi-boot\create-MBR.txt   
     725   371 C:\Users\GARLIN\Downloads\Multi-boot\create-UEFI.txt  
     725   659 C:\Users\GARLIN\Downloads\Multi-boot\startnet-ORIG.cmd
     216   195 C:\Users\GARLIN\Downloads\Multi-boot\unattend.xml
      My Computer


  7. Posts : 4
    Windows 10
    Thread Starter
       #7

    This is perfect!! What do i need to add to export to CSV?
      My Computer


  8. Posts : 787
    Windows 7
       #8

    The last two lines are inelegant, because the results header isn't the same format as the PSCustomObject. Otherwise you could just export a CSV file using "$List | Export-Csv", but that only keeps the file list.
    Code:
    $Path = 'C:\Users\GARLIN\Downloads\Multi-boot'
    $Days = 180
    
    $CSV_File = 'Report.csv'
    
    $Today = Get-Date
    $CutoffDate = $Today.AddDays(-$Days)
    
    $List = @(Get-ChildItem $Path -File -Recurse | Where-Object { $_.LastWriteTime -lt $CutoffDate } | foreach {
        [PSCustomObject]@{
            Days = (New-TimeSpan -Start $_.LastWriteTime -End $Today).Days
            Bytes = $_.Length
            Filename = $_.FullName
        }
        $TotalSize += $_.Length
    })
    
    switch ($TotalSize) {
        { $TotalSize -ge 1GB }  { $ReportedSize = ('{0} GB' -f ($TotalSize/1GB).tostring("#.##")) }
        { $TotalSize -ge 1MB }  { $ReportedSize = ('{0} MB' -f ($TotalSize/1MB).tostring("#.##")) }
        { $TotalSize -ge 1024 } { $ReportedSize = ('{0} KB' -f ($TotalSize/1024).tostring("#.#")) }
        default { $ReportedSize = ('{0} bytes' -f $TotalSize) }
    }
    
    
    @("""{0} Files"",""{1} total"",`r`n,," -f $List.Count, $ReportedSize
    $List | ConvertTo-Csv -NoTypeInformation) | Set-Content $CSV_File
    Code:
    "5 Files","7.2 KB total",
    ,,
    "Days","Bytes","Filename"
    "725","5857","C:\Users\GARLIN\Downloads\Multi-boot\autounattend.xml"
    "725","268","C:\Users\GARLIN\Downloads\Multi-boot\create-MBR.txt"
    "725","371","C:\Users\GARLIN\Downloads\Multi-boot\create-UEFI.txt"
    "725","659","C:\Users\GARLIN\Downloads\Multi-boot\startnet-ORIG.cmd"
    "216","195","C:\Users\GARLIN\Downloads\Multi-boot\unattend.xml"
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 12:44.
Find Us




Windows 10 Forums