Jump to content
Xtreme .Net Talk

How to get the files in vb.net in all directories/Code written but corrctns r ther


Recommended Posts

Posted (edited)

Note from moderator: Please use the

 and [code=visualbasic] tags and other formatting features to make your posts readable. Spelling and grammar help too.[/color]

This is in class file 
[code=visualbasic]Imports Microsoft.VisualBasic
Imports System.IO
Imports System.Collections.Generic
Public Class FileSrch
'' File Search Options
Public Enum FileSearchOptions
YearFirst
MonthFirst
DateinExtension
NormalFiles
End Enum

Public Shared Function GetSearchcriteria(ByVal DatetoSearchwith As Date, ByVal Filename As String, ByVal FileExtension As String, ByVal SearchOptions As FileSearchOptions)

Select Case SearchOptions
Case FileSearchOptions.YearFirst
Return String.Format("{0}_{1:yyyyMMdd}{2}", Filename, DatetoSearchwith, FileExtension)
Exit Select
Case FileSearchOptions.MonthFirst
Return String.Format("{0}_{1:yyyyMMdd}{2}", Filename, DatetoSearchwith, FileExtension)
Exit Select
Case FileSearchOptions.NormalFiles
Return String.Format(Filename + FileExtension)
Case FileSearchOptions.DateinExtension
Dim ExtensionFormat As String = (Convert.ToInt32(DatetoSearchwith.ToString("mm")).ToString() & DatetoSearchwith.ToString("dd"))
'' Month in Double Digit
If DatetoSearchwith.Month > 9 Then
ExtensionFormat = (DatetoSearchwith.ToString("mmm")(0) & DatetoSearchwith.ToString("dd"))
End If
Return String.Format("{0}.{1}", Filename, ExtensionFormat)
Case Else
Return String.Format("{0}.{1}", Filename, FileExtension)

End Select



End Function

Public Shared Function GetFiles(ByVal RootDirectory As DirectoryInfo, ByVal Filename As String, ByVal FileExtension As String, ByVal DatetoSearchwith As Date) As List(Of FileInfo)
Dim FiFiles As New List(Of FileInfo)
For Each DateOption As FileSrch.FileSearchOptions In [Enum].GetValues(GetType(FileSrch.FileSearchOptions))
FiFiles.AddRange(RootDirectory.GetFiles(FileSrch.GetSearchcriteria(DatetoSearchwith, Filename, FileExtension, DateOption), SearchOption.AllDirectories))
Next
Return FiFiles
End Function

End Class

This in Button Click
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim FileName As String
' Dim fileName As String = txtextension.Text.Trim().Substring(0, txtextension.Text.Trim().IndexOf("."))
If txtextension.Text.Contains("_") Then
FileName = txtextension.Text.Trim().Substring(0, txtextension.Text.Trim().IndexOf("_"))
Else
FileName = txtextension.Text.Trim().Substring(0, txtextension.Text.Trim().IndexOf("."))
End If
Dim extensionToSearch As String = Path.GetExtension(txtextension.Text)
'Dim Directory As String = Path.GetDirectoryName(txtpath.Text.Trim())
Dim Directory As New DirectoryInfo(txtpath.Text.Trim())

'Dim filess As String() = Directory.GetFiles(txtpath.Text, txtextension.Text, SearchOption.AllDirectories)
'For Each fiFile As String In filess
' Response.Write(fiFile & "</br>")
'Next

Dim SearchedFiles As List(Of FileInfo) = FileSrch.GetFiles(Directory, fileName, extensionToSearch, Now.Date())
'Dim searchedFiles As List(Of FileInfo) = GlobalStuff.GetFiles(directoryToSearch, "whateverfilename", extensionToSearch, DateTime.Now)

If (SearchedFiles IsNot Nothing AndAlso SearchedFiles.Count > 0) Then

For Each fi As FileInfo In SearchedFiles
Response.Write(fi.Name & "</br>")
Next

Else

Response.Write("No files found")

End If


End Sub

 

Now,we need to address the following things in that to complete req,

  1. If I enter filename_mmddyyyy.txt it should fetch only files having current date in their name in mmddyyyy format.For eg. If I give test_mmddyyyy.txt in txtextension it should return only files with test_10222010.txt and not thee ones with yyyymmdd and all�Right now I am getting all the files created in the current date including the one with ordinary test.txt and also test_20101022.txt ��.And the third thing filename.mdd everything is getting passed correctly but it is not fetching the file ..Again test,test_20101022.txt everything comes.It shouldn�t be like that it should only fetch files with .mdd (current date)�.if we give so��Hope u got�
  2. I implemented the normalfiles as u can see above.But in that there is one extra thing we need to address it is if I give ????.???,it should fetch only files like ajay.txt,siva.txt and so on�it should match exactly with the question mark .but what I am getting now is if I give ????.??? in text box I am getting files like nks.txt,nk.tt also�..all lesser files are coming �..? Should match exactly with letters also in extension.
    Hope you got��If we address the above things then we are done.
    And please help me in this final step..we are almost done�

 

In my above code i have two text boxes one for file path and other one for getting extension and also for gettting file names with diff search criteria.....What i do is i should implement file search which is like as mentioned below -

  1. it should search for all files if i enter *.*,only jpg if *.jpg, and files like nks.txt if i enter ???.???.......
  2. if i enter the filename alone without extn it should return me filenames that exist without extension in directories.
  3. the final thing is the date in filename where my file will be like filename_mmddyyyy.txt or filename_yyyymmdd.txt or filename.mdd.In first case it should return only files with current date in mmddyyyy format and txt extn....like taht....last case is mdd in extn...where if i enter filename.123,it sould get me files with jan 23(23 currnt date) and for months greater than 9 it is N for nov ,D for dec and so on.But in m,ytext box i will enter only file.mdd only .and also filename_mmddyyyy.jpg like that....

Edited by snarfblam
  • Leaders
Posted
Sorry, but I'm having an awfully difficult time following either the code or the question. I think you need to break them both down a bit. As far as the code is concerned, a little formatting would go a long way.
[sIGPIC]e[/sIGPIC]
Posted

Ok,i will format the code and put here.But the requirement is that the user can enter any search criteria like mentioned below,

 

The requirement is, the user can enter any wild card patterns combination in text box and we should get files as per that,Below I had mentioned some examples for more clarity��And in filename s with date there are three possible combinations�like filenamemmddyyyy.*(should fetch all files with todays date in mmddyyyy format and not files modified in todays date )�User can also enter like filename_yyyymmdd.txt where it should return the files with todays date in filename with the format yyyymmdd�.and last possible combination is mdd in extension like filename.mdd,it should get all files with filename.O26 (since today date is 26��.for months greater than 9 we denote using alphabets like instead of 10 for oct we use O and for November we use N and so on �.<9 means we use 1 for jan and 2 for feb and so on��

 

I had written the below code but its not fully done.There are some corrections to be made..One class file I create and another form in which in button click I write code.Please correct the code as per the requirements above.in the form there are two textboxes one for path and other for entering the search criteria�..

 

 

test - should fetch files with name test without any extn

test.* - all files with any extn

test.jpg - only jpg files..

testmmddyyyy - only files like test10262010 without any extension (here todays date is in filename in spd format )

test_yyyymmdd.txt - only files like test_20101026.txt files alone ......We should not get files modified in todays date

test.mdd - test.O26

t?st.?? - files like test.tx

????.??? - test.txt or extns with three characters.Rite now its fethcing files with < 4 characters in filename amnd <3 chrs in extn.Its wrong the pattern should exactly match with ? ...

 

 

 

See these are some combinations that i had shown.We should write code for this.File search should happen as mentioned above.Coding also i will format now and put it for you more clearly.One is the class file and other is web form where i place two text boxes one for path and other for search criteria.If u have any doubts please let me know,i will reply then and there but please help me.

Posted

'This is the class file where i write code for Search Criteria and Get files part

 

 

Imports Microsoft.VisualBasic

Imports System.IO

Imports System.Collections.Generic

Public Class FileSrch

 

'' File Search Options

Public Enum FileSearchOptions

 

YearFirst

MonthFirst

DateinExtension

NormalFiles

 

End Enum

 

Public Shared Function GetSearchcriteria(ByVal DatetoSearchwith As Date, ByVal Filename As String, ByVal FileExtension As String, ByVal SearchOptions As FileSearchOptions)

 

Select Case SearchOptions

 

Case FileSearchOptions.YearFirst

Return String.Format("{0}_{1:yyyyMMdd}{2}", Filename, DatetoSearchwith, FileExtension)

Exit Select

 

Case FileSearchOptions.MonthFirst

Return String.Format("{0}_{1:yyyyMMdd}{2}", Filename, DatetoSearchwith, FileExtension)

Exit Select

 

Case FileSearchOptions.NormalFiles

 

Return String.Format(Filename + FileExtension)

 

Case FileSearchOptions.DateinExtension

Dim ExtensionFormat As String = (Convert.ToInt32(DatetoSearchwith.ToString("mm")).ToString() & DatetoSearchwith.ToString("dd"))

'' Month in Double Digit

If DatetoSearchwith.Month > 9 Then

ExtensionFormat = (DatetoSearchwith.ToString("mmm")(0) & DatetoSearchwith.ToString("dd"))

End If

Return String.Format("{0}.{1}", Filename, ExtensionFormat)

Case Else

Return String.Format("{0}.{1}", Filename, FileExtension)

 

End Select

 

 

 

End Function

 

Public Shared Function GetFiles(ByVal RootDirectory As DirectoryInfo, ByVal Filename As String, ByVal FileExtension As String, ByVal DatetoSearchwith As Date) As List(Of FileInfo)

 

Dim FiFiles As New List(Of FileInfo)

For Each DateOption As FileSrch.FileSearchOptions In [Enum].GetValues(GetType(FileSrch.FileSearchOptions))

FiFiles.AddRange(RootDirectory.GetFiles(FileSrch.GetSearchcriteria(DatetoSearchwith, Filename, FileExtension, DateOption), SearchOption.AllDirectories))

 

Next

 

Return FiFiles

 

End Function

 

End Class

 

 

 

'As u can see above i use four cases one for normal files like *.*,*.jpg,???.?? and so on...Others for Date whcih can be like filenamemmddyyyy.txt (should return only txt files with todays date in filename in mentioned format),filename_mmddyyyyy (if the user gives this it should give files with todays date in filename with mmddyyyy format and without any extension) and last one is for mdd extension in filename where m stands for month and dd is date.User will feed fielname.mdd in textbox where m is month like 1 fo rJAN 2 for FEB and so on,If month becomes double digit then it should fetch files like filename.O26 -where O stands for october and todays date is 26.....

Posted

'This i write in my form in button click

 

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

 

Dim FileName As String

 

If txtextension.Text.Contains("_") Then

FileName = txtextension.Text.Trim().Substring(0, txtextension.Text.Trim().IndexOf("_"))

 

Else

 

FileName = txtextension.Text.Trim().Substring(0, txtextension.Text.Trim().IndexOf("."))

End If

 

Dim extensionToSearch As String = Path.GetExtension(txtextension.Text)

 

Dim Directory As New DirectoryInfo(txtpath.Text.Trim())

 

Dim SearchedFiles As List(Of FileInfo) = FileSrch.GetFiles(Directory, fileName, extensionToSearch, Now.Date())

 

If (SearchedFiles IsNot Nothing AndAlso SearchedFiles.Count > 0) Then

 

For Each fi As FileInfo In SearchedFiles

Response.Write(fi.Name & "</br>")

Next

 

Else

 

Response.Write("No files found")

 

End If

 

 

End Sub

 

 

 

 

In my above coding its not necessary that the user will give _ before filename so even that has to be changed.Can you please rewrite/modify this code to meet the mentioned requirement.If u have doubts,reply me ..I will just reply immediately.....

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...