Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 464949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:19:37+00:00 2026-05-12T23:19:37+00:00

Inside a folder (say c:\test) I want to delete the oldest file if the

  • 0

Inside a folder (say c:\test) I want to delete the oldest file if the number of files are over 21.

This is what I came up with, issue is that it one time deletes the oldest file, second run it does nothing and deletes the oldest for third, continues like that. Also it doesn’t care if the file amount is lower than 21, deletes even if it is lower.

Here is the code:

SET BACKUPDIR=C:\test
SET countfiles=dir BACKUPDIR /b | find /v /c "::"

if countfiles GTR 21 (
FOR /F %%i IN ('DIR /B /O-D %BACKUPDIR%') DO SET OLDEST=%%i
DEL %BACKUPDIR%\%OLDEST%
)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T23:19:37+00:00Added an answer on May 12, 2026 at 11:19 pm

    here’s an alternative in vbscript, that iterates the directory just once.

    Set objFS=CreateObject("Scripting.FileSystemObject")
    Set objArgs = WScript.Arguments
    strFolder = objArgs(0)
    strScriptName = WScript.ScriptFullName 'get script name
    Set objFolder = objFS.GetFolder(strFolder)
    Set dict = CreateObject("Scripting.Dictionary") 
    current  = Now
    temp=0
    count=0 'count files
    For Each strFile In objFolder.Files
        If strFile.Path <> strScriptName Then 'do if not script name
          count=count+1
          dict.Add strFile.Path, strFile.DateLastModified
          'if there are more than 21 files
          If count >= 21 Then
            flag=1
          End If
        End If
    Next
    
    filedate = dict.Items
    filename = dict.keys
    If flag = 1 Then
        For i = 0 To dict.Count -1 'iterate dictionary
          diffdate = DateDiff("d",filedate(i),now)
          If num_days_diff >= temp Then
            oldestfile = filename(i)
            oldestdate = filedate(i)
            temp = num_days_diff
          End If             
        Next
    End If 
    Set dict=Nothing
    WScript.Echo "Oldest file: " & oldestfile, oldestdate
    objFS.DeleteFile(oldestfile)
    

    save as deletefile.vbs and on command line

    c:\test> cscript /nologo deletefile.vbs c:\path\somewhere
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this file. let's say x for this example inside folder y .
I have a folder and inside that folder I have 10-15 files with arbitrary
When there is no files inside the folder the below script goes inside the
I have HTML pages that I put inside temp folder(Outside WEB-INF directory). I have
My OS environment is linux. I have a php file in this folder -
I am using this chat: http://www.phpfreechat.net/ I place it inside a folder called chat
I have a zip file (let's say 'inside.zip') inside another zip file (let's say
I have folder inside reource folder lets say resource->folder A->61.png so how to access
My code runs inside a JAR file, say foo.jar , and I need to
I have, let's say, www.website.org/folder/ which inside has the following .htaccess RewriteEngine On RewriteRule

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.