Batch file to delete files older than N days

Batch file to delete files older than N days

Asked on November 29, 2018 in Windows.
Add Comment


  • 7 Answer(s)

    I hope this will help you.

    forfiles -p "C:\what\ever" -s -m *.* -d <number of days> -c "cmd /c del @path"
    

    For further information and doubts , refer here forfile documentation and An A-Z Index of the Windows XP command line.

    copy forfiles from any Windows Server 2003 to your Windows XP machine at %WinDir%\system32\ if you don’t have it. This is possible since the EXE is fully compatible between Windows Server 2003 and Windows XP.

    But the good news is , latest version have these as default. if you have windows give this command,

    forfiles /p "C:\what\ever" /s /m *.* /D -<number of days> /C "cmd /c del @path"
    

     

     

    Answered on November 29, 2018.
    Add Comment

    Try this one, this should works.

    ROBOCOPY C:\source C:\destination /mov /minage:7
    del C:\destination /q
    

    Move the all files, via robocopy to another location, and then execute a delete command on that path and you’re all good.If you want you have use also the /mir comment.

    Answered on November 29, 2018.
    Add Comment

    Here is the answer,

    forfiles /p "v:" /s /m *.* /d -3 /c "cmd /c del @path"
    
    

    First forfiles don’t support UNC paths, so make a network connection to a specific drive. All the complicated batches could be in the trash bin. we should use /day -3 for 3 days earlier .

    Answered on November 29, 2018.
    Add Comment

    Have a look at my answer to a similar question:

    REM del_old.bat
    REM usage: del_old MM-DD-YYY
    for /f "tokens=*" %%a IN ('xcopy *.* /d:%1 /L /I null') do if exist %%~nxa echo %%~nxa >> FILES_TO_KEEP.TXT
    for /f "tokens=*" %%a IN ('xcopy *.* /L /I /EXCLUDE:FILES_TO_KEEP.TXT null') do if exist "%%~nxa" del "%%~nxa"
    

    This deletes files older than a given date. I’m sure it can be modified to go back seven days from the current date.

    update: I notice that HerbCSO has improved on the above script. I recommend using his versioninstead.

    Answered on January 19, 2019.
    Add Comment

    Borrowed some of the codes from good stuff i was reading and made some modifications until i got stuck with the code actually not doing what its suppose to do (really my bad 🙂

    #
    #          Made by KCOR
    #          05/24/2016
    ##################
    Function DelOldFldr
    {
    $Days = Read-Host How old is the folder? #You can change the number of days here (we used -ge since we wanted to delete all folders created for the last 30days or older
    $TargetFolder = Import-Csv C:\temp\KCOR\FoldersOriginal.csv #Enter full Target folder path
    if (Test-Path $TargetFolder)
    {
    
    $Now = Get-Date
    $LastWrite = $Now.AddDays(-$days)
    if ($targetfolder.unc -eq $LastWrite){
    write-host "Deleting $unc" -foregroundcolor "Green";Remove-Item -Path $unc.unc -Recurse -force 
    # it says here that whatever folders inside each path in csv file that is -le(greater than or equal to the $days(in this case 30 will be deleted) can be $_.lastwritetime or creationtime
    Write-Output $unc (Get-Date -UFormat %d%m%y)>> c:\temp\kcor\DeletedFolders.log
    }
    Else
    {Write-Host "The folders $targetfolder are not Old Enough! " -foregroundcolor "red"}
    }
    }
    
    delOldFldr
    Answered on January 19, 2019.
    Add Comment

    For further information and doubts , refer here forfile documentation and An A-Z Index of the Windows XP command line

    1
    forfiles /p "v:" /s /m *.* /d -3 /c "cmd /c del @path"

    or

    2
    forfiles /p "C:\what\ever" /s /m *.* /D -<number of days> /C "cmd /c del @path
    Answered on January 30, 2019.
    Add Comment
    1. Open a new command prompt instance.
    2. Type the following command: ForFiles /p “C:\My Folder” /s /d -30 /c “cmd /c del @file” Substitute the folder path and the amount of days with desired values and you are done.
    Answered on February 17, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.