簡體   English   中英

將文件從一個文件夾移動到另一個文件夾

[英]move files from one folder to another

實際上,如果有任何方法,我正在尋找將excel文件從一個文件夾移動到另一個文件夾的代碼請有人幫助我。 我很抱歉,但我不知道如何進行編碼,因為我從未使用過 VBA,事實上我是第一次看到它。

我會感激你

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

試試下面的代碼

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

下面是僅將 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

您可以使用文件系統對象:

Dim FSO as Object
Set FSO = CreateObject("Scripting.Filesystemobject")
FSO.MoveFile("SourceFileName", "TargetFileName")

如果您需要進一步的說明,請隨時發表評論。

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

嘗試:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM