如果存在,检查目标文件夹中的文件。

时间:2021-11-12 13:26:24

I am trying to determine the number of files that would be copied from the source folder to the destination and then assign this value to the progressbar.max.But using the code below I get Runtime error 5, Invalid procedure call or argument at the marked position.Please Guide

我正在尝试确定从源文件夹复制到目的地的文件的数量,然后将这个值分配给progressbar.max。但是使用下面的代码,我得到了运行时错误5,无效的过程调用或在标记位置的参数。请指导

Private Sub cmdCopy_Click()
Dim sFileName As String    'Source File
Dim sDirName As String     'Source Directory
Dim dDirName As String     'Destination Directory
Dim fiFileCount As Integer 'Number of Files to be copied
Dim fbFileMatch As Boolean

If prgFCount.Visible = True Then prgFCount.Visible = False

dDirName = "D:\Destination\"
sDirName = "C:\Source\"
sFileName = Dir(sDirName)


' Disable this button so the user cannot
' start another copy.
cmdCopy.Enabled = False
cmdCancel.Enabled = True
fiFileCount = 0

Do While Len(sFileName) > 0
fbFileMatch = False
If Len(Dir$(dDirName & sFileName)) > 0 Then
fbFileMatch = True
End If
If fbFileMatch = False Then
fiFileCount = fiFileCount + 1
End If

sFileName = Dir '## Error at this Point ##
Loop
If fiFileCount = 0 Then
cmdCopy.Enabled = True
cmdCancel.Enabled = False
Exit Sub
End If
prgFCount.Min = 0
prgFCount.Max = fiFileCount
prgFCount.Visible = True
End Sub

2 个解决方案

#1


2  

If Len(Dir$(dDirName & sFileName)) > 0 Then

You set up your directory iteration with the line:

将目录迭代设置为一行:

sFileName = Dir(sDirName)

Calling the Dir function without parameters will get the next item meeting the file name pattern and attributes is retrieved. The Len(Dir$ call is screwing it up.

在没有参数的情况下调用Dir函数将会获得下一个要处理文件名称模式和属性的项目。Len(Dir$ call正在把它搞得一团糟。

I would suggest rewriting your code to loop through all the files in your source folder and build a list, then loop through the list and look for matches in your destination folder.

我建议重写您的代码,以循环遍历源文件夹中的所有文件并构建一个列表,然后遍历列表并在目标文件夹中查找匹配项。

Something like this:

是这样的:

...
sFileName = Dir$(sDirName)
Do While Len(sFileName) > 0
   i = i + 1
   ReDim Preserve strSourceFileList(i)
   strSourceFileList(i) = sFileName
   sFileName = Dir()
Loop

If i > 0 Then
   For i = LBound(strSourceFileList) To UBound(strSourceFileList)
      sFileName = Dir$(dDirName & strSourceFileList(i))
      If Len(sFileName) = 0 Then
         fiFileCount = fiFileCount + 1
      End If
   Next i
End If
...

#2


1  

Dir returns the name of a matching file, directory, or folder. Calling Dir should be fine but in your case it generates the error.

Dir返回匹配文件、目录或文件夹的名称。调用Dir应该没问题,但是在您的情况下,它会生成错误。

You also have no loop implemented to iterrate through all the available source files.

您还没有实现遍历所有可用源文件的循环。

Using the FileSystemObject is one of the options. To use the FileSystemObject, click the Project menu option, followed by the References... menu option. This will open the References Dialog.

使用文件系统对象是选项之一。要使用文件系统对象,请单击项目菜单选项,然后是引用…菜单选项。这将打开引用对话框。

Tick the box beside the reference named "Microsoft Scripting Runtime" and click OK.

在名为“Microsoft脚本运行时”的引用旁边打勾,然后单击OK。

Now you can declare a variable as a FileSystemObject. In addition you get access to more objects such as File, Folder, Files and more.

现在可以将变量声明为文件系统对象。此外,您还可以访问更多的对象,比如文件、文件夹、文件等等。

Using the FileSystemObject gives you access to a wide range of features.

使用文件系统对象可以让您访问各种特性。

The code below demonstrates how to get the count of files which do not exist in the destination and will be copied, using the FileSystemObject.

下面的代码演示了如何获取目标中不存在的文件的计数,并使用文件系统对象进行复制。

Private Sub cmdCopy_Click()
    Dim fso As New FileSystemObject
    Dim sourceFolder As Folder
    Dim sourceFile As File
    Dim destinationFolder As Folder
    Dim filesToBeCopied As Integer

    Set sourceFolder = fso.GetFolder("C:\-- Temp --\Source")
    Set destinationFolder = fso.GetFolder("C:\-- Temp --\Destination")

    filesToBeCopied = 0

    ' Iterrate through each file in the source folder.
    For Each sourceFile In sourceFolder.Files
        ' Check if the source file exists in the destination folder
        If Not (fso.FileExists(destinationFolder + "\" + sourceFile.Name)) Then
            filesToBeCopied = filesToBeCopied + 1
        End If
    Next
End Sub

I have tested the above code and it correctly increments the count of filesToBeCopied to the expected number.

我已经测试了上面的代码,并且它正确地增加了filesToBeCopied的计数。

#1


2  

If Len(Dir$(dDirName & sFileName)) > 0 Then

You set up your directory iteration with the line:

将目录迭代设置为一行:

sFileName = Dir(sDirName)

Calling the Dir function without parameters will get the next item meeting the file name pattern and attributes is retrieved. The Len(Dir$ call is screwing it up.

在没有参数的情况下调用Dir函数将会获得下一个要处理文件名称模式和属性的项目。Len(Dir$ call正在把它搞得一团糟。

I would suggest rewriting your code to loop through all the files in your source folder and build a list, then loop through the list and look for matches in your destination folder.

我建议重写您的代码,以循环遍历源文件夹中的所有文件并构建一个列表,然后遍历列表并在目标文件夹中查找匹配项。

Something like this:

是这样的:

...
sFileName = Dir$(sDirName)
Do While Len(sFileName) > 0
   i = i + 1
   ReDim Preserve strSourceFileList(i)
   strSourceFileList(i) = sFileName
   sFileName = Dir()
Loop

If i > 0 Then
   For i = LBound(strSourceFileList) To UBound(strSourceFileList)
      sFileName = Dir$(dDirName & strSourceFileList(i))
      If Len(sFileName) = 0 Then
         fiFileCount = fiFileCount + 1
      End If
   Next i
End If
...

#2


1  

Dir returns the name of a matching file, directory, or folder. Calling Dir should be fine but in your case it generates the error.

Dir返回匹配文件、目录或文件夹的名称。调用Dir应该没问题,但是在您的情况下,它会生成错误。

You also have no loop implemented to iterrate through all the available source files.

您还没有实现遍历所有可用源文件的循环。

Using the FileSystemObject is one of the options. To use the FileSystemObject, click the Project menu option, followed by the References... menu option. This will open the References Dialog.

使用文件系统对象是选项之一。要使用文件系统对象,请单击项目菜单选项,然后是引用…菜单选项。这将打开引用对话框。

Tick the box beside the reference named "Microsoft Scripting Runtime" and click OK.

在名为“Microsoft脚本运行时”的引用旁边打勾,然后单击OK。

Now you can declare a variable as a FileSystemObject. In addition you get access to more objects such as File, Folder, Files and more.

现在可以将变量声明为文件系统对象。此外,您还可以访问更多的对象,比如文件、文件夹、文件等等。

Using the FileSystemObject gives you access to a wide range of features.

使用文件系统对象可以让您访问各种特性。

The code below demonstrates how to get the count of files which do not exist in the destination and will be copied, using the FileSystemObject.

下面的代码演示了如何获取目标中不存在的文件的计数,并使用文件系统对象进行复制。

Private Sub cmdCopy_Click()
    Dim fso As New FileSystemObject
    Dim sourceFolder As Folder
    Dim sourceFile As File
    Dim destinationFolder As Folder
    Dim filesToBeCopied As Integer

    Set sourceFolder = fso.GetFolder("C:\-- Temp --\Source")
    Set destinationFolder = fso.GetFolder("C:\-- Temp --\Destination")

    filesToBeCopied = 0

    ' Iterrate through each file in the source folder.
    For Each sourceFile In sourceFolder.Files
        ' Check if the source file exists in the destination folder
        If Not (fso.FileExists(destinationFolder + "\" + sourceFile.Name)) Then
            filesToBeCopied = filesToBeCopied + 1
        End If
    Next
End Sub

I have tested the above code and it correctly increments the count of filesToBeCopied to the expected number.

我已经测试了上面的代码,并且它正确地增加了filesToBeCopied的计数。