首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FSO没有收到任何文件

FSO没有收到任何文件
EN

Stack Overflow用户
提问于 2014-11-20 18:09:43
回答 1查看 1.8K关注 0票数 1

我正试着用程序来复制带有特定字符的文件。要复制的文件应在今天的日期到前100天之间。我的程序可以运行,但是新文件夹上没有显示任何内容。我确实确保了文件在那两天之间。我没有任何错误,所以我不知道该在哪里修理。我尝试过其他方法,但都没有奏效。

我尝试混合来自http://www.rondebruin.nl/win/s3/win026.htm的代码。我玩得很开心,只有copy_folder()在工作。我得到了运行时错误'53‘-在Copy_Certain_Files_In_Folder()Copy_Files_Dates()上找不到的文件也没有给我任何东西。

无论如何,我的代码有什么问题,我如何将FileExt合并到下面的代码中?谢谢!

代码语言:javascript
复制
Sub CopyPasteFiles()

Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
Dim Fdate As Date
Dim FileExt As String
Dim objFile As Object
Dim objFolder As Object

FromPath = "C:\Users\Run"  '<< Change
ToPath = "C:\Users\Test"    '<< Change
FileExt = "*BT.csv"

If Right(FromPath, 1) <> "\" Then
    FromPath = FromPath & "\"
End If

Set FSO = CreateObject("scripting.filesystemobject")

If FSO.FolderExists(FromPath) = False Then
    MsgBox FromPath & " doesn't exist"
    Exit Sub
End If

If FSO.FolderExists(ToPath) = False Then
    MsgBox ToPath & " doesn't exist"
    Exit Sub
End If

For Each objFolder In FSO.GetFolder(FromPath).SubFolders
    For Each objFile In objFolder.Files
            Fdate = Int(objFile.DateCreated)
            If Fdate >= Date And Fdate <= Format(DateAdd("d", -100, Date), "dd mmmm yyyy") Then
                objFile.Copy ToPath
            End If
    Next objFile
Next objFolder

MsgBox "You can find the files from " & FromPath & " in " & ToPath

End Sub
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-21 01:40:27

好吧,我试着添加一些评论给你一些方向。第一个问题是,您没有对根文件夹做任何事情--您试图直接进入子文件夹,这可能就是为什么您说它“突出显示”了外循环层上的线条。(高亮显示的行是当您单击F8 next时将执行的行。)

我所做的就是将复制操作分解为另一个过程,这样您就可以在任何子文件夹上递归地调用它。这只是一种方式-有其他,可能更简单的方法,但这是我想到的,因为我已经习惯了在文件夹和记录集递归挖掘这种方式。

你遇到的另一个问题是比较日期的方法。.DateCreated属性的格式随日期和时间而增加。您可以直接将其与返回日期和时间的Now()函数进行比较,但如果您尝试将其与Date()函数进行比较,它将无法工作,因为它是一种不同的格式。

我不知道你想用这个文件扩展名做什么。我以为你想用它作为过滤器,所以这就是我用它做的。

注意:您目前正在告诉用户“您可以从其中找到文件”,但是您没有检查这是否属实。您可能需要在.Copy操作之后添加一个检查,然后将结果添加到数组或其他东西中,以便向用户显示成功复制的文件和未复制的文件的列表。在测试时,我创建了您在Users目录中的文件夹,并且在试图复制不具有所需权限时出错。

现在,从路径、到路径和扩展过滤器都是硬编码的.如果您计划在多个位置分发它或自己使用它,您可以使用BrowseForFolder方法向用户显示文件夹浏览器对话框,并允许他们选择From和From。您还可以使用InputBox从用户那里获得一个过滤器。这只是个想法。

总之,这是我对你的代码所做的。我将变量名更改为我的命名约定,因为这是我习惯的地方--您可以随意更改它们。

代码语言:javascript
复制
Option Explicit

Public Sub CopyPasteFiles()
    'Declare variables
        Dim SRfso                   As Scripting.FileSystemObject
        Dim strFrom                 As String
        Dim strTO                   As String
        Dim strExtFilter             As String
        Dim SRfolderA               As Scripting.Folder
        Dim SRfolderB               As Scripting.Folder

    'Are you always going to hardcode these or do you want to be able to browse for a folder?
        strFrom = "C:\Users\Run"  '<< Change
        strTO = "C:\Users\Test"    '<< Change

    'I'm not sure what your intent is with this - I assumed you wanted to filter by file extension.
        strExtFilter = "*BT.CSV"

    'Prep the folder path
        If Right(strFrom, 1) <> "\" Then
            strFrom = strFrom & "\"
        End If

    'Intialize the FileSystemObject
        Set SRfso = New Scripting.FileSystemObject

        'Verify input and output folders exist. Inform user if they don't.
            If SRfso.FolderExists(strFrom) = False Then
                MsgBox strFrom & " doesn't exist"
                Exit Sub
            End If

            If SRfso.FolderExists(strTO) = False Then
                MsgBox strTO & " doesn't exist"
                Exit Sub
            End If

    'Get the input folder using the FileSystemObject
        Set SRfolderA = SRfso.GetFolder(strFrom)

    'Call the routine that copies the files
        MoveTheFiles SRfolderIN:=SRfolderA, strFolderOUT:=strTO ', strExtFilter:=strExtFilter

    'Inform the user where they can find the files. CAUTION: You may be misinforming the user.
        MsgBox "You can find the files from " & strFrom & " in " & strTO

End Sub

Private Sub MoveTheFiles(ByRef SRfolderIN As Scripting.Folder, _
                            ByRef strFolderOUT As String, _
                            Optional ByRef strExtFilter As String = "*.*", _
                            Optional ByRef blnSUBFOLDERS As Boolean = True)
'This routine copies the files.  It requires two arguments.  First, it requires the root folder as folder object from the scripting library. _
 Second, it requires the output path as a string.  There are two optional arguments. The first allows you _
 to use a text filter as a string.  The second is a boolean that tells us whether or not to move files in subfolders - the default is true.

    'Delcare variables
        Dim SRfileA                 As Scripting.File
        Dim SRfolderCol             As Scripting.Folders
        Dim SRfolderA               As Scripting.Folder
        Dim datCreated              As Date
        Dim lngFX                   As Long
        Dim blnResult               As Boolean

    'Find the file extension in the filter
        lngFX = InStrRev(strExtFilter, ".", , vbTextCompare)

    'Move the files from the root folder
        For Each SRfileA In SRfolderIN.Files
            'Only work with files that contain the filter criteria
                If Ucase(Mid(SRfileA.Name, InStrRev(SRfileA.Name, ".", , vbTextCompare) - (Len(strExtFilter) - lngFX) + 1, Len(strExtFilter))) Like Ucase(strExtFilter) Then
                'Only work with files that were created within the last 100 days
                    datCreated = SRfileA.DateCreated
                        If datCreated <= Now And (datCreated >= DateAdd("d", -100, Now())) Then
                            SRfileA.Copy strFolderOUT
                        End If
                End If
        Next

    'Check if the calling procedure indicated we are supposed to move subfolder files as well
        If blnSUBFOLDERS Then
        'Check that we have subfolders to work with
            Set SRfolderCol = SRfolderIN.SubFolders
                If SRfolderCol.Count > 0 Then
                        For Each SRfolderA In SRfolderIN.SubFolders
                            MoveTheFiles SRfolderIN:=SRfolderA, strFolderOUT:=strFolderOUT, strExtFilter:=strExtFilter, blnSUBFOLDERS:=blnSUBFOLDERS
                        Next
                End If
        End If

End Sub
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27046564

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档