elmonko
elmonko

Reputation: 685

Could not complete operation on some files

The below code basically takes a directory, copies it the zips it up. This has worked perfectly for me for the last 6 months

    Dim tempMail As String = Temp
    Dim startpath As String = tempMail & tvProgress.SelectedNode.FullPath
    Dim outMail As String = "c:\LTEMP\"
    Dim fileout As String = outMail & tvProgress.SelectedNode.Text
    Dim tempfolder As String = "c:\LTEMP" & "\" & tvProgress.SelectedNode.FullPath
    Dim Reality As String = "\Reality Stock Report"
    Dim enUK As New CultureInfo("en-GB")
    Try
        Dim x As Integer
        Dim q As Integer
        Dim inMail As String = FileStr

        If CheckInternet() = False Then
            MessageBox.Show("No internet connection is detected!" & vbNewLine & vbNewLine & "This job will still be added to completed but you will need to connect to the internet to complete sending this file to CIS", "Internet Connection Check", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
        End If

        If tvProgress.SelectedNode.Parent.Text = "Archive" Then Call INIDate()

        Dim Datey As String = Nothing
        If tvProgress.SelectedNode.Parent.Text <> "Archive" Then
            Datey = Date.ParseExact(Convert.ToDateTime(tvProgress.SelectedNode.Parent.Text), "dd/MM/yy", enUK)
        Else

            For q = 0 To UBound(INIdet)
                If tvProgress.SelectedNode.Parent.Text = "Archive" Then Datey = INIdet(q).iDate

            Next

        End If

        If My.Computer.FileSystem.FileExists(startpath & Reality & ".docx") Then
            Call Word2Pdf(startpath & Reality & ".docx", startpath & Reality & ".pdf")
        End If
        System.Threading.Thread.Sleep(150)
        For x = 0 To UBound(AllDetails)


            If AllDetails(x).uName & " - " & AllDetails(x).uCode & " - " & AllDetails(x).uOps = tvProgress.SelectedNode.Text Then

                If AllDetails(x).uPlan <> Datey Then
                    DateCh.Show()
                    Exit Sub

                End If
            End If

            If AllDetails(x).uName & " - " & AllDetails(x).uCode & " - " & AllDetails(x).uOps & " - " & AllDetails(x).uPlan = tvProgress.SelectedNode.Text & " - " & Datey Then
                Application.DoEvents()
                My.Computer.FileSystem.CopyDirectory(startpath, tempfolder, True)
                Directory.CreateDirectory(fileout & "\")

                With tempfolder

                    For Each xDir As String In Directory.GetFiles(tempfolder)

                        If Not ((UCase(Path.GetDirectoryName(xDir)).Contains("BACKUP")) Or (UCase(Path.GetDirectoryName(xDir)).Contains("BARS")) Or (UCase(Path.GetDirectoryName(xDir)).Contains("TCMS"))) Then
                            File.Copy(xDir, Path.Combine(fileout, Path.GetFileName(xDir)), True)

                        End If

                    Next

                    If Directory.Exists(tempfolder & "\Controller") Then

                        For Each xDir As String In Directory.GetFiles(tempfolder & "\Controller")

                            If Not ((UCase(Path.GetDirectoryName(xDir)).Contains("BACKUP")) Or (UCase(Path.GetDirectoryName(xDir)).Contains("BARS")) Or (UCase(Path.GetDirectoryName(xDir)).Contains("TCMS"))) Then
                                File.Copy(xDir, Path.Combine(fileout, Path.GetFileName(xDir)), True)

                            End If

                        Next
                    End If
                    If Directory.Exists(tempfolder & "\Capcon") Then

                        For Each xDir As String In Directory.GetFiles(tempfolder & "\Capcon")

                            If Not ((UCase(Path.GetDirectoryName(xDir)).Contains("BACKUP")) Or (UCase(Path.GetDirectoryName(xDir)).Contains("BARS")) Or (UCase(Path.GetDirectoryName(xDir)).Contains("TCMS"))) Then
                                File.Copy(xDir, Path.Combine(fileout, Path.GetFileName(xDir)), True)

                            End If

                        Next
                    End If
                    If Directory.Exists(tempfolder & "\Capcas") Then

                        For Each xDir As String In Directory.GetFiles(tempfolder & "\Capcas")

                            If Not ((UCase(Path.GetDirectoryName(xDir)).Contains("BACKUP")) Or (UCase(Path.GetDirectoryName(xDir)).Contains("BARS")) Or (UCase(Path.GetDirectoryName(xDir)).Contains("TCMS"))) Then
                                File.Copy(xDir, Path.Combine(fileout, Path.GetFileName(xDir)), True)

                            End If

                        Next
                    End If

                End With

                Dim zip As String = inMail & AllDetails(x).uFile & ".zip"
                Dim txt As String = inMail & AllDetails(x).uFile & ".txt"
                Dim pdf As String = inMail & AllDetails(x).uFile & ".pdf"
                Dim fldr As String = FileStr & AllDetails(x).uFile

                ZipFile.CreateFromDirectory(fileout & "\", fileout & ".zip", CompressionLevel.Optimal, False)
                My.Computer.FileSystem.CopyFile(fileout & ".zip", ArcFolder & tvProgress.SelectedNode.Text & Date.Parse(AllDetails(x).pDate).ToString(" - dd-MM-yyyy") & ".zip", True)

                My.Computer.FileSystem.MoveFile(fileout & ".zip", FileStrOut & tvProgress.SelectedNode.Text & ".zip", True)

                My.Computer.FileSystem.DeleteDirectory(fileout, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin, FileIO.UICancelOption.DoNothing)
                If My.Computer.FileSystem.FileExists(zip) Then
                    My.Computer.FileSystem.MoveFile(zip, "c:\LTEMP\" & AllDetails(x).uFile & ".zip", True)
                End If
                If My.Computer.FileSystem.FileExists(txt) Then
                    My.Computer.FileSystem.CopyFile(txt, ArcFolder & AllDetails(x).uFile & ".txt", True)
                    My.Computer.FileSystem.MoveFile(txt, "c:\LTEMP\" & AllDetails(x).uFile & ".txt", True)
                End If
                If My.Computer.FileSystem.FileExists(pdf) Then
                    My.Computer.FileSystem.MoveFile(pdf, "c:\LTEMP\" & AllDetails(x).uFile & ".pdf", True)
                End If
                With fldr
                    If My.Computer.FileSystem.DirectoryExists(fldr) Then

                        Directory.Delete(fldr, True)
                    End If
                End With

                System.Threading.Thread.Sleep(150)
            End If

        Next

        My.Computer.FileSystem.DeleteDirectory(tempMail & tvProgress.SelectedNode.FullPath, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin)
        tvProgress.BeginUpdate()
        tvProgress.SelectedNode.Remove()
        tvProgress.EndUpdate()
        Dim sRoot As String = tempMail

        Dim sPath As String = tempMail

        DeleteEmptyFolders(sPath, sRoot)
        lstPlanned.BeginUpdate()
        Call TxtRfrsh()
        Call RfshArray()
        Call CompleteArray()
        Call ltempfiles()
        lstPlanned.EndUpdate()

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

I've recently added the following bit of code which calls a routine that converst a docx file to a pdf (works fine on its own)

 If My.Computer.FileSystem.FileExists(startpath & Reality & ".docx") Then
            Call Word2Pdf(startpath & Reality & ".docx", startpath & Reality & ".pdf")
        End If
        System.Threading.Thread.Sleep(150)

The called sub

Public Sub Word2Pdf(ByVal infile As String, ByVal outfile As String)

    Dim wordApp As Word.Application = Nothing

    Try
        wordApp = New Word.Application()
        wordApp.Documents.Open(infile)
        wordApp.ActiveDocument.ExportAsFixedFormat(outfile, Word.WdExportFormat.wdExportFormatPDF)
    Finally

        If wordApp IsNot Nothing Then

            wordApp.Quit()

        End If

    End Try
End Sub

Now when the criteria is met to call the above sub I'm getting the Could not complete operation on some files error. When I check the directory the PDF has been created but its not then completing the rest of the zip and copy routine.

Any ideas??

Upvotes: 2

Views: 2313

Answers (1)

xpda
xpda

Reputation: 15813

WordApp probably has one of the files locked. Check to make sure WordApp has finished execution before you continue. The Sleep(150) is apparently not long enough.

Upvotes: 1

Related Questions