Solved

How do I look for a file with no extension and delete it.  VB.net 2005

Posted on 2011-02-16
18
614 Views
Last Modified: 2013-11-05
I want to do house keeping on a folder via my VB App.  I have Alpha Numeric files with no extension that is being copied everytime an excel file is created.  They are always 8 characters in length.  I want to look in the folder and if those files are present delete them.  This what I've tried, but it doesn't work.


Dim file5 As String = "N:\Archive\*.*"
            If System.IO.File.Exists(file5).Length = 8 Then
                System.IO.File.Delete(file5)
            End If
0
Comment
Question by:TEStack
  • 11
  • 6
18 Comments
 
LVL 11

Expert Comment

by:jasonduan
ID: 34906850
DirectoryInfo info = new DirectoryInfo( @"N:\Archive" );
foreach( FileInfo finfo in info.GetFiles( "*.*" ) )
{
    if( finfo.Name.Length==8 && !finfo.Contains(".") )
       finfo.Delete();
}


This is C#, you should be able to easily convert it to VB.net
0
 

Author Comment

by:TEStack
ID: 34906870
I'm afraid it's not easy for me.  I'm pretty much a rookie and don't know any C#.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34907039
If you are using .Net 3.5 or above you can do it with Linq. Sample follows:
Imports System.Linq
Imports System.IO

Module Module1

    Sub Main()

        Dim files = From file In (New DirectoryInfo("N:\Archive\").GetFiles()) _
                                  Where file.Name.Length = 8 And String.IsNullOrEmpty(file.Extension) _
                                  Select file

        For Each f In files
            '// Do something with each file
        Next

    End Sub

End Module

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:TEStack
ID: 34907933
Does anyone else know how this might be done in vb.net?
0
 

Author Comment

by:TEStack
ID: 34907946
Disregard last post I hadn't updated my screen.
0
 

Author Comment

by:TEStack
ID: 34908426
I get this error
namespace or type specified in the Imports "system.Linq' doesn't contain any public member or cannot be found.  Make sure the namespace or the type is defined and contains at least one public member.  Make sure the  imported element name doesn't use any aliases.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34908455
Can you post your code precisely as you have it? Including the "Imports" line.
0
 

Author Comment

by:TEStack
ID: 34908483
Imports System.Linq
Imports System.IO

Module Module1
   
    Sub Main()

        Dim files = From file In (New DirectoryInfo("N:\Archive\").GetFiles()) _
                                  Where file.Name.Length = 8 And String.IsNullOrEmpty(file.Extension) _
                                  Select file

        For Each f In files
            '// Do something with each file
        Next

    End Sub





End Module
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34908515
What version of the .net framework are you using? Linq is only available with 3.0 onwards.
0
 

Author Comment

by:TEStack
ID: 34908546
I'm running VB.net 2005.  Under add and remove softeware I see .net 3.5.  If I look at my references in my program they refer to 2.0.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34908643
Ah, in that case you're stuck with 2.0. Try this version instead:
Imports System.Linq
Imports System.IO

Module Module1

    Sub Main()

        Dim files As FileInfo() = (New DirectoryInfo("N:\Archive").GetFiles())

        For Each f In files
                If f.Name.Length = 8 And String.IsNullOrEmpty(f.Extension) Then
                     '// Do something with the file
                End If
        Next

    End Sub

End Module

Open in new window

0
 

Author Comment

by:TEStack
ID: 34908731
Do I leave Imports System.Linq out?  How should f be declared?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34908741
You leave it out. System.Linq isn't available in VS2005.
0
 

Author Comment

by:TEStack
ID: 34908759
You left it in your example above.  How should F be declared?
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 500 total points
ID: 34908783
Ah, so I did :)

Updated version:
Imports System.IO

Module Module1

    Sub Main()

        Dim files As FileInfo() = (New DirectoryInfo("N:\Archive").GetFiles())

        For Each f as FileInfo In files
                If f.Name.Length = 8 And String.IsNullOrEmpty(f.Extension) Then
                     '// Do something with the file
                End If
        Next

    End Sub

End Module

Open in new window

0
 

Author Comment

by:TEStack
ID: 34908870
How do I actually get it to delete the files?
0
 

Accepted Solution

by:
TEStack earned 0 total points
ID: 34909864
Imports System.IO

Module Module1
    Public Sub Main()

        Dim files As FileInfo() = (New DirectoryInfo("N:\usmPTP\PlatPar\3050Archive").GetFiles())

        For Each f As FileInfo In files
            If f.Name.Length = 8 And String.IsNullOrEmpty(f.Extension) Then
                f.Delete()

            End If
        Next

    End Sub


End Module
0
 

Author Closing Comment

by:TEStack
ID: 34941359
Thanks for the help.  I needed a little more detail.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question