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

arundelrAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.