0

I have the following code to create a directory, the task accepts a recordcount and every time the recordcount reaches the required number, say 1000 records, a new directory is created. If the task is run a second time it will add another 1000 records to the existing directories, I want it to skip these existing directories and create a new one. I've tried adding various ifexists, but mess it up all the time, any help would be appreciated

Public Sub Main()

    Dim SourceDirectory As String = "E:\Data"
    Dim TargetDirectory As String = "E:\CN"
    Dim FileExtensionsToProcess As String = "CON*.pdf"
    Dim FileCounter As Integer = 0
    Dim FolderName As Integer = 1
    Dim recordcount As Integer
    recordcount = CInt(Dts.Variables("RecordCount").Value)

    For Each FileName As String In System.IO.Directory.GetFiles(SourceDirectory, FileExtensionsToProcess)
        Dim FileOnly As String = System.IO.Path.GetFileName(FileName)
        Try
            If Not IO.Directory.Exists(IO.Path.Combine(TargetDirectory, FolderName.ToString())) Then
                IO.Directory.CreateDirectory(IO.Path.Combine(TargetDirectory, FolderName.ToString()))
            End If
            IO.File.Move(FileName, IO.Path.Combine(TargetDirectory, IO.Path.Combine(FolderName.ToString(), FileOnly)))
        Catch
        End Try
        FileCounter += 1
        If (FileCounter Mod recordcount) = 0 Then
            FolderName += 1
        End If
    Next
    Dts.TaskResult = ScriptResults.Success
End Sub
2
  • Set recordcount to a hard-coded small number e.g. 5, don´t swallow a possible exception in try...catch, set a breakpoint in the for each loop, check your counter variables and if statement, debug your code, fix errors. Commented Oct 18, 2016 at 7:12
  • You check if the target directory exists... But don't bother to count the number of files in it, and then move the files anyway. Commented Oct 18, 2016 at 9:07

1 Answer 1

0

Okay. The full solution is shown below and then I will explain some of it.

Public Sub Main()
    Dim SourceDirectory As String = "E:\Data"
    Dim TargetDirectory As String = "E:\CN"
    Dim FileExtensionsToProcess As String = "CON*.pdf"
    Dim FileCounter As Integer = 0
    Dim FolderName As Integer = 1
    Dim recordcount As Integer = CInt(Dts.Variables("RecordCount").Value)

    Dim targetDir As String = SetOutputFolder(TargetDirectory, FolderName, recordcount)

    For Each FileName As String In Directory.GetFiles(SourceDirectory, FileExtensionsToProcess)
        Dim FileOnly As String = Path.GetFileName(FileName)

        'Try - Leave this out to observe any exceptions, then add handling when you see any
        ' Check for file name conflicts before moving
        File.Move(FileName, Path.Combine(targetDir, FileOnly))  

        'Catch
        'End Try
        FileCounter += 1
        If FileCounter >= recordcount Then
            FolderName += 1
            targetDir = SetOutputFolder(TargetDirectory, FolderName, recordcount)
            FileCounter = Directory.GetFiles(targetDir).Count
        End If
    Next
End Sub

Private Function SetOutputFolder(baseDir As String, ByRef folderName As Integer, ByRef recordCount As Integer) As String
    Dim targetDir = Path.Combine(baseDir, folderName.ToString())
    Dim filecounter = 0

    While Directory.Exists(targetDir)
        filecounter = Directory.GetFiles(targetDir).Count
        If filecounter >= recordCount Then
            folderName += 1
            targetDir = Path.Combine(baseDir, folderName.ToString())
        Else
            Exit While
        End If
    End While

    If Not Directory.Exists(targetDir) Then
        Directory.CreateDirectory(targetDir)
    End If

    Return targetDir
End Function

The additional function I created solves a few problems. Note that it is passing the folder counter and the record count as references ByRef folderName As Integer, ByRef recordCount As Integer, so it can continue with correct values after getting the right directory. It will search for the target directory, starting at 1, and for each directory it finds it will check to see if it is full or not. If it is, then it will carry on, otherwise it will select that directory.

Within this it also checked if the directory exists and if not, creates it before exiting, this removes the extra If statements that are needed throughout and puts them in one place.

Sign up to request clarification or add additional context in comments.

1 Comment

Glad I could help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.