这是一段代码,每分钟将文件夹中的所有 .csv 文件转换为 .xls。但我寻求一些帮助以避免重复

Here is a piece of code that converts all .csv files in a folder to .xls every minute. But I seeking some help to avoid duplication

这是一段代码,每分钟将文件夹中的所有 .csv 文件转换为 .xls。但我正在寻求一些帮助以避免重复。换句话说,根据流程,它会在某个文件夹中查找所有 .CSV 文件,并将它们保存为完全相同的文件夹中的 .XLS。在一个循环中,它每分钟继续这样做。现在已经转换一次的文件不需要再次转换。所以我正在寻求一些帮助来为它添加一个额外的逻辑,如下所述。

对于文件夹中的每个文件,如果文件扩展名为.CSV,请检查同一文件夹中是否存在扩展名为.XLS 的相同文件名。如果确实如此,则检查下一个文件是否相同,否则将该文件另存为 .XLS。对该文件夹中的所有剩余文件执行相同操作。

Dim waittime : waittime = 1 * 60 * 1000
'Constants
Const xlOpenXMLWorkbook = 51             '(without macro's in 2007-2016, xlsx)
Const xlOpenXMLWorkbookMacroEnabled = 52 '(with or without macro's in 2007-2016, xlsm)
Const xlExcel12 = 50                     '(Excel Binary Workbook in 2007-2016 with or without macro's, xlsb)
Const xlExcel8 = 56
Const xlExcEL9795 = 43

Do
    ' Extensions for old and new files
    strExcel = "xls"
    strCSV = "csv"

    ' Set up filesystem object for usage
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    ' Access the folder to process
    Set objFolder = objFSO.GetFolder("C:\Users\User\Desktop\CSV to XLS\")

    ' Load Excel (hidden) for conversions
    Set objExcel = CreateObject("Excel.Application")
    objExcel.Visible = False
    objExcel.DisplayAlerts = False

    ' Process all files
    For Each objFile In objFolder.Files
        ' Get full path to file
        strPath = objFile.Path
        ' Only convert CSV files
        If LCase(objFSO.GetExtensionName(strPath)) = LCase(strCSV) Then
            ' Display to console each file being converted
            'WScript.Echo "Converting """ & strPath & """"
            ' Load CSV into Excel and save as native Excel file
            Set objWorkbook = objExcel.Workbooks.Open(strPath, False, True)
            objWorkbook.SaveAs Replace(strPath, strCSV, strExcel), xlOpenXMLWorkbook
            objWorkbook.Close False
            Set objWorkbook = Nothing
        End If
    Next

    WScript.Sleep(waittime)
Loop

基本上,您想检查同一目录中是否存在具有相同基本名称但扩展名不同的文件,并且只有在这种情况下才进行转换。

If LCase(objFSO.GetExtensionName(strPath)) = LCase(strCSV) Then
    directory = objFSO.GetParentFolderName(strPath)
    basename  = objFSO.GetBaseName(strPath)
    xlsPath   = objFSO.BuildPath(directory, basename & "." & strExcel)

    If Not objFSO.FileExists(xlsPath) Then
        Set objWorkbook = objExcel.Workbooks.Open(strPath, False, True)
        objWorkbook.SaveAs xlsPath, xlOpenXMLWorkbook
        objWorkbook.Close False
        Set objWorkbook = Nothing
    End If
End If