+ Post New Thread
Results 1 to 4 of 4
Coding Thread, .bat file clear old files in Coding and Web Development; Hi All, Been working on another project. I am looking for a piece of code, to put in a .bat ...
  1. #1
    T_Noble's Avatar
    Join Date
    Jan 2010
    Location
    Gloucestershire
    Posts
    13
    Thank Post
    7
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    .bat file clear old files

    Hi All,

    Been working on another project.

    I am looking for a piece of code, to put in a .bat file, that does the following:

    *Filename* is stored like the following example "19-01-2010--14-51.sql"

    ----
    Looks at the current date
    If C:\LoginTrackerBackups\*Filename* is 7 days or older than the current date
    DeleteFile
    Else
    Move onto the Next file
    End
    ----

    Kind Regards

    Tom

  2. #2

    Join Date
    Dec 2007
    Location
    cumbria
    Posts
    182
    Thank Post
    7
    Thanked 43 Times in 39 Posts
    Rep Power
    25
    It's not quite what you were talking about,
    but here's a few lines I used last week to get a backup job tidying up after itself ...

    net use Z: \DELETE
    net use Z: \\server\share
    forfiles /P Z: /M "*.BKF" /D -21 /C "CMD /C DEL @FILE"

  3. #3
    T_Noble's Avatar
    Join Date
    Jan 2010
    Location
    Gloucestershire
    Posts
    13
    Thank Post
    7
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Quote Originally Posted by jinnantonnix View Post
    You should really store your filenames like "2010-10-29--14-15.sql", not "19-01-2010--14-51.sql"
    Even so, it's tricky to do the date arithmetic in a script, more so in a batch file.

    All is not lost, though. If the age of the file is correct, you can use ROBOCOPY, which makes short work of tasks like these.


    Code:
    md c:\MyTemp
    robocopy.exe C:\LoginTrackerBackups C:\MyTemp  /e /MOVE  /MINAGE:7 *
    del /s /q /f c:\MyTemp
    This will move all files older than 7 days to MyTemp, then delete MyTemp.
    I am hoping that the MINAGE part is going to work. This is something I cannot test until this time next week, as I dont have a file in that folder that is more than a week old. Does it look at the File Modified Date attribute to work out if it is over a week old?

    Kind Regards

    Tom

  4. #4


    Join Date
    Feb 2007
    Location
    51.405546, -0.510212
    Posts
    8,706
    Thank Post
    220
    Thanked 2,615 Times in 1,926 Posts
    Rep Power
    777
    The attached script should do it. Although it's a VBScript you can use CScript to run it in a batch file. Run 'cscript PurgeFiles.vbs /?' from a command prompt for help.

    Another alternative (if you don't mind relying on third-party programs) is DelOld.
    Attached Files Attached Files

SHARE:
+ Post New Thread

Similar Threads

  1. Sims permissions bat file
    By ful56_uk in forum MIS Systems
    Replies: 21
    Last Post: 24th April 2010, 08:53 PM
  2. BAT File issue
    By Danny159 in forum Windows Vista
    Replies: 2
    Last Post: 14th March 2009, 01:54 PM
  3. Client's Are using .bat files.
    By Lesley_tech in forum General Chat
    Replies: 2
    Last Post: 13th June 2008, 04:09 PM
  4. Stopping SQL using a Bat file
    By ful56_uk in forum Windows
    Replies: 4
    Last Post: 7th April 2008, 12:09 AM
  5. How to block .bat and .exe files
    By z4ydi in forum Network and Classroom Management
    Replies: 6
    Last Post: 12th February 2008, 02:36 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •