我有一些代码,它会根据几个因素将递归文件夹中的文件选择重命名并复制到输出文件夹中。我尝试添加进度条,因为在进程完成时不清楚,但progressbar.maximum并不总是相同。我想将其设置为输出文件夹中的文件数,但是在原始任务完成之前,下一个任务已经开始处理这些文件,转换和复制/删除。这意味着我希望在第二个任务开始处理文件夹中的所有文件之前完全完成第一个任务。据我所知,我认为我需要threading.tasks,但对于我的生活,我似乎无法让它工作,我认为这可能是因为sub中的递归函数。
如此简单的版本......如何阻止一个if / then循环开始直到前一个循环结束?
Private Sub finalise(ByVal folder As DirectoryInfo)
Dim task1 As New Threading.Tasks.Task(Sub()
Dim files = folder.GetFiles
For Each doc In files
Dim testvar = doc.FullName
Dim pos1 = InStr(doc.FullName, "SAT\") - 1 'position in string marks beginning of project name
Dim pos2 = doc.FullName.LastIndexOf("\") ' position in string marks last subfolder
Dim NoofChars As Integer = pos2 - pos1 'number of chars to use from full string
Dim filenameappend = doc.FullName.Substring(pos1, NoofChars) 'select the appropriate chars
Dim append As String = filenameappend.Replace("\", " ") 'replace all teh \ with spaces
Dim exten As String = Path.GetExtension(doc.FullName)
Dim procfilepdf = Nothing
Try
If exten = ".tmp" Then
Else
If exten = ".pdf" Then
doc.CopyTo(projDirectory & "\processfolder\" & append & " " & doc.Name)
'if you are looking at a pdf, copy it to process
Else 'if its not a pdf however...
procfilepdf = Path.ChangeExtension(doc.FullName, "pdf") 'change the variable extension to pdf ( not the file)
If File.Exists(procfilepdf) Then 'it must have already been copied
Else
doc.CopyTo(projDirectory & "\processfolder\" & append & " " & doc.Name)
'ie its not a PDF and there is no PDF version of that file
'copy the original to the process folder and prefix the filename with the filestructure of its parent project
End If
End If
End If
Catch e As System.IO.IOException
MsgBox("some of these files already exist, has this already been done?")
End Try
Next
For Each subfolder In folder.GetDirectories
finalise(subfolder) 'recursive through all folders
Next
End Sub)
ProgressBar1.Visible = False
task1.Start()
task1.Wait()
Dim fileNo As Integer
Dim procFiles = processfolder.GetFiles
fileNo = procFiles.Count
Label2.Text = fileNo
Dim prog As Integer = fileNo
Dim procexten As String
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oExcel As Excel.Application
Dim oXLS As Excel.Workbook
ProgressBar1.Visible = True
ProgressBar1.Minimum = 1
ProgressBar1.Maximum = fileNo
ProgressBar1.Step = 1
ProgressBar1.Value = 1
For Each file In processfolder.GetFiles
Dim ext As String = Path.GetExtension(file.Name)
If ext = ".pdf" Then
ElseIf ext = ".doc" Then
Dim filepdf = Path.ChangeExtension(file.FullName, "pdf")
oWord = CreateObject("Word.Application")
oWord.Visible = False
oDoc = oWord.Documents.Add(file.FullName)
oDoc.ExportAsFixedFormat(filepdf, Word.WdExportFormat.wdExportFormatPDF)
oDoc.Close(False)
oWord.Quit()
ProgressBar1.PerformStep()
prog = prog - 1
ElseIf ext = ".docx" Then
Dim filepdf = Path.ChangeExtension(file.FullName, "pdf")
oWord = CreateObject("Word.Application")
oWord.Visible = False
oDoc = oWord.Documents.Add(file.FullName)
oDoc.ExportAsFixedFormat(filepdf, Word.WdExportFormat.wdExportFormatPDF)
oDoc.Close(False)
oWord.Quit()
ProgressBar1.PerformStep()
prog = prog - 1
ElseIf ext = ".xls" Then
Dim filepdf = Path.ChangeExtension(file.FullName, "pdf")
oExcel = CreateObject("Excel.Application")
oExcel.Visible = False
oXLS = oExcel.Workbooks.Add(file.FullName)
oXLS.Worksheets("Report").ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, filepdf)
oXLS.Close(False)
oExcel.Quit()
ProgressBar1.PerformStep()
prog = prog - 1
ElseIf ext = ".xlsm" Then
Dim filepdf = Path.ChangeExtension(file.FullName, "pdf")
oExcel = CreateObject("Excel.Application")
oExcel.Visible = False
oXLS = oExcel.Workbooks.Add(file.FullName)
oXLS.Worksheets("Report").ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, filepdf)
oXLS.Close(False)
oExcel.Quit()
ProgressBar1.PerformStep()
prog = prog - 1
ElseIf ext = ".xlsb" Then
Dim filepdf = Path.ChangeExtension(file.FullName, "pdf")
oExcel = CreateObject("Excel.Application")
oExcel.Visible = False
oXLS = oExcel.Workbooks.Add(file.FullName)
oXLS.Worksheets("Report").ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, filepdf)
oXLS.Close(False)
oExcel.Quit()
ProgressBar1.PerformStep()
prog = prog - 1
End If
Next
For Each procTest In procFiles
Dim procfilepdf
procexten = Path.GetExtension(procTest.FullName)
If procexten = ".pdf" Then 'if you are looking at a pdf, do nothing
Else 'if its not a pdf however...
procfilepdf = Path.ChangeExtension(procTest.FullName, "pdf") 'change the variable extension to pdf ( not the file)
If File.Exists(procfilepdf) Then 'check if there is a pdf version
File.Delete(procTest.FullName) 'and if there is, delete the original.
Else
MsgBox("the file" & procTest.Name & "hasn't been converted to pdf automagically, you'll have to do it manuelly", MsgBoxStyle.OkOnly)
End If
End If
Next
Dim Command As String = "/C pdftk " & processfolder.FullName & "\*.pdf cat output " & Final.FullName & "\" & cmbProjPicker.SelectedItem & ".pdf"
Process.Start("cmd.exe", Command)
End Sub