Results 1 to 12 of 12

Thread: Script needed to Delete Files in a Directory Based on Date

  1. #1
    adaher008 via WinServerKB.com Guest

    Script needed to Delete Files in a Directory Based on Date

    I have a folder that contains about 250,000 zipped files. Windows explorer is
    having tremendous difficulty opening such folder, even when it displays
    results (after 10 or so minutes) it hangs after I even highlight any files
    for deletion. a lot of these files go back to 2006 and 2007.

    I kindly NEED a DOS batch file or some script that I would enable me to
    delete the files that are older than may 2008 (In other words delete files
    between May 2008 and May 2006).

    Thank You, and your efforts are much appreciated in advance.

  2. #2
    Pegasus \(MVP\) Guest
    Try this script, written by Tom Lavedas and myself. It will delete files
    older than 120 days.
    sFolder = "d:\temp\"
    iMaxAge = 120
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    If oFSO.FolderExists(sFolder) Then
    for each oFile in oFSO.GetFolder(sFolder).Files
    If DateDiff("d", oFile.DateLastModified, Now) > iMaxAge Then
    wscript.echo "Deleting oFile.Name"
    ' oFile.Delete
    End If
    next
    End If

    Run the script in its current form. When you're happy with the result,
    activate it by removing the single quote from this line:
    ' oFile.Delete

    Tiny typo in the echo line, it should read ...

    wscript.echo "Deleting", oFile.Name

  3. #3
    Join Date
    Sep 2008
    Posts
    2
    You could always use forfiles.exe. Here is a guide on using it with a working example. I ahve used it and it works great

    forfiles.exe comes with server 2003 or is downloadable from the server 200 resource kit.

  4. #4
    Join Date
    Feb 2010
    Posts
    2

    Re: Script needed to Delete Files in a Directory Based on Date

    Im a newbie in creating script..I would like to ask if you can provide me a script that can be turn into a dos.bat file that I can run on my windows xp pc that will delete txt files older than 30 days my working directory is d:\temp .

  5. #5
    Join Date
    Dec 2009
    Posts
    23

    Re: Script needed to Delete Files in a Directory Based on Date

    This script will delete all files that are not modified in n days within the directory structure d.
    Code:
    # Script DeleteFiles.txt
    var str d, n, list, file
    lf -r -n "*" $d (($ftype=="f") AND ($fmtime < addtime(diff("-"+$n)))) > $list
    while ($list <> "")
    do
        lex "1" $list > $file
        system del ("\""+$file+"\"")
        echo -e "DEBUG: Deleted file " $file
    done
    Script is in biterscripting can be download if you don't have it). Save the script in file C:/Scripts/DeleteFiles.txt. Kick off the script by typing this command into biterscripting.
    s
    Code:
    cript "C:/Scripts/DeleteFiles.txt" d("d:/temp") n("30")

  6. #6
    Join Date
    Feb 2010
    Posts
    2

    Re: Script needed to Delete Files in a Directory Based on Date

    But is there a way that it can be made into a Dos bat file so that it can be executed once i double click it?

  7. #7
    Join Date
    Dec 2009
    Posts
    23

    Re: Script needed to Delete Files in a Directory Based on Date

    Yes, there is a way to execute this script with a double-click.

    Create an icon on the desktop, and assign it this command.
    Code:
    "C:/biterscripting/biterscripting.ex" "C:/Scripts/DeleteFiles.txt" d("d:/temp") n("30")
    Then when you double click this icon, the script will be executed. (Basically, we are replacing the command word 'script' with path to biterscripting executable. This works with all scripts. I often do that.)

    - There was an error in my first post - the first letter of the script command - s - somehow appears outside the code block. Now, I can not fix it. But, I am sure you figured it out. So, the correct command to execute the script interactively is -
    Code:
    script "C:/Scripts/DeleteFiles.txt" d("d:/temp") n("30")
    Last edited by ranjankumar09; 13-03-2010 at 02:05 AM.

  8. #8
    Join Date
    Aug 2010
    Posts
    1

    Re: Script needed to Delete Files in a Directory Based on Date

    Hi

    I want a script which will delete a file or folder or a drive on a date.

    Example: delete a file called " deleteit.txt" on date 8-20-2010

    and the date should be future date , it means if todays date is 8-18-2010
    i want a delete a file or folder or drive on 8-25-2010

    Thanks.
    Yellowblock.

  9. #9
    Join Date
    Dec 2009
    Posts
    23

    Re: Script needed to Delete Files in a Directory Based on Date

    @ Yellowblock


    the date should be future date , it means if todays date is 8-18-2010
    i want a delete a file or folder or drive on 8-25-2010
    I am not sure if I fully understand, but sounds to me that you want to delete a file "deleteit.txt" that is a week old. There are multiple files called "deleteit.txt" on the drive, and you want to delete the one that was last modified a week ago.

    Here is a script


    Code:
    # Script Delete7days.txt
    # Input arguments - drive letter, file name
    var string drive, name
    # Other variables
    var string list, file, targettime
    # Check if arguments assigned.
    if ($drive=="") or ($name=="")
        exit 1 "Error: Input arguments not assigned. Usage: script "Delete7days.txt" drive("D:/") file("deleteit.txt")
    endif
    
    # Target time is now - 7 days.
    set $targettime = addtime( diff("-7000000") )
    # Get a list of files that were modified earlier than $targettime.
    lf -r -n $name $drive ($fmtime < $targettime) > $list
    # Delete file.
    while ($list <> "")
    do
        lex "1" $list > $file
        system delete ("\""+$file+"\"")
    done

    The "-7000000" means subtract 7 days, 00 hours, 00 mins, 00 seconds from current time. See the documentation for function addtime().

    Save the script in file C:/Scripts/Delete7days.txt. Create a shortcut on desktop and assign it this command.

    Code:
    "C:/biterscripting/biterscripting.ex" "C:/Scripts/Delete7days.txt" drive("D:/
    ") name("deleteit.txt")
    Each time you double click this icon, it will delete the file "deleteit.txt" that was modified 7 days ago from drive D:/.

  10. #10
    Join Date
    Mar 2011
    Posts
    1

    Re: Script needed to Delete Files in a Directory Based on Date

    I have folders named according to date format like
    14Mar2011, 13Mar2011
    so I want a script that deletes files older than 10 days
    thank you

  11. #11
    Join Date
    Nov 2009
    Posts
    1,191

    Re: Script needed to Delete Files in a Directory Based on Date

    You can try to make use fo the script below and see if that helps you. This script will delete the files which are older than 120 days. You can change this value according to your need.
    myfolder = "d:\temp\"
    iMaxAge = 120
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    If oFSO.FolderExists(myfolder) Then
    for each oFile in oFSO.GetFolder(myfolder).Files
    If DateDiff("d", oFile.DateLastModified, Now) > iMaxAge Then
    wscript.echo "Deleting oFile.Name"
    ' oFile.Delete
    End If
    next
    End If
    After running this, if you are happy by the effect then you can activate this taking out the single quote from line
    ' oFile.Delete

  12. #12
    Join Date
    Jan 2012
    Posts
    1

    Re: Script needed to Delete Files in a Directory Based on Date

    Quote Originally Posted by Pegasus \(MVP\) View Post
    Try this script, written by Tom Lavedas and myself. It will delete files
    older than 120 days.
    sFolder = "d:\temp\"
    iMaxAge = 120
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    If oFSO.FolderExists(sFolder) Then
    for each oFile in oFSO.GetFolder(sFolder).Files
    If DateDiff("d", oFile.DateLastModified, Now) > iMaxAge Then
    wscript.echo "Deleting oFile.Name"
    ' oFile.Delete
    End If
    next
    End If

    Run the script in its current form. When you're happy with the result,
    activate it by removing the single quote from this line:
    ' oFile.Delete

    Tiny typo in the echo line, it should read ...

    wscript.echo "Deleting", oFile.Name


    yo dude this works very fine but how do you make it instead of deleting a file, to run an .exe file after the specified time
    for example:
    after 30 days to
    RUN C:\folder\my.exe
    please reply

Similar Threads

  1. Delete files based on creation date?
    By Hank Arnold (MVP) in forum Windows Server Help
    Replies: 2
    Last Post: 19-10-2010, 12:55 AM
  2. Batch script to delete directory
    By Logan 2 in forum Software Development
    Replies: 4
    Last Post: 01-04-2010, 12:31 PM
  3. Create new folder and move files based on creation date
    By dstiff in forum Windows Server Help
    Replies: 3
    Last Post: 13-11-2009, 05:04 AM
  4. Delete files with logoff/on script
    By Pine Le in forum Windows Security
    Replies: 1
    Last Post: 03-06-2008, 11:19 PM
  5. Need a script to delete temporary ASP.NET files
    By Mak66 in forum Windows Server Help
    Replies: 2
    Last Post: 13-02-2008, 07:00 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,687,354.29407 seconds with 17 queries