[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 959
  • Last Modified:

Delete Files from Temp Directory that are at least 2 hours old

Is there a Windows utility or method not using vb script that can delete files from a temporary directory that are at least 2 hours old or more.
0
bmichaelh
Asked:
bmichaelh
  • 3
  • 2
  • 2
  • +6
4 Solutions
 
koreansuperCommented:
Use CCleaner program to delete file sfrom temp directory. the program is free to use and download and it is recommended for all user and it has supported xp and vista
0
 
bmichaelhAuthor Commented:
Thank you.

I should have added that I need to run as a batch file so that I can run it as a scheduled job.
0
 
Joseph DalyCommented:
Ok so this is what im thinking. Its basically a two step process there is only one part of it that I dont have figured out yet.

Robocopy has an option to only move files older than a certain amount of time. I believe the switch  you will want to use is /mingage

/MINAGE:n : MINimum file AGE - exclude files newer than n days/date

What you would do is have robocopy run and move the files in your folder that are older than a certain date to another folder. Once they were in there you could use the del command to delete the folders. The batch file would be something similar to below.

The only thing I have to figure out is how to specify hours in the robocopy move command instead of days.
robocopy /sourcedire /targetdir /mov /MINAGE:n 
del targetdir\*.* /F /Q

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Shift-3Commented:
Here's a way to do it with VBScript.

Paste the script below into a text file with a .vbs extension.  Customize the value of the strRoot variable on line 1 with the location of the temporary folder.  To include files in subfolders, change the value of the blnSubFolders variable on line 3 to True.

Running the script (e.g. with cscript filename.vbs) will echo the actions to be performed.  Once you have tested it successfully, remove the apostrophe from line 29 to delete the files.

Note that this deletes files permanently, without moving them to the Recycle Bin.


strRoot = "c:\temp"
strMask = "*.*"
blnSubFolders = False
intHours = 2
 
strCommand = "cmd /c @echo off & dir " & Chr(34) & _
    strRoot & "\" & strMask & Chr(34) & " /a:-d /b"
    
If blnSubFolders Then
    strCommand = strCommand & " /s"
End If
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Set objWshScriptExec = objShell.Exec(strCommand)
Set objStdOut = objWshScriptExec.StdOut
 
Do Until objStdOut.AtEndOfStream
    strFile = objStdOut.ReadLine
    
    If Not blnSubFolders Then
        strFile = strRoot & "\" & strFile
    End If
    
    Set objFile = objFSO.GetFile(strFile)
    
    If DateDiff("h", objFile.DateLastModified, Now) > intHours Then
        WScript.Echo "Delete " & strFile
        'objFile.Delete True
    End If
Loop

Open in new window

0
 
BillDLCommented:
Hi bmichaelh

The expert batch files in this question will give you some idea of the complexities involved with reliably using dates and times in pure "DOS" batch:

http://www.experts-exchange.com/Operating_Systems/MSDOS/Q_20810699.html

Another:
http://fixunix.com/ms-dos/270673-please-help-batch-files-delete-files-older-than-x-minutes-specific-directory.html

Calculating Days from the %DATE% is hard enough when you consider the different number of days in different months, but calculating the Hours or Minutes using the %TIME% causes problems of its own because of the different time formats and calculating back past midnight.

Compare this to the apparent simplicity of a VBScript:

http://halfloaded.com/blog/vbscript-delete-all-files-older-than-one-hour/

Why is it that you can't use a *.vbs file?
0
 
BillDLCommented:
I was googling and typing while Shift-3 was posting ;-)
0
 
Bill PrewCommented:
Here's a prior question that has a (lengthy) batch script approach to the question you asked.  It could probably be made to do what you need, or perhaps after you see the complexity involved you'll decide to pursue a non batch approach.

If you still want to pursue a batch script approach let me know.

~bp
0
 
Bill PrewCommented:
Sorry, hit SUBMIT too sooon, here's the link.

http://www.experts-exchange.com/Operating_Systems/MSDOS/Q_20810699.html

~bp
0
 
Steve KnightIT ConsultancyCommented:
Personally I'd do this in VBScript too... or a at a push and having to be seen to be using a batch file then use a batch file that gets VBScript to do the hard work of checking the file age. The one on my page below is checking for a file being there in the last 20 mins but the same sort of principal:

http://scripts.dragon-it.co.uk/scripts.nsf/8178b1c14b1e9b6b8525624f0062fe9f/a3af60ecd1228adb8025765d00438528!OpenDocument

Resource kit command forfiles.exe only works on whole days old as far as I remember (don't have it on this machine) otherwise that would be an option too.

Steve
0
 
bmichaelhAuthor Commented:
If I use VBScript and run this on a server.  Is there anything that needs to be enabled on the server for this to run?
0
 
Steve KnightIT ConsultancyCommented:
It shouldn't be an issue on 2000 or 2003 servers.  I haven't tried VBScript on a 2008 one yet so can;t say either way.

Try this as a simple test on one of your servers:

x=inputbox("please enter something")
wscript.echo x

And save it as test.vbs

then double click it from explorer on the server or from a command prompt using:

cscript //nologo test1.vbs

Steve
0
 
David Johnson, CD, MVPOwnerCommented:
vbscript works fine in ws2008/w7
0
 
David Johnson, CD, MVPOwnerCommented:
vbscript works fine in ws2008/w7
0
 
ChandarSCommented:
U can simply right a batch file to Delete Junk file

Following file types are considered as junk files : *.~* ~*.* *.??~ *. *.tmp *._mp *.old *.bak *.syd *.chk *.gid mscreate.dir *.dmp 0???????.nch
You can make a batch file that scans for them and deletes them with the following content:
del c:\*.~* /s /q
del c:\~*.* /s /q
del c:\*.??~ /s /q
del c:\*. /s /q
del c:\*.tmp /s /q
del c:\*._mp /s /q
del c:\*.old /s /q
del c:\*.bak /s /q
del c:\*.syd /s /q
del c:\*.chk /s /q
del c:\*.gid /s /q
del c:\mscreate.dir /s /q
del c:\*.dmp /s /q
del c:\0???????.nch /s /q
del c:\windows\temp\*.* /q
del c:\temp\*.* /q
del d:\*.~* /s /q
del d:\~*.* /s /q
del d:\*.??~ /s /q
del d:\*. /s /q
del d:\*.tmp /s /q
del d:\*._mp /s /q
del d:\*.old /s /q
del d:\*.bak /s /q
del d:\*.syd /s /q
del d:\*.chk /s /q
del d:\*.gid /s /q
del d:\mscreate.dir /s /q
del d:\*.dmp /s /q
del d:\0???????.nch /s /q
Add commands for other partitions in a similar fashion. Again, type del /? at the command prompt for more about this command.

Regards,
Chandar Singh
0
 
BillDLCommented:
Thank you bmichaelh.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now