VBA在移动到另一个文件夹之前比较文件

时间:2017-09-22 17:47:16

标签: excel-vba vba excel

在将输入文件移动到另一个文件夹之前,我一直在尝试使用能够确保输入和输出文件夹匹配的代码。这是一个片段:

'Select first Excel file within that Outlook_ImportedClaimsFiles folder path
OutlookImportFN = Dir(OutlookImportPath & "\*.xls")


'Select first Excel file within the SAS_Outputs folder path
SASOutputFN = Dir(SASOutputsPath & "\*.xls")

'Cycle through all files in the source folder path until there are no more left to cycle through
Do While OutlookImportFN <> ""
Counter = Counter + 1

    'Create full path name of the source file
    sFilePathName = OutlookImportPath & "\" & OutlookImportFN

    'Create full path name of the destination file and add the date and time the file was moved
    dFilePathName = OutlookRunPath & "\" & Format(Now, "yyyymmdd") & "_" & OutlookImportFN

问题是在第一个文件夹(OutlookImportFN)中,有一个批处理/保留文件,其中包含该整个过程中使用的该文件夹中所有文件的文件名。标记第一个文件时,此文件首先出现(此文件根本不应被标记/选中)。因此,在进行比较时,文件不匹配(除非我从列表中的第二个文件开始)。与Output文件夹中的第一个文件进行比较时,如何从Input文件夹中的第二个文件开始,或者在进行比较时跳过Input文件夹中的保存文件?我尝试过一些东西但似乎没什么用。提前感谢您的见解!

1 个答案:

答案 0 :(得分:0)

您需要添加If语句,以确保该文件不是该批处理文件:

Do While OutlookImportFN <> ""
Counter = Counter + 1

    If Not OutlookImportFN = "MyBatchFile" Then

    'Create full path name of the source file
    sFilePathName = OutlookImportPath & "\" & OutlookImportFN

    'Create full path name of the destination file and add the date and time the file was moved
    dFilePathName = OutlookRunPath & "\" & Format(Now, "yyyymmdd") & "_" & OutlookImportFN

    End If
相关问题