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:

Code:
[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
Code:
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..
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,412
Location
Norway
Programming Experience
10+
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.
 

GREG_DORIAN

New member
Joined
Aug 31, 2015
Messages
4
Programming Experience
1-3
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???
 

GREG_DORIAN

New member
Joined
Aug 31, 2015
Messages
4
Programming Experience
1-3
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:
 

GREG_DORIAN

New member
Joined
Aug 31, 2015
Messages
4
Programming Experience
1-3
.....because the extension are series eg. "00012".01, "00013.02", "00014.02", "00046.01", "00047.01" etc.:eek3:
and in the data base id has the following numbers "12" "13", "14", "46", "47" which correspond to the file name
 
Top Bottom