copy *.tif files depending that the name exist in the dataBase

GREG_DORIAN

New member
Joined
Aug 31, 2015
Messages
4
Programming Experience
1-3
I need to copy files from one directory to another, depending on the existence of the file name in a table of SQL database.

For this I use the following code:

VB.NET:
[COLOR=#000000]Public Iterator Function ReadFileNamesFromDatabase() As IEnumerable(Of Integer)[/COLOR]
    Try
        Using connection As New SqlConnection("conection")
            Using cmd As SqlCommand = connection.CreateCommand()
                cmd.CommandType = CommandType.Text
                cmd.CommandText = "SELECT ID, PCOUNT FROM dbo.DocumentPics WHERE PCOUNT = 1"

                connection.Open()
                Using reader As SqlDataReader = cmd.ExecuteReader()
                    While reader.Read()
                        Yield reader.GetInt32(reader.GetOrdinal("ID"))
                    End While
                End Using

                connection.Close()
            End Using
        End Using
    Catch ex As Exception
        Throw ex [COLOR=#000000]    End Try[/COLOR]


then I use a an hashSet and Parallel.ForEach for copy in the fastest way an amount of 140.000 files depending if exist in the database
VB.NET:
Sub Main()    Dim source As New DirectoryInfo("C:\SourcePics\")
    Dim destination As New DirectoryInfo("C:\destinationPics\")
    Dim destinationPath As String




    Dim filesToBeCopied As New HashSet(Of Integer)(ReadFileNamesFromDatabase())




    Dim options As New ParallelOptions() With { _
         .MaxDegreeOfParallelism = 4 _
    }


    Parallel.ForEach(filesToBeCopied.SelectMany(
                     Function(fn) source.EnumerateFiles(fn)), options, _
                     Sub(fi)
                         destinationPath = Path.Combine(destination.FullName, Path.ChangeExtension(fi.Name, ".tif"))
                         fi.CopyTo(destinationPath, False)
                     End Sub)


    ' Keep the console window open in debug mode.
    Console.WriteLine("Processing complete. Press any key to exit.")
    Console.ReadKey()


End Sub

but in the parallel.ForEach does not copy anything? what is wrong w/ this code??

this is the first time I use parallel.Foreach..
 
What do you expect the result to be for DirectoryInfo.EnumerateFiles(integer) ? There is no EnumerateFiles that accept an integer as parameter, and the only one that has one parameter is searchPattern as String, which means you are implicitly converting the integer to a string. With no wildcard in searchPattern you will get at most one file that is named the same as that integer, for example "1", so a search is redundant for this.

Option Strict will help you avoid unnecessary data type mistakes like that.
 
hi JohnH yes! you got reason! I?ve been searching in this line "DirectoryInfo.EnumerateFiles(integer)" an Integer, but how can I convert that in String? fn.toString???
 
fn.toString???
Yes, but as I said, what would be the point of searching for a file "1" when the result can only be the single file named "1" (with no extension) ?
 
the extension are series.

Yes, but as I said, what would be the point of searching for a file "1" when the result can only be the single file named "1" (with no extension) ?
.....because the extension are series eg. "00012".01, "00013.02", "00014.02", "00046.01", "00047.01" etc.:eek3:
 
Let's say your ReadFileNamesFromDatabase method returns for example 1, then is there a file named "1" ?
 

Latest posts

Back
Top