Who knows a lot about making .bat files?

How can i make a .bat file that will search a hard drive for all files which have been modified within a certain date range and have them deleted? I know that xcopy has a d-m-y switch with it, but that will just copy files instead of deleting them.

I guess i should ask if this is even able to be done with a .bat or if a vbscript or javascript would do this better?

Yes, it can be done in DOS with the FOR command though don’t have the knowledge (nor time) to help you atm :slight_smile:
Check this page for examples…

http://www.robvanderwoude.com/index.html

but I’m sure someone will drop by with a better answer !

You might want to check out a nice little piece of freeware called xxcopy (available at, of course, www.xxcopy.com). It’s like xcopy, but has an almost bewildering array of switches and options. I can’t swear to it, but I’ll wager you can get it to do what you’re after.

If I were doing this, I would lean toward using a more fully functional scripting language instead of DOS BAT files. This would be simple to do in any scripting langauge. You mention VBScript, which has a FileSystemObject which gives you access to everything you need. VBScript also has Folder and File objects which you create through FileSystemObject and give you details about those items. You could set up a script to recurse through subdirectories and do the delete based on the File.DateLastModified property.

One advantage of doing this in a higher-level script is that you could include more extensive logging of what was done. Instead of just getting a “done” response, you could log every action to a file so you have a record of exactly what files were deleted on each pass. This is especially useful in debugging. I typically write a script like this to include extensive logging to a file and include a “debug flag” in the header which allows me to turn off the script actions and just log what it would have done. That is, instead of deleting everything in the date range, do the logging but skip the delete. This allows you to painlessly debug to make sure the script does what you intend it to do without wreaking havoc on your files.

I checked out the xxcopy tool and have been perusing the 10 pages worth of switches looking for what i need. I think i found it and now just have to toggle the dates and stuff.

micco, the only problem with doing a script is that neither me, nor those i work with are familiar with doing them. :smiley:

Here’s a bit of VBScript that will recurse through all the subdirectories below a specified directory and list files with name, last modified date, etc. If you look in the EnumerateFiles subroutine, you’ll see a comment that says “process file” where it just outputs the filename and last modified date. You could add a little conditional there to check the date against your range, and add a FileSystemObject.DeleteFile call to delete them if they pass the check. I haven’t added those bits because I don’t have time to debug and don’t want to give you a script I haven’t tested, but the script below has the code to walk the directories and get file details, which is the guts of what you need.

To run this, save it in a file with a VBS extension (e.g. myscript.vbs), modify the path specified in the sPath variable at the top, and run from a DOS prompt using “cscript myscript.vbs”.




Set disp = WScript.StdOut
Set fs = WScript.CreateObject("Scripting.FileSystemObject")

disp.Write "Start: " & Now & vbCrLf

sPath = "c:\mydir\"

'process root folder
Set oFolder = fs.GetFolder(sPath)
EnumerateFiles oFolder

'process subdirectories
RecurseFolder(sPath)

disp.Write "Done: " & Now & vbCrLf

'------------------------------------------------------------
'loop files in directory
Sub EnumerateFiles(oFolder)
    For Each oFile in oFolder.Files
        sName = oFile.Path
        sTimestamp = oFile.DateLastModified
        sSize = oFile.Size
        sExt = LCase(Right(oFile.ShortName, Len(oFile.ShortName)-InstrRev(oFile.ShortName, ".")))

        'process file
        disp.Write "File: " & sName & " - " & sTimestamp & vbCrlf
    Next
End Sub

'loop subdirectories in directory
Function RecurseFolder(sDir)
    'process root files
    Set oFolders = fs.GetFolder(sDir)

    'process subdirectories
    If oFolders.SubFolders.Count > 0 Then
        For Each oFolder In oFolders.SubFolders
            EnumerateFiles oFolder
            RecurseFolder oFolder.Path
        Next
    End If
End Function


1988 = BAT files
2004 = CMD files

Assuming you’re using NT\2000\XP, of course.

Since the file’s extension is .bat i will keep calling it as i know it. It’s not like i call .jpg a PhotoImpact file just b/c i open it with a modern program. :-/

I warned you that the sheer number of options was a little overwhelming! :smiley:

Just be damned careful when fiddling with the “delete” options - make sure you’ve got good backups. If you get the file selection criteria wrong, whoops! There goes that pesky c:\windows directory!