Get Files in Directory having Current date in file name and also in ex

Get Files in Directory having Current date in file name and also in ex

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 7682 Views - Last Post: 28 October 2010 - 04:49 AM Rate Topic: -----

#1 ajaynks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 20-October 10

Get Files in Directory having Current date in file name and also in ex

Posted 20 October 2010 - 06:01 AM

Hi,can anyone say how to do below thing

i have to get the files in all the directories with date formats in file name and also in extension.
Some thing like new_mmddyyyy.txt / new_yyyymmdd.* like this....
Is This A Good Question/Topic? 0
  • +

Replies To: Get Files in Directory having Current date in file name and also in ex

#2 demausdauth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 177
  • View blog
  • Posts: 641
  • Joined: 03-February 10

Re: Get Files in Directory having Current date in file name and also in ex

Posted 20 October 2010 - 07:14 AM

MSDN is a wonderful resource ..... DirectoryInfo.GetFIles
Was This Post Helpful? 0
  • +
  • -

#3 ajaynks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 20-October 10

Re: Get Files in Directory having Current date in file name and also in ex

Posted 20 October 2010 - 08:51 AM

View Postajaynks, on 20 October 2010 - 05:01 AM, said:

Hi,can anyone say how to do below thing

i have to get the files in all the directories with date formats in file name and also in extension.
Some thing like new_mmddyyyy.txt / new_yyyymmdd.* like this....



I searched in the MSDN link that u suggested but i am not able to find anything related to the above thing over there.Can u or anyone help how to do that ...i want to get the files based on the date format mmddyyyy or yyyymmdd in the file name.....

View Postdemausdauth, on 20 October 2010 - 06:14 AM, said:

MSDN is a wonderful resource ..... DirectoryInfo.GetFIles



I searched in the MSDN link that u suggested but i am not able to find anything related to the above thing over there.Can u or anyone help how to do that ...i want to get the files based on the date format mmddyyyy or yyyymmdd in the file name.....Date can also be in extension like filename.mdd where m is month if it is jan corr to 1,2 for feb and so on...dd is the date
Was This Post Helpful? 0
  • +
  • -

#4 demausdauth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 177
  • View blog
  • Posts: 641
  • Joined: 03-February 10

Re: Get Files in Directory having Current date in file name and also in ex

Posted 20 October 2010 - 08:57 AM

Seriously??!!! I gave you the link right to what you need to do... Even the example does almost exactly what you want to do, very little needs to be changed. Try reading it again.
Was This Post Helpful? 0
  • +
  • -

#5 ajaynks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 20-October 10

Re: Get Files in Directory having Current date in file name and also in ex

Posted 20 October 2010 - 09:35 AM

View Postdemausdauth, on 20 October 2010 - 07:57 AM, said:

Seriously??!!! I gave you the link right to what you need to do... Even the example does almost exactly what you want to do, very little needs to be changed. Try reading it again.


Yes you are right,but i think we need to change the search pattern.I am doing the below thing which is working out for searching files with differnt extensions like file.txt,.jp,some.xml like that....But if the same thing i have to get the files if i feed like - upd_mmddyyyy.txt in the textbox (for wildcard entering) it has to get that particular file like upd20102010.txt like taht.For this what is the change i have to make in the below thing

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        ''Dim filePaths As String() = Directory.GetFiles(TextBox1.Text, "*." + TextBox2.Text, SearchOption.AllDirectories)
        Dim fileDateTime As String = DateTime.Now.ToString("yyyyMMdd")
        Dim filePaths As String() = Directory.GetFiles(TextBox1.Text, TextBox2.Text, SearchOption.AllDirectories)
        For Each filePath As String In filePaths

            Response.Write(filePath & "</br>")
        Next

    End Sub



In textbox1 i give the path and textbox2 i enter wildcards like *.jpg like that...

MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 21 October 2010 - 06:33 AM

Was This Post Helpful? 0
  • +
  • -

#6 demausdauth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 177
  • View blog
  • Posts: 641
  • Joined: 03-February 10

Re: Get Files in Directory having Current date in file name and also in ex

Posted 20 October 2010 - 09:59 AM

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
     ''Dim filePaths As String() = Directory.GetFiles(TextBox1.Text, "*." + TextBox2.Text, SearchOption.AllDirectories)
     Dim fileDateTime As String = DateTime.Now.ToString("yyyyMMdd")
     Dim filePaths As String() = Directory.GetFiles(TextBox1.Text, TextBox2.Text, SearchOption.AllDirectories)
     For Each filePath As String In filePaths

          Response.Write(filePath & "</br>")
     Next

End Sub



I am still not exactly sure what your inputs are but let me see if I have this straight:

TextBox1 is the file path
TextBox2 is the extensions that you want to search for

Somewhere in there is a defined file name based on some prefix and a date? You mentioned "upd_" and "new_".

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
     ''Dim filePaths As String() = Directory.GetFiles(TextBox1.Text, "*." + TextBox2.Text, SearchOption.AllDirectories)
     
     ' makes new date to be used in search pattern
     Dim searchDate As DateTime = new DateTime(2010, 10, 10)

     ' gets the last 3 characters of inputed extension to be used in the search pattern
     Dim fileExtension As String = TextBox2.Text.Trim().SubString(TextBox2.Text.Trim().Length - 3)

     ' gets the starting directory
     Dim filePath As String = TextBox1.Text

     'sets the search pattern to be used
     Dim searchString As String = String.Format("upd_{0:MMddyyyy}.{1}", searchDate, fileExtension)

     ' gets all files that match the search pattern
     Dim files As FileInfo() = Directory.GetFiles(filePath, searchString, SearchOption.AllDirectories)

     'loop through each file output the name
     For Each fiFile As FileInfo in files
          Response.Write(fiFile.Name & "</br>")
     Next 
     

End Sub


Was This Post Helpful? 0
  • +
  • -

#7 ajaynks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 20-October 10

Re: Get Files in Directory having Current date in file name and also in ex

Posted 21 October 2010 - 01:04 AM

View Postdemausdauth, on 20 October 2010 - 08:59 AM, said:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
     ''Dim filePaths As String() = Directory.GetFiles(TextBox1.Text, "*." + TextBox2.Text, SearchOption.AllDirectories)
     Dim fileDateTime As String = DateTime.Now.ToString("yyyyMMdd")
     Dim filePaths As String() = Directory.GetFiles(TextBox1.Text, TextBox2.Text, SearchOption.AllDirectories)
     For Each filePath As String In filePaths

          Response.Write(filePath & "</br>")
     Next

End Sub






I am still not exactly sure what your inputs are but let me see if I have this straight:

TextBox1 is the file path
TextBox2 is the extensions that you want to search for

Somewhere in there is a defined file name based on some prefix and a date? You mentioned "upd_" and "new_".

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
     ''Dim filePaths As String() = Directory.GetFiles(TextBox1.Text, "*." + TextBox2.Text, SearchOption.AllDirectories)
     
     ' makes new date to be used in search pattern
     Dim searchDate As DateTime = new DateTime(2010, 10, 10)

     ' gets the last 3 characters of inputed extension to be used in the search pattern
     Dim fileExtension As String = TextBox2.Text.Trim().SubString(TextBox2.Text.Trim().Length - 3)

     ' gets the starting directory
     Dim filePath As String = TextBox1.Text

     'sets the search pattern to be used
     Dim searchString As String = String.Format("upd_{0:MMddyyyy}.{1}", searchDate, fileExtension)

     ' gets all files that match the search pattern
     Dim files As FileInfo() = Directory.GetFiles(filePath, searchString, SearchOption.AllDirectories)

     'loop through each file output the name
     For Each fiFile As FileInfo in files
          Response.Write(fiFile.Name & "</br>")
     Next 
     

End Sub





Yes you are right.Upd and new are just for examples,whatever the name is the file will have date (in mmddyyyy or yyyymmdd and also in extension like .mdd where m stands for month denoted by number like 4 for april and if it is like double digit we denote by letter like for November we say N instead of 11.So it will come .N21 ....)So i have to get those files....This is my requirement.Clear ?...I will try the above thing and get back to you whether it works for me...
Was This Post Helpful? 0
  • +
  • -

#8 ajaynks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 20-October 10

Re: Get Files in Directory having Current date in file name and also in ex

Posted 21 October 2010 - 02:46 AM

View Postdemausdauth, on 20 October 2010 - 08:59 AM, said:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
     ''Dim filePaths As String() = Directory.GetFiles(TextBox1.Text, "*." + TextBox2.Text, SearchOption.AllDirectories)
     Dim fileDateTime As String = DateTime.Now.ToString("yyyyMMdd")
     Dim filePaths As String() = Directory.GetFiles(TextBox1.Text, TextBox2.Text, SearchOption.AllDirectories)
     For Each filePath As String In filePaths

          Response.Write(filePath & "</br>")
     Next

End Sub



I am still not exactly sure what your inputs are but let me see if I have this straight:

TextBox1 is the file path
TextBox2 is the extensions that you want to search for

Somewhere in there is a defined file name based on some prefix and a date? You mentioned "upd_" and "new_".

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
     ''Dim filePaths As String() = Directory.GetFiles(TextBox1.Text, "*." + TextBox2.Text, SearchOption.AllDirectories)
     
     ' makes new date to be used in search pattern
     Dim searchDate As DateTime = new DateTime(2010, 10, 10)

     ' gets the last 3 characters of inputed extension to be used in the search pattern
     Dim fileExtension As String = TextBox2.Text.Trim().SubString(TextBox2.Text.Trim().Length - 3)

     ' gets the starting directory
     Dim filePath As String = TextBox1.Text

     'sets the search pattern to be used
     Dim searchString As String = String.Format("upd_{0:MMddyyyy}.{1}", searchDate, fileExtension)

     ' gets all files that match the search pattern
     Dim files As FileInfo() = Directory.GetFiles(filePath, searchString, SearchOption.AllDirectories)

     'loop through each file output the name
     For Each fiFile As FileInfo in files
          Response.Write(fiFile.Name & "</br>")
     Next 
     

End Sub




Hi,i tested the above code.there it just hardcodes date as 20102010 ....the filename and date can be anything .....The requirement is clearly there in this link - http://www.febooti.c...-wildcard-mask/ and added to the req in this link the date filtering should also happen which i had explained in the above reply of mine.Can you please help....
Was This Post Helpful? 0
  • +
  • -

#9 ajaynks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 20-October 10

Re: Get Files in Directory having Current date in file name and also in ex

Posted 21 October 2010 - 05:38 AM

View Postdemausdauth, on 20 October 2010 - 08:59 AM, said:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
     ''Dim filePaths As String() = Directory.GetFiles(TextBox1.Text, "*." + TextBox2.Text, SearchOption.AllDirectories)
     Dim fileDateTime As String = DateTime.Now.ToString("yyyyMMdd")
     Dim filePaths As String() = Directory.GetFiles(TextBox1.Text, TextBox2.Text, SearchOption.AllDirectories)
     For Each filePath As String In filePaths

          Response.Write(filePath & "</br>")
     Next

End Sub



I am still not exactly sure what your inputs are but let me see if I have this straight:

TextBox1 is the file path
TextBox2 is the extensions that you want to search for

Somewhere in there is a defined file name based on some prefix and a date? You mentioned "upd_" and "new_".

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
     ''Dim filePaths As String() = Directory.GetFiles(TextBox1.Text, "*." + TextBox2.Text, SearchOption.AllDirectories)
     
     ' makes new date to be used in search pattern
     Dim searchDate As DateTime = new DateTime(2010, 10, 10)

     ' gets the last 3 characters of inputed extension to be used in the search pattern
     Dim fileExtension As String = TextBox2.Text.Trim().SubString(TextBox2.Text.Trim().Length - 3)

     ' gets the starting directory
     Dim filePath As String = TextBox1.Text

     'sets the search pattern to be used
     Dim searchString As String = String.Format("upd_{0:MMddyyyy}.{1}", searchDate, fileExtension)

     ' gets all files that match the search pattern
     Dim files As FileInfo() = Directory.GetFiles(filePath, searchString, SearchOption.AllDirectories)

     'loop through each file output the name
     For Each fiFile As FileInfo in files
          Response.Write(fiFile.Name & "</br>")
     Next 
     

End Sub




And when it comes to getting file names with dates the user will feed filename_mmddyyyy or filename_yyyymmdd or filename.mdd and it should fetch all the files in the current date...this is the requirement for date masking
Was This Post Helpful? 0
  • +
  • -

#10 ajaynks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 20-October 10

Re: Get Files in Directory having Current date in file name and also in ex

Posted 21 October 2010 - 06:26 AM

View Postajaynks, on 21 October 2010 - 12:04 AM, said:

View Postdemausdauth, on 20 October 2010 - 08:59 AM, said:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
     ''Dim filePaths As String() = Directory.GetFiles(TextBox1.Text, "*." + TextBox2.Text, SearchOption.AllDirectories)
     Dim fileDateTime As String = DateTime.Now.ToString("yyyyMMdd")
     Dim filePaths As String() = Directory.GetFiles(TextBox1.Text, TextBox2.Text, SearchOption.AllDirectories)
     For Each filePath As String In filePaths

          Response.Write(filePath & "</br>")
     Next

End Sub






I am still not exactly sure what your inputs are but let me see if I have this straight:

TextBox1 is the file path
TextBox2 is the extensions that you want to search for

Somewhere in there is a defined file name based on some prefix and a date? You mentioned "upd_" and "new_".

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
     ''Dim filePaths As String() = Directory.GetFiles(TextBox1.Text, "*." + TextBox2.Text, SearchOption.AllDirectories)
     
     ' makes new date to be used in search pattern
     Dim searchDate As DateTime = new DateTime(2010, 10, 10)

     ' gets the last 3 characters of inputed extension to be used in the search pattern
     Dim fileExtension As String = TextBox2.Text.Trim().SubString(TextBox2.Text.Trim().Length - 3)

     ' gets the starting directory
     Dim filePath As String = TextBox1.Text

     'sets the search pattern to be used
     Dim searchString As String = String.Format("upd_{0:MMddyyyy}.{1}", searchDate, fileExtension)

     ' gets all files that match the search pattern
     Dim files As FileInfo() = Directory.GetFiles(filePath, searchString, SearchOption.AllDirectories)

     'loop through each file output the name
     For Each fiFile As FileInfo in files
          Response.Write(fiFile.Name & "</br>")
     Next 
     

End Sub





Yes you are right.Upd and new are just for examples,whatever the name is the file will have date (in mmddyyyy or yyyymmdd and also in extension like .mdd where m stands for month denoted by number like 4 for april and if it is like double digit we denote by letter like for November we say N instead of 11.So it will come .N21 ....)So i have to get those files....This is my requirement.Clear ?...I will try the above thing and get back to you whether it works for me...



And finally to be more clear...if we give filename_mmddyyyy.txt it should get files with currentdate in the mentioned format in filename.......suppose tody it should fetch filename_10212010.txt....
Was This Post Helpful? 0
  • +
  • -

#11 demausdauth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 177
  • View blog
  • Posts: 641
  • Joined: 03-February 10

Re: Get Files in Directory having Current date in file name and also in ex

Posted 21 October 2010 - 07:22 AM

Here is what I came up with...

First I created a separate class called GlobalStuff.

Public Class GlobalStuff

    Public Enum DateSearchOptions
        YearFirst
        MonthFirst
        DateInExtension
    End Enum

    Public Shared Function GetSearchCriteria(ByVal dateToSearchWith As DateTime, ByVal fileNamePrefix As String, ByVal fileNameExtension As String, ByVal searchOptions As DateSearchOptions) As String
        Select Case searchOptions
            Case DateSearchOptions.YearFirst
                Return String.Format("{0}_{1:yyyyMMdd}.{2}", fileNamePrefix, dateToSearchWith, fileNameExtension)

            Case DateSearchOptions.MonthFirst
                Return String.Format("{0}_{1:MMddyyyy}.{2}", fileNamePrefix, dateToSearchWith, fileNameExtension)

            Case DateSearchOptions.DateInExtension

                'Set this as default since this is  9 out of 12 months 
                'need to get the Month number and day number: date = mm/dd/yyyy 04/02/2010 fileNamePrefix.402
                Dim extensionFormat As String = (Convert.ToInt32(dateToSearchWith.ToString("MM")).ToString() & dateToSearchWith.ToString("dd"))

                'Determine if the Month is double digit
                If dateToSearchWith.Month > 9 Then
                    'need to get the first letter of the 3-letter month abbreviation: date = mm/dd/yyyy 11/02/2010 need to get fileNamePrefix.N02
                    extensionFormat = (dateToSearchWith.ToString("MMM")(0) & dateToSearchWith.ToString("dd"))
                End If

                Return String.Format("{0}.{1}", fileNamePrefix, extensionFormat)

            Case Else
                Return String.Format("{0}.{1}", fileNamePrefix, fileNameExtension)

        End Select

    End Function

    Public Shared Function GetFiles(ByVal rootDirectory As DirectoryInfo, ByVal fileNamePrefix As String, ByVal fileExtension As String, ByVal dateToSearchWith As DateTime) As List(Of FileInfo)

        'make the return value 
        Dim fiFiles As New List(Of FileInfo)


        'loop through the date options

        For Each dateOption As GlobalStuff.DateSearchOptions In [Enum].GetValues(GetType(GlobalStuff.DateSearchOptions))

            'perform the search for each iteration of the DateSearOption
            ' add any results to fiFiles
            fiFiles.AddRange(rootDirectory.GetFiles(GlobalStuff.GetSearchCriteria(dateToSearchWith, fileNamePrefix, fileExtension, dateOption), SearchOption.AllDirectories))

        Next
        Return fiFiles

    End Function
End Class



The Enum DateSearchOption is to help figure out which version of search criteria we want to do. The function GetSearchCriteria will build the necessary search from the passed values based on the DateSearchOption. The GetFiles will build a list of files based on the passed values.

And here is how you would use this class and its functions.

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

        Dim directoryToSearch As New DirectoryInfo(TextBox1.Text.Trim())
        Dim extensionToSearch As String = TextBox2.Text.Trim().SubString(TextBox2.Text.Trim().Length - 3)

        Dim fileNamePrefixToSearch As String = "whateverfilename" ' not sure where you get this??

        Dim searchedFiles As List(Of FileInfo) = GlobalStuff.GetFiles(directoryToSearch, "whateverfilename", extensionToSearch, DateTime.Now)

        'check to make sure that searchedFiles contains anyting
        If (searchedFiles IsNot Nothing AndAlso searchedFiles.Count > 0) Then

            'loop and display filename
            For Each fi As FileInfo In searchedFiles
                Response.Write(fi.Name)
            Next

        Else
            Response.Write("No files found")
        End If

    End Sub



Quote

And when it comes to getting file names with dates the user will feed filename_mmddyyyy or filename_yyyymmdd or filename.mdd

The assumption made in this code is that the file name formats are:

fileNamePrefix_dateToSearch.fileExtension
or
fileNamePrefix.dateToSearch
Was This Post Helpful? 0
  • +
  • -

#12 ajaynks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 20-October 10

Re: Get Files in Directory having Current date in file name and also in ex

Posted 21 October 2010 - 08:02 AM

View Postdemausdauth, on 21 October 2010 - 06:22 AM, said:

Here is what I came up with...

First I created a separate class called GlobalStuff.

Public Class GlobalStuff

    Public Enum DateSearchOptions
        YearFirst
        MonthFirst
        DateInExtension
    End Enum

    Public Shared Function GetSearchCriteria(ByVal dateToSearchWith As DateTime, ByVal fileNamePrefix As String, ByVal fileNameExtension As String, ByVal searchOptions As DateSearchOptions) As String
        Select Case searchOptions
            Case DateSearchOptions.YearFirst
                Return String.Format("{0}_{1:yyyyMMdd}.{2}", fileNamePrefix, dateToSearchWith, fileNameExtension)

            Case DateSearchOptions.MonthFirst
                Return String.Format("{0}_{1:MMddyyyy}.{2}", fileNamePrefix, dateToSearchWith, fileNameExtension)

            Case DateSearchOptions.DateInExtension

                'Set this as default since this is  9 out of 12 months 
                'need to get the Month number and day number: date = mm/dd/yyyy 04/02/2010 fileNamePrefix.402
                Dim extensionFormat As String = (Convert.ToInt32(dateToSearchWith.ToString("MM")).ToString() & dateToSearchWith.ToString("dd"))

                'Determine if the Month is double digit
                If dateToSearchWith.Month > 9 Then
                    'need to get the first letter of the 3-letter month abbreviation: date = mm/dd/yyyy 11/02/2010 need to get fileNamePrefix.N02
                    extensionFormat = (dateToSearchWith.ToString("MMM")(0) & dateToSearchWith.ToString("dd"))
                End If

                Return String.Format("{0}.{1}", fileNamePrefix, extensionFormat)

            Case Else
                Return String.Format("{0}.{1}", fileNamePrefix, fileNameExtension)

        End Select

    End Function

    Public Shared Function GetFiles(ByVal rootDirectory As DirectoryInfo, ByVal fileNamePrefix As String, ByVal fileExtension As String, ByVal dateToSearchWith As DateTime) As List(Of FileInfo)

        'make the return value 
        Dim fiFiles As New List(Of FileInfo)


        'loop through the date options

        For Each dateOption As GlobalStuff.DateSearchOptions In [Enum].GetValues(GetType(GlobalStuff.DateSearchOptions))

            'perform the search for each iteration of the DateSearOption
            ' add any results to fiFiles
            fiFiles.AddRange(rootDirectory.GetFiles(GlobalStuff.GetSearchCriteria(dateToSearchWith, fileNamePrefix, fileExtension, dateOption), SearchOption.AllDirectories))

        Next
        Return fiFiles

    End Function
End Class



The Enum DateSearchOption is to help figure out which version of search criteria we want to do. The function GetSearchCriteria will build the necessary search from the passed values based on the DateSearchOption. The GetFiles will build a list of files based on the passed values.

And here is how you would use this class and its functions.

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

        Dim directoryToSearch As New DirectoryInfo(TextBox1.Text.Trim())
        Dim extensionToSearch As String = TextBox2.Text.Trim().SubString(TextBox2.Text.Trim().Length - 3)

        Dim fileNamePrefixToSearch As String = "whateverfilename" ' not sure where you get this??

        Dim searchedFiles As List(Of FileInfo) = GlobalStuff.GetFiles(directoryToSearch, "whateverfilename", extensionToSearch, DateTime.Now)

        'check to make sure that searchedFiles contains anyting
        If (searchedFiles IsNot Nothing AndAlso searchedFiles.Count > 0) Then

            'loop and display filename
            For Each fi As FileInfo In searchedFiles
                Response.Write(fi.Name)
            Next

        Else
            Response.Write("No files found")
        End If

    End Sub



Quote

And when it comes to getting file names with dates the user will feed filename_mmddyyyy or filename_yyyymmdd or filename.mdd

The assumption made in this code is that the file name formats are:

fileNamePrefix_dateToSearch.fileExtension
or
fileNamePrefix.dateToSearch




Yes this is part of my requirement.Thanks,but how to include normal file search in the above code..I did it separately...like normal below code will do that.....saying getFiles...() ...it should get all files if i give *.* ,*.jpg - only jpg files and so on.Just wild card normal usage...How and where to incorporate this functionality in the above code.....?And also if i give ???.?? or so it should fetch files exactly like nks.gg or ttt.gg like that.....? stands for single char ....????.??? means somethinglikie ajay.jpg and so on...Can u show me how to implement this also in the above code....this is straight forward na,the only thing where we need to twist is in my code if i give ????.??? it should actually fetch only ajay.txt....but its also fetching files with three letters like aja.txt also.That should be avoided...
Was This Post Helpful? 0
  • +
  • -

#13 ajaynks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 20-October 10

Re: Get Files in Directory having Current date in file name and also in ex

Posted 21 October 2010 - 08:13 AM

View Postdemausdauth, on 21 October 2010 - 06:22 AM, said:

Here is what I came up with...

First I created a separate class called GlobalStuff.

Public Class GlobalStuff

    Public Enum DateSearchOptions
        YearFirst
        MonthFirst
        DateInExtension
    End Enum

    Public Shared Function GetSearchCriteria(ByVal dateToSearchWith As DateTime, ByVal fileNamePrefix As String, ByVal fileNameExtension As String, ByVal searchOptions As DateSearchOptions) As String
        Select Case searchOptions
            Case DateSearchOptions.YearFirst
                Return String.Format("{0}_{1:yyyyMMdd}.{2}", fileNamePrefix, dateToSearchWith, fileNameExtension)

            Case DateSearchOptions.MonthFirst
                Return String.Format("{0}_{1:MMddyyyy}.{2}", fileNamePrefix, dateToSearchWith, fileNameExtension)

            Case DateSearchOptions.DateInExtension

                'Set this as default since this is  9 out of 12 months 
                'need to get the Month number and day number: date = mm/dd/yyyy 04/02/2010 fileNamePrefix.402
                Dim extensionFormat As String = (Convert.ToInt32(dateToSearchWith.ToString("MM")).ToString() & dateToSearchWith.ToString("dd"))

                'Determine if the Month is double digit
                If dateToSearchWith.Month > 9 Then
                    'need to get the first letter of the 3-letter month abbreviation: date = mm/dd/yyyy 11/02/2010 need to get fileNamePrefix.N02
                    extensionFormat = (dateToSearchWith.ToString("MMM")(0) & dateToSearchWith.ToString("dd"))
                End If

                Return String.Format("{0}.{1}", fileNamePrefix, extensionFormat)

            Case Else
                Return String.Format("{0}.{1}", fileNamePrefix, fileNameExtension)

        End Select

    End Function

    Public Shared Function GetFiles(ByVal rootDirectory As DirectoryInfo, ByVal fileNamePrefix As String, ByVal fileExtension As String, ByVal dateToSearchWith As DateTime) As List(Of FileInfo)

        'make the return value 
        Dim fiFiles As New List(Of FileInfo)


        'loop through the date options

        For Each dateOption As GlobalStuff.DateSearchOptions In [Enum].GetValues(GetType(GlobalStuff.DateSearchOptions))

            'perform the search for each iteration of the DateSearOption
            ' add any results to fiFiles
            fiFiles.AddRange(rootDirectory.GetFiles(GlobalStuff.GetSearchCriteria(dateToSearchWith, fileNamePrefix, fileExtension, dateOption), SearchOption.AllDirectories))

        Next
        Return fiFiles

    End Function
End Class



The Enum DateSearchOption is to help figure out which version of search criteria we want to do. The function GetSearchCriteria will build the necessary search from the passed values based on the DateSearchOption. The GetFiles will build a list of files based on the passed values.

And here is how you would use this class and its functions.

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

        Dim directoryToSearch As New DirectoryInfo(TextBox1.Text.Trim())
        Dim extensionToSearch As String = TextBox2.Text.Trim().SubString(TextBox2.Text.Trim().Length - 3)

        Dim fileNamePrefixToSearch As String = "whateverfilename" ' not sure where you get this??

        Dim searchedFiles As List(Of FileInfo) = GlobalStuff.GetFiles(directoryToSearch, "whateverfilename", extensionToSearch, DateTime.Now)

        'check to make sure that searchedFiles contains anyting
        If (searchedFiles IsNot Nothing AndAlso searchedFiles.Count > 0) Then

            'loop and display filename
            For Each fi As FileInfo In searchedFiles
                Response.Write(fi.Name)
            Next

        Else
            Response.Write("No files found")
        End If

    End Sub



Quote

And when it comes to getting file names with dates the user will feed filename_mmddyyyy or filename_yyyymmdd or filename.mdd

The assumption made in this code is that the file name formats are:

fileNamePrefix_dateToSearch.fileExtension
or
fileNamePrefix.dateToSearch



The above thing what i mentioned shortly is in the above link which i mentioned..http://www.febooti.com/products/automation-workshop/online-help/file-wildcard-mask/
Was This Post Helpful? 0
  • +
  • -

#14 demausdauth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 177
  • View blog
  • Posts: 641
  • Joined: 03-February 10

Re: Get Files in Directory having Current date in file name and also in ex

Posted 21 October 2010 - 08:48 AM

If I understand what you are asking you could add another DateSearchOption for example NormalFiles. Doing this will cause the Else condition to be executed inside GetSearchCriteria() and this will build a search criteria that is only the specified fileName and specified fileExtension. So if you enter *.jpg into TextBox2, then what we need to do is split this into the fileNamePrefix and the fileNameExtension.

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

    Dim directoryToSearch As New DirectoryInfo(TextBox1.Text.Trim())
    Dim extensionToSearch As String = TextBox2.Text.Trim().SubString(TextBox2.Text.Trim().Length - 3)

    'Dim fileNamePrefixToSearch As String = "whateverfilename" ' not sure where you get this??
    Dim fileNamePrefixToSearch As String = TextBox2.Text.Trim().SubString(0, TextBox2.Text.Trim().IndexOf("."))
    Dim searchedFiles As List(Of FileInfo) = GlobalStuff.GetFiles(directoryToSearch, "whateverfilename", extensionToSearch, DateTime.Now)

    'check to make sure that searchedFiles contains anyting
    If (searchedFiles IsNot Nothing AndAlso searchedFiles.Count > 0) Then

        'loop and display filename
        For Each fi As FileInfo In searchedFiles
            Response.Write(fi.Name)
        Next

    Else
        Response.Write("No files found")
    End If

End Sub


Was This Post Helpful? 0
  • +
  • -

#15 ajaynks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 20-October 10

Re: Get Files in Directory having Current date in file name and also in ex

Posted 21 October 2010 - 09:11 AM

View Postdemausdauth, on 21 October 2010 - 07:48 AM, said:

If I understand what you are asking you could add another DateSearchOption for example NormalFiles. Doing this will cause the Else condition to be executed inside GetSearchCriteria() and this will build a search criteria that is only the specified fileName and specified fileExtension. So if you enter *.jpg into TextBox2, then what we need to do is split this into the fileNamePrefix and the fileNameExtension.

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

    Dim directoryToSearch As New DirectoryInfo(TextBox1.Text.Trim())
    Dim extensionToSearch As String = TextBox2.Text.Trim().SubString(TextBox2.Text.Trim().Length - 3)

    'Dim fileNamePrefixToSearch As String = "whateverfilename" ' not sure where you get this??
    Dim fileNamePrefixToSearch As String = TextBox2.Text.Trim().SubString(0, TextBox2.Text.Trim().IndexOf("."))
    Dim searchedFiles As List(Of FileInfo) = GlobalStuff.GetFiles(directoryToSearch, "whateverfilename", extensionToSearch, DateTime.Now)

    'check to make sure that searchedFiles contains anyting
    If (searchedFiles IsNot Nothing AndAlso searchedFiles.Count > 0) Then

        'loop and display filename
        For Each fi As FileInfo In searchedFiles
            Response.Write(fi.Name)
        Next

    Else
        Response.Write("No files found")
    End If

End Sub





I think u r asking me to implement that normal files fxn inside getsearchcriteria as one case ? am i right ? ok anyway i will try implementing the same in my home tonight and get back to u......
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2