将文件从一个文件夹移动到另一个文件夹

9

实际上,我正在搜索移动Excel文件的代码,如果有任何方法可以这样做,请有人帮助我。非常抱歉,但我不知道如何编写代码,因为我从未使用过VBA,事实上,我第一次见到它。

我将非常感激你的帮助。


现在有一个Name语句可以在一行内完成此操作。 - user7868
7个回答

19
Sub MoveFiles()
    Dim FSO As Object
    Dim SourceFileName As String, DestinFileName As String

    Set FSO = CreateObject("Scripting.Filesystemobject")
    SourceFileName = "C:\Users\Jun.xlsx"
    DestinFileName = "C:\Users\Desktop\Jun.xlsx"

    FSO.MoveFile Source:=SourceFileName, Destination:=DestinFileName

    MsgBox (SourceFileName + " Moved to " + DestinFileName)
End Sub

1
声明 FSO 为 FileSystemObject。 - Jean-Charles LUC

3
尝试使用以下代码:
Sub test()
    Set fso = CreateObject("scripting.filesystemobject")
    fso.MoveFile Source:="C:\work\test1.xlsx", Destination:="c:\work\movecheck\" ' replace with source and destination as required.
End Sub

您必须提供完整的文件路径作为目标。仅仅带一个文件夹会导致错误。 - Julian Kuchlbauer

3
以下是将源文件夹中的Excel(xlsx)文件移动到目标文件夹的代码。其他类型的文件将保留在目标文件夹中。
Sub MoveFiles()

Dim sourceFolderPath As String, destinationFolderPath As String
Dim FSO As Object, sourceFolder As Object, file As Object
Dim fileName As String, sourceFilePath As String, destinationFilePath As String

Application.ScreenUpdating = False

sourceFolderPath = "D:\SourceFolder"
destinationFolderPath = "D:\DestinationFolder"

Set FSO = CreateObject("Scripting.FileSystemObject")
Set sourceFolder = FSO.Getfolder(sourceFolderPath)

For Each file In sourceFolder.Files
    fileName = file.Name
    If InStr(fileName, ".xlsx") Then ' Only xlsx files will be moved
        sourceFilePath = file.Path
        destinationFilePath = destinationFolderPath & "\" & fileName
        FSO.MoveFile Source:=sourceFilePath, Destination:=destinationFilePath
    End If ' If InStr(sourceFileName, ".xlsx") Then' Only xlsx files will be moved
Next

'Don't need set file to nothing because it is initialized in for each loop 
'and after this loop is automatically set to Nothing
Set sourceFolder = Nothing
Set FSO = Nothing

End Sub

如果您只需要移动一个文件,则最佳解决方案是:

Name sourceFolderPath & fileName As destinationFilePath

2
您可以使用Filesystemobject对象:
Dim FSO as Object
Set FSO = CreateObject("Scripting.Filesystemobject")
FSO.MoveFile("SourceFileName", "TargetFileName")

如果您需要进一步的说明,请随意发表评论。


这个能用通配符吗?例如,将以“Test_”开头的任何文件从xyz目录移动到相同的名称? - Vnge
在我的电脑上,Excel 2013和这个代码编辑器无法接受。请尝试使用 FSO.MoveFile Source:=sourceFilePath, Destination:=destinationFilePath。 - Sharunas Bielskis

1

1
尝试:
Sub movefile()
Dim fso As New FileSystemObject
Dim fil As file
Dim SourcePath, BackUpPath As String
    
SourcePath = "C:\mydata\"
BackUpPath = "G:\Backup\"

    For Each fil In fso.GetFolder(SourcePath).Files
        fName = fso.GetFileName(fil)
        Name fil As BackUpPath & fName
    Next fil
End Sub

1
Sub move_data()
    'Move test data to folder

    Dim FSO As Object
    Dim FromPath As String
    Dim ToPath As String
    Dim Fdate As Date
    Dim FileInFromFolder As Object


    MkDir "D:\TEST\"        'Create new folder name TEST in D:
    FromPath = "E:\test\"   'Source files
    ToPath = "D:\TEST\"     'Target destination

    Set FSO = CreateObject("scripting.filesystemobject")

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

    For Each FileInFromFolder In FSO.getfolder(FromPath).Files
        FileInFromFolder.move ToPath
    Next FileInFromFolder

End Sub

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接