move files from one folder to another

前端 未结 5 903
广开言路
广开言路 2020-12-18 07:27

actually I am searching for code to move excel files from one folder to another if there is any way to do so Please someone help me. I am very sorry but I dont know how to d

相关标签:
5条回答
  • 2020-12-18 08:17

    Below is code which moves only Excel (xlsx) files from source folder into destination folder. Other types files will be left in the destination folder.

    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
    

    If you need move only one file the best solution is:

    Name sourceFolderPath & fileName As destinationFilePath
    
    0 讨论(0)
  • 2020-12-18 08: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
    
    0 讨论(0)
  • 2020-12-18 08:21

    Try with the below code

    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
    
    0 讨论(0)
  • 2020-12-18 08:21

    You can use the Filesystemobject:

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

    Feel free to comment, if you need further instructions.

    0 讨论(0)
  • 2020-12-18 08:21
    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
    
    0 讨论(0)
提交回复
热议问题