Solved

looping to a folder list

Posted on 2011-03-01
14
268 Views
Last Modified: 2012-05-11
I have a folder that list the name of GarageID for eg.
In c:\Process, the files in it are GarageID's
6666.xls
6667.xls
6668.xls
so it is an array..

I d lke to use a Sql statement
that something like  "Select xxx,sss, vvv from table where GarageId = 'filename' in that foder'
How is that possible?
Wjat code will implement reading that array(loop) adn constructing the statement? Thanks
0
Comment
Question by:zachvaldez
  • 6
  • 3
  • 2
  • +2
14 Comments
 
LVL 40

Expert Comment

by:Sharath
ID: 35014509
Are you looking for a t-SQL solution for this? i.e. Reading the file names from a folder on a drive?
0
 

Author Comment

by:zachvaldez
ID: 35014535
vb.net because it has to be done inline code... any ideas if stored proc can provide the logic/
0
 
LVL 40

Expert Comment

by:Sharath
ID: 35014733
I asked you because you mentioned that you want to query using a SQL statement. If you are looking for SQL statement, let me know. will try.
0
 
LVL 37

Expert Comment

by:ValentinoV
ID: 35014861
If you're looking at looping through files while applying a filter in VB.NET, have a look at the EnumerateFiles method in the System.IO.Disrectory class: http://msdn.microsoft.com/en-us/library/dd413233.aspx

It allows you to return a collection of file names while applying a search pattern.

0
 
LVL 18

Expert Comment

by:carlnorrbom
ID: 35016905
Hi,

Don't get it... Are you looking to pick up each file in the array and do something with it or what? How is a sql statement relevant when files are stored in a folder? To iterate over the files in the folder you simply do something like:
        Dim dir As DirectoryInfo = New DirectoryInfo("C:\Process")
        For Each f As FileInfo In dir.GetFiles("*.xls")
            'Here you have a whole bunch of properties to play around with!
        Next

Open in new window


/Carl.
0
 

Author Comment

by:zachvaldez
ID: 35017236
Something like I want to store in a variable the filename as I loop
0
 
LVL 18

Accepted Solution

by:
carlnorrbom earned 175 total points
ID: 35017821
Hi,

Well to put the filename into a sql table you can do something like:
Imports System.IO
Imports System.Data.SqlClient

Partial Class Default14
    Inherits System.Web.UI.Page

    Private Sub DoFileStuff()

        Dim _conn As New SqlConnection(ConfigurationManager.ConnectionStrings("YourConnectionStringGoesHere").ConnectionString)
        Dim strInsertGarageID As String = "INSERT INTO tblGarageID (GarageID) VALUES (@GarageID)"
        Dim sqlInsertGarageID As New SqlCommand(strInsertGarageID, _conn)
        Dim dir As DirectoryInfo = New DirectoryInfo("C:\Process")
        If Not _conn.State = Data.ConnectionState.Open Then
            _conn.Open()
        End If
        Try
            For Each f As FileInfo In dir.GetFiles("*.xls")
                sqlInsertGarageID.Parameters.Clear()
                sqlInsertGarageID.Parameters.AddWithValue("@GarageID", f.Name.ToString()) 'Will put the filename into the sql table
                sqlInsertGarageID.ExecuteNonQuery()
            Next
        Catch ex As Exception
            'TODO: Implement error handling logic
        Finally
            If Not _conn.State = Data.ConnectionState.Closed Then
                _conn.Close()
            End If
        End Try


    End Sub
End Class

Open in new window


And then construct your sql statements something like:
SELECT Table.xxx, Table.sss, Table.vvv FROM Table INNER JOIN tblGarageID ON Table.GarageId=tblGarageID.GarageID ORDER BY tblGarageID.GarageID

Open in new window


/Carl.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 175 total points
ID: 35021069
Are you looking for something like below?



Dim Root As New DirectoryInfo("C:\")
Dim Files As FileInfo() = Root.GetFiles("*.*")

Dim dbadp As New SqlDataAdapter("", "connectionstring")
For each File in Files
   dbadp.SelectCommand.CommandText = "Select * From Table Where FileName='" & File & "'"
   dbadp.Fill(datatable)
   ...
Next
dbadp.Dispose

Open in new window

0
 

Author Comment

by:zachvaldez
ID: 35023987
In the loop,I am thinking to run a stored proc by passing the file name jn the where clause
Something like";select email from table where garageid is the filename
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 175 total points
ID: 35035362
Then try this



Dim Root As New DirectoryInfo("C:\")
Dim Files As FileInfo() = Root.GetFiles("*.*")

Dim dbadp As New SqlDataAdapter("", "connectionstring")
dbadp.SelectCommand.CommandType = CommandType.StoredProcedure
dbadp.SelectCommand.CommandText = "stored procedure name"
For each File in Files
   dbadp.SelectCommand.Parameters.AddWithValue("@filename", File)
   dbadp.Fill(datatable)
   dbadp.Parameters.Clear()
   ...
Next
dbadp.Dispose

Open in new window

0
 

Author Comment

by:zachvaldez
ID: 35039063
I am getting
'Parameters is not a member of  System.Data.SQLClient.SQLDataAdapter

in line

 dbadp.Parameters.Clear()

Here's what Im trying to do.

Inside the For Each loop
I pass the filename without the extenxion.That will be the ID that I pass in the stored proc.
My SP statement is something like..Select email from xtablle where GarageId = '@fileId'
What ever is returned I stored it in variable
sMsgTo:(email)
I already got the email routine. ...
So every loop I get the email in a variable.

so does this proposition fit in the loop?
0
 
LVL 18

Assisted Solution

by:carlnorrbom
carlnorrbom earned 175 total points
ID: 35039504
Hi,

You need to change it to:
dbadp.SelectParameters.Clear()

Open in new window


/Carl.
0
 

Author Comment

by:zachvaldez
ID: 35040070
For each File in Files
   dbadp.SelectCommand.Parameters.AddWithValue("@filename", File)
   dbadp.Fill(datatable)
  dbadp.SelectParameters.Clear()
Next

where will the return value stored here in the loop
0
 

Author Closing Comment

by:zachvaldez
ID: 35083219
Well  done!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now