vba 使用vba将文件从一个文件夹复制到另一个文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26241927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 04:46:19  来源:igfitidea点击:

Copying files from one folder to another using vba

excelvbafiledirectory

提问by dekio

There are some similar posts about this topic, I know. However, I have a code which is different than all codes I have seen here (when talking about this subject).

我知道有一些关于这个主题的类似帖子。但是,我有一个与我在这里看到的所有代码不同的代码(在谈论这个主题时)。

The error I am receiving is saying that the file couldn't be found. But that's kind of impossible, since I am searching for the file in the same folder I am using as SOURCE in fso.CopyFile.

我收到的错误是找不到文件。但这有点不可能,因为我在 fso.CopyFile 中用作 SOURCE 的同一文件夹中搜索文件。

So I have to fix this error and, if possible, I would like to copy the file to another folder and change the name. For example, if I have the file "Excel.xls", I would like to copy with the name "Excel_old.xls", is that possible using the code below or is it too hard that it's not worth?

所以我必须修复这个错误,如果可能的话,我想将该文件复制到另一个文件夹并更改名称。例如,如果我有文件“Excel.xls”,我想使用名称“Excel_old.xls”进行复制,是否可以使用下面的代码或者它太难而不值得?

This is the code:

这是代码:

Sub CopyFiles()
'Macro to copy all files modified yesterday

Dim n As String, msg As String, d As Date
Dim fso As Object

Set fso = CreateObject("Scripting.FileSystemObject")
Set fils = fso.GetFolder("C:\Users\Desktop\Files\").Files

'Verify all files in the folder, check the modification date and then copy 
'to another folder (named Old)
For Each fil In fils
    n = fil.Name
    d = fil.DateLastModified
    If d >= Date - 1 Then
        file = n
        'The following line is where the error occurs
        fso.CopyFile "C:\Users\Desktop\Files\file", "C:\Users\Desktop\Files\Old\file"

    End If
Next fil

End Sub

回答by JNevill

This is because fso.CopyFile "C:\Users\Desktop\Files\file", "C:\Users\Desktop\Files\Old\file"is not a file... It's just a string to a dummy file from the looks of it.

这是因为fso.CopyFile "C:\Users\Desktop\Files\file", "C:\Users\Desktop\Files\Old\file"它不是一个文件......从它的外观来看,它只是一个虚拟文件的字符串。

If instead the line was

如果相反,该行是

fso.CopyFile fil.Path, "C:\Users\Desktop\Files\Old\" & fil.name... that might work.

fso.CopyFile fil.Path, "C:\Users\Desktop\Files\Old\" & fil.name……那可能有用。

Updated to add:

更新添加:

I just attempted the following using (subbing computer username for below) and had success moving everything into a new folder:

我只是尝试了以下使用(将计算机用户名替换为下面)并成功将所有内容移动到一个新文件夹中:

Sub test()
    Dim fso As FileSystemObject
    Dim fsoFiles As Files
    Dim fil As File

    Set fso = New FileSystemObject
    Set fils = fso.GetFolder("C:\Users\<MY USERNAME>\Desktop\").Files

    For Each fil In fils
        n = fil.Name
        d = fil.DateLastModified
        fso.CopyFile fil.Path, fil.ParentFolder & "\test\" & fil.Name

    Next fil
End Sub

The only difference here is that I used fil.ParentFolder to get my Desktop and then tossed it into a new folder I created on my desktop (prior to running the script) named "test".

这里唯一的区别是我使用 fil.ParentFolder 来获取我的桌面,然后将它扔到我在桌面上创建的一个名为“test”的新文件夹中(在运行脚本之前)。

回答by user3884205

For moving all files in a folder:

要移动文件夹中的所有文件:

Sub MoveFiles()

Dim MyFile As String

MyFile = Dir("C:\AAAA\*.*")

Do Until MyFile = ""

Name "C:\AAAA\" & MyFile As "C:\AAA\" & MyFile

MyFile = Dir

Loop

End Sub