使用vba将文件从一个文件夹复制到另一个文件夹 [英] Copying files from one folder to another using vba

查看:473
本文介绍了使用vba将文件从一个文件夹复制到另一个文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个话题有一些类似的帖子。但是,我有一个代码与我在这里看到的所有代码(当谈到这个问题)不同。



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



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



这是代码:

  Sub CopyFiles()
'Macro to复制所有文件修改

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

设置fso = CreateObject(Scripting.FileSystemObject )
设置文件= fso.GetFolder(C:\Users\Desktop\Files\)。文件

'验证文件夹中的所有文件,检查修改日期,然后将
'复制到另一个文件夹(名为Old)
对于每个文件在文件中
n = fil.Name
d = fil.DateLastModified
如果d> =日期 - 1然后
文件= n
'以下行是发生错误的地方
fso.CopyFileC:\Users\Desktop\Files\file,C :\Users\Desktop\Files\Old\file

结束如果
下一个fil

End Sub


解决方案

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



如果相反,该行是



fso.CopyFile fil.PathC :\Users\Desktop\Files\Old\&



更新为添加:



我刚刚尝试使用(下面的计算机用户名),并成功地将所有内容移动到一个新的文件夹中:

 子测试()
Dim fso As FileSystemObject
Dim fsoFiles As Files
Dim fil As File

设置fso =新建FileSystemObject
设置fils = fso.GetFolder( C:\Users\< MY USERNAME> \Desktop\)。文件

对于每个文件在文件中
n = fil.Name
d = fil。 DateLastModified
fso.CopyFile fil.Path,fil.ParentFolder& \test\& fil.Name

下一个fil
End Sub

这里的区别是,我使用fil.ParentFolder获取我的桌面,然后将其扔到我在桌面上(在运行该脚本之前)名为test的新文件夹中。


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.

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?

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

解决方案

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.

If instead the line was

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

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".

这篇关于使用vba将文件从一个文件夹复制到另一个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆