Using forfiles to delete files, Need report first

Page 1 of 2 12 LastLast

  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,777
    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,913
    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 : 825
    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 : 825
    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


  9. Posts : 26
    Windows 11 Pro 64-bit 22H2 22621.1
       #9

    I created a much more robust Powershell function that can accomplish this. The function retrieves files from a specified directory (and optionally its subdirectories) based on their age. It can filter files that are older or newer than a specified number of days. The function displays the file size, age, and name of the matching files.

    The output is sorted by age.

    Show-FilesSortedByAgeInDirectory.ps1

    Here are some examples to get you started:

    Code:
    # Show all files modified within 10 days
    Show-FilesSortedByAgeInDirectory -Directory "C:\Windows\System32" -NewerThan 10
    
    # Show all files modified over 40 days ago
    Show-FilesSortedByAgeInDirectory -Directory "C:\Windows\System32" -OlderThan 40
    
    # Show all files CREATED within two days
    Show-FilesSortedByAgeInDirectory -Directory "C:\Users\Public" -NewerThan 2 -DateMethod Creation
    
    # Show all files MODIFIED within 20 days, and display only filenames
    Show-FilesSortedByAgeInDirectory -Directory "C:\Users\Public" -NewerThan 20 -FileDisplay FileOnly
    Edit: Screenshot.

    Using forfiles to delete files, Need report first-pwsh_shuvzacnrm.png

    Hope this helps!
      My Computer


  10. Posts : 825
    Windows 7
       #10

    Good work, and here's some points for improvement:

    1. This script uses the ternary operator [? A : B], therefore requires PowerShell 7 to run, and needs to clearly declare:
    Code:
    #Requires -Version 7.0
    Obviously, those expressions could be rewritten to work under PS 5. Please consider how many average W10 users won't have PS 7.

    2. Allow the user to sort by file age, in either direction. Right now, it's fixed to ascending and users are more likely interested in seeing the oldest files first.
    Last edited by garlin; 2 Days Ago at 01:18.
      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 13:08.
Find Us




Windows 10 Forums