问题描述
在一个文件夹中遍历所有文件名,判断是否存在于数据库如果存在就移动到truefilepath文件夹没有存在移动到false文件夹但是运行时间很长姚156秒才能运行完。求大神帮助,史什么地方的操作导致的程序运行缓慢'/判断是否受信PrivateFunctionExecuteTableshoukai(ByValfolderspec)AsBooleanDimvlSqlAsStringDimvlDataTableAsDataTableDimvlCheck1AsString=String.EmptyDimIsNullAsStringDimfs,f1,f2,FFFreDimFormatName,FormatName1DimReadAsBoolean=TrueDimiAsInteger=1Dimi1AsStringDimi2AsStringDimvlReturnAsBooleanfs=CreateObject("Scripting.FileSystemObject")basLogOut.WriteOperationLog(cmMODULE_NAME,"ExecuteTableshoukai","YesORNo")'遍历文件WhileRead=Truei1=Trim(i)i2=Trim(i+1)FilePathFileName=KobayashiCreate.Common.IniFile.GetAppString("GIGCC2JP","AW_ReciveFolder_"+i1,vmIniFile)IsNull=KobayashiCreate.Common.IniFile.GetAppString("GIGCC2JP","AW_ReciveFolder_"+i2,vmIniFile)IfIsNull=String.EmptyThenRead=FalseEndIfWorkFilePathFileName=KobayashiCreate.Common.IniFile.GetAppString("GIGCC2JP","AW_WorkFolder_"+i1,vmIniFile)TrueFilePathFileName=KobayashiCreate.Common.IniFile.GetAppString("GIGCC2JP","AW_SaveFolder_"+i1,vmIniFile)FalsePathFileName=KobayashiCreate.Common.IniFile.GetAppString("GIGCC2JP","AW_Taishougai_"+i1,vmIniFile)vlCheck=KobayashiCreate.Common.IniFile.GetAppString("GIGCC2JP","SearchPattern_"+i1,vmIniFile).Split(".")vlCheck1=vlCheck(1)i=i+1'/遍历目标文件夹f1=fs.GetFolder(FilePathFileName)DimSerchFD=f1.SubFolders'定义文件夹对象DimSerchFF=f1.Files'定义文件对象'/复制文件到目标文件夹Ifi=2ThenForEachFFFreInSerchFFFileCopy(FilePathFileName+FFFre.Name,WorkFilePathFileName+FFFre.Name)NextEndIf'/遍历工作文件夹f2=fs.GetFolder(WorkFilePathFileName)DimSerchFD1=f2.SubFolders'定义文件夹对象DimSerchFF1=f2.Files'定义文件对象ForEachFFFreInSerchFF1FormatName=Split(FFFre.Name,".")FormatName1=FormatName(1)'/对比该文件后缀是否相同IfUCase(FormatName1)=UCase(vlCheck1)ThenvlSql="select*fromTT_GIG_RCV_AW_HEADwhereINT_JUSHIN_STATUS=2"vlDataTable=IDataBaseAccess.GetDataTable(vlSql)ForEachdrAsDataRowInvlDataTable.Rows'/判断数据库字段是否存在该文件Ifdr("TORIKOMI_FILE_NAME").ToString()=FFFre.NameThenDimstrPair,a,b,Time,NewfileName'/检查文件字段是否合格vlReturn=MyTxtReader(WorkFilePathFileName+FFFre.Name)'/判断文件中第39IfvlReturn=TrueThen'/更改名称strPair=Split(FFFre.Name,".")a=strPair(0)Time=Format(Now,"yyyyMMddHHmmss")NewfileName=a+Time+".BK"Rename(WorkFilePathFileName+FFFre.Name,WorkFilePathFileName+NewfileName)FileCopy(WorkFilePathFileName+NewfileName,TrueFilePathFileName+NewfileName)'复制文件Kill(WorkFilePathFileName+NewfileName)ExitForElseFileCopy(WorkFilePathFileName+FFFre.Name,FalsePathFileName+FFFre.Name)'复制文件Kill(WorkFilePathFileName+FFFre.Name)ExitForEndIf'Else'FileCopy(WorkFilePathFileName+FFFre.Name,FalsePathFileName+FFFre.Name)'复制文件'Kill(WorkFilePathFileName+FFFre.Name)EndIfNextEndIfNextForEachFFFreInSerchFF1FileCopy(WorkFilePathFileName+FFFre.Name,FalsePathFileName+FFFre.Name)'复制文件Kill(WorkFilePathFileName+FFFre.Name)NextEndWhileEndFunction'/读取文本PublicFunctionMyTxtReader(ByValStrPathAsString)AsBooleanDimvlRtnAsBoolean=TrueDimMyTxtAsStringIfIO.File.Exists(StrPath)=TrueThenDimTxtReaderAsIO.StreamReader=NewIO.StreamReader(StrPath,System.Text.Encoding.Default)DimvlCheckAsString()DimvlCheck1AsStringMyTxt=TxtReader.ReadToEndvlCheck=Split(MyTxt,",")vlCheck1=vlCheck(39)IfvlCheck1="""0"""OrvlCheck1="""2"""ThenvlRtn=TrueTxtReader.Close()ReturnvlRtnElsevlRtn=FalseTxtReader.Close()ReturnvlRtnEndIfEndIfEndFunction
解决方案
解决方案二:
自己断点,打印下各步骤的耗时就找出耗时原因了,你的代码有点复杂,就排查文件,只文件名,大小等,不验证文件是否相同应该很快,如果是严重文件内容,介个就不好说了,还有数据库存储的文件信息能一次查询最好,可以节约速度。