Link to home
Start Free TrialLog in
Avatar of TEStack
TEStack

asked on

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

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
Avatar of jasonduan
jasonduan
Flag of United States of America image

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
Avatar of TEStack
TEStack

ASKER

I'm afraid it's not easy for me.  I'm pretty much a rookie and don't know any C#.
Avatar of Carl Tawn
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

Avatar of TEStack

ASKER

Does anyone else know how this might be done in vb.net?
Avatar of TEStack

ASKER

Disregard last post I hadn't updated my screen.
Avatar of TEStack

ASKER

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.
Can you post your code precisely as you have it? Including the "Imports" line.
Avatar of TEStack

ASKER

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
What version of the .net framework are you using? Linq is only available with 3.0 onwards.
Avatar of TEStack

ASKER

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.
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

Avatar of TEStack

ASKER

Do I leave Imports System.Linq out?  How should f be declared?
You leave it out. System.Linq isn't available in VS2005.
Avatar of TEStack

ASKER

You left it in your example above.  How should F be declared?
SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TEStack

ASKER

How do I actually get it to delete the files?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TEStack

ASKER

Thanks for the help.  I needed a little more detail.