[Webinar] Streamline your web hosting managementRegister Today

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

Delete files if date created is equal to todays date

Hi,

I would like a script to look in a specific folder e.g. C:\testing\deletedates

The delete any files where the date created is equal to todays date, all other files can be ignored.

It could be VBS or BAT

Many thanks

Ps. I have the below VBS but it does not seem to work

Dim oFs
Dim oFolder
Dim oFile
Dim Folder
Dim FolderSpec
Dim FileCollection
 
FolderSpec = "C:\testing\deletedates"
Set oFs = CreateObject("Scripting.FileSystemObject")
 
If oFs.FolderExists(FolderSpec) Then
     Set oFolder = oFs.GetFolder(FolderSpec)
     Set FileCollection =  oFolder.SubFolders
     On Error Resume Next
     For Each subfolders In FileCollection
          For Each oFile In subfolders.Files
            If oFile.DateLastModified < (Date() - 1) Then
                oFile.Delete(True)
                End If
          Next
     Next
End If

Open in new window

0
arundelr
Asked:
arundelr
  • 9
  • 4
1 Solution
 
knightEknightCommented:
Create this line in a batch file in the folder where your files exist:

   for /f "tokens=1-5" %%F in ('dir *.txt ^| find "%date:~4%"') do @echo %%J

The replace the filespec *.txt with what ever filespec you want to delete.
0
 
knightEknightCommented:
oops, but change  "@echo"  to  "del"
0
 
knightEknightCommented:
actually you may want to run it first with the @echo still in there first -- it will show you which files will be affected.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
knightEknightCommented:
... and to make sure it is in the correct folder:

  @echo off
    c:
    cd C:\testing\deletedates
    for /f "tokens=1-5" %%F in ('dir *.txt ^| find "%date:~4%"') do del %%J

0
 
arundelrAuthor Commented:
Hi,

Thanks.. Not sure why but I get the error "The syntax of the command is incorrect."
0
 
knightEknightCommented:
what OS?
0
 
arundelrAuthor Commented:
Sorry, XP
0
 
arundelrAuthor Commented:
Also same result on 2000 server (attached is the exact code I used)
@echo off
c:
cd C:\testing\deletedates
for /f "tokens=1-5" %%F in ('dir *.csv ^| find "%date:~4%"') do del %%J
pause

Open in new window

0
 
knightEknightCommented:
You need to have "Command Extensions" enabled to use  for /f

One quick way to do this is to use the /y option when you open the command shell:

   start cmd.exe /y

See this article on how to enable them system wide:  http://www.windowsitpro.com/Article/ArticleID/13556/13556.html

0
 
knightEknightCommented:
 start cmd.exe /y

... then run the batch file in the new window.
0
 
knightEknightCommented:
SORRY!  the switch is  /x  not  /y

  start cmd.exe /x

( /y expressly turns off Command Extensions -- duh! )
0
 
arundelrAuthor Commented:
Hi,

Sorry, I tried doing that but still the same error ;o(
0
 
knightEknightCommented:
Well, I don't know -- I cut-n-pasted your code above and it works perfectly for me on XP.  The only thing I can think of is to check the single-quotes inside the parens in the for-loop.  Make sure they are the correct type of quotes.

alertnatively you can use the "other" backquote (the little one next to the 1 key), but you have to specify the usebackq option:

  for /f "usebackq tokens=1-5" %%F in (`dir *.csv ^| find "%date:~4%"`) do @echo del %%J
0
 
RobSampsonCommented:
Try this script.  I have taken the example you provided, removed the On Error Resume Next, and added some output so you may be able to see why it's not deleting. I have also commented out the actual oFile.Delete line for testing...

'==============
If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
      strPath = Wscript.ScriptFullName
      strCommand = "%comspec% /k cscript  """ & strPath & """"
      Set objShell = CreateObject("Wscript.Shell")
      objShell.Run(strCommand), 1, True
      Wscript.Quit
End If

Dim oFs
Dim oFolder
Dim oFile
Dim Folder
Dim FolderSpec
Dim FileCollection
 
FolderSpec = "C:\testing\deletedates"
FolderSpec = "C:\Temp\Scripts"
Set oFs = CreateObject("Scripting.FileSystemObject")
 
If oFs.FolderExists(FolderSpec) Then
     Set oFolder = oFs.GetFolder(FolderSpec)
     Set FileCollection =  oFolder.SubFolders
     'On Error Resume Next
     For Each subfolders In FileCollection
          For Each oFile In subfolders.Files
            WScript.Echo "Checking if file date: " & oFile.DateLastModified & VbCrLf & _
                  "is less than yesterday: " & Date() - 1 & VbCrLf
            If oFile.DateLastModified < (Date() - 1) Then
                WScript.Echo "Deleting " & oFile.Path
                'oFile.Delete True
            End If
          Next
     Next
End If


WScript.Echo "Done"
'==============

Regards,

Rob.
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 9
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now