473,396 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to delete files with kill dates at the end?

Alright I am writing a small little program to go through a set directory, in this case it's c:\test, and find all the files that are older than today, the naming convention is as follows:

description-KILL-02-dec-2010.mp3
description-KILL-28-dec-2010.mp3
description-KILL-1-jan-2011.mp3

Now what I have so far:

Expand|Select|Wrap|Line Numbers
  1. Imports System.Text.RegularExpressions
  2. Imports System.IO
  3. Public Class Form1
  4.  
  5.     Dim pattern As String = "(\d?\d \w\w\w \d{4})\.\w{3}$"
  6.     Sub Main()
  7.         DeleteOldFiles("C:\test")
  8.     End Sub
  9.  
  10.     Private Sub DeleteOldFiles(ByVal path As String)
  11.         Dim filterRegex As New Regex(pattern)
  12.         For Each filePath As String In Directory.EnumerateFiles(path)
  13.             Dim match As Match = filterRegex.Match(filePath)
  14.             If match.Success Then
  15.                 ' Convert the text into a date
  16.                 Dim dateString As String = match.Groups(2).Value
  17.                 Dim killDate As DateTime
  18.                 ' Note that if dates have a leading 0 like 01 nov 2010 you'll need "dd MMM yyyy".
  19.                 If DateTime.TryParseExact(dateString, "dd MMM yyyy", Nothing, Globalization.DateTimeStyles.None, killDate) Then
  20.                     If killDate < DateTime.Now Then
  21.                         File.Delete(filePath)
  22.                         Console.WriteLine("Deleting {0}...", filePath)
  23.                     End If
  24.                 End If
  25.             End If
  26.         Next
  27.         MsgBox(DateString)
  28.         MsgBox(DateTime.Now)
  29.         MsgBox(path)
  30.         MsgBox(DateString)
  31.  
  32.  
  33.     End Sub
  34.  
  35.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  36.  
  37.     End Sub
  38.  
  39.     Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  40.         MsgBox(DateString)
  41.         MsgBox(DateTime.Now)
  42.         MsgBox(DateString)
  43.         Call DeleteOldFiles("C:\test")
  44.     End Sub
  45.  
  46.     Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
  47.         MsgBox("You can't change this.")
  48.     End Sub
  49.  
  50.     Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
  51.     End Sub
  52. End Class

I was getting a few errors, now I am mysteriously not before I want to post this, one of them was:

A first chance exception of type 'System.ArgumentException' occurred in Microsoft.VisualBasic.dll

But my main problem is that we got the right file path detected, all the variables are reading correct, but the actual deleting process doesn't work. Help!?

I am just getting back into VB with a few years of hiatus, so forgive me if something looks stupid.
I am using VB 2010.

Here is a download link for the entire project so you can better assist me:

http://www.woofiles.com/dl-214353-796CAP07-DeleteOLDPsa.zip

or it's attached as well.
Attached Files
File Type: zip DeleteOLDPsa.zip (119.4 KB, 66 views)
Dec 3 '10 #1
0 817

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Ben | last post by:
Greetings, I am looking for a way to search for and delete files based on a pattern mask. For example, the search method would find all files matching a certain pattern containing wildcards (e.g....
5
by: Jobs | last post by:
Hello All, I want to delete all files in a directory. I am making a backup copy of all files in the directories say c:\abc by reading and writing to a file. After making a backup copy I want to...
2
by: U C | last post by:
Hi, Can i delete files in cdump folder. Whats the basic use of having files in cdump folder. Is there any need to take backup when deleting the same. I am having solaris if we can delet then what...
5
by: Raj | last post by:
Hi all, Can anyone help me with a script which would delete files or move them to a different folder at some scheduled time..! Please.....!!! Thanks in advance...
23
by: da Vinci | last post by:
Greetings, Onwards with the school studying. Working on a program and need to delete a file from a known location on the hard drive but cannot get anything I do to work. I have tried to use...
6
by: Sonoman | last post by:
Hi all: I would like to find out how to write a small program that deletes files. I want to be able to delete all files from a known directory (i.e. cookies, temp files, etc.) regardless of type...
3
by: Scott_Tuttle | last post by:
I'm trying to delete files with strange characters in the names but csharp doesnt seem to be able to see them at all. Solution??
2
by: David | last post by:
How can I delete files at once? Like: " delete C:\MyFiles.* "
2
by: abulreesh | last post by:
Hi, I hope someone can help me out here. I have this project where i'm supposed to write a program that takes in a user's id which (s)he enters on a web page, through CGI the page is linked to a...
1
by: amituts | last post by:
Hi! I need a script which delete files in a directory when there are more than 8 files exist in that directory. It should delete oldest file and 3 latest file should be remains in that directory. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.