如何使用文件名的部分字符串在vba上将一个文件从一个目录移动到另一个目录?

时间:2021-07-12 00:00:11

I have a report that is generated daily and automatically. I need a code that moves the file from one directory to another, taking in account that I need to use it daily, and only a part of the file name is constant (the rest varies each day without a defined pattern).

我有一个每天自动生成的报告。我需要一个代码将文件从一个目录移动到另一个目录,考虑到我需要每天使用它,并且只有一部分文件名是常量的(其余每天都有不同的定义模式)。

I would be very gradeful for your help, I'm a beginner on vba and I need help, if you need additional explanation ask me for it.

我会非常有资格帮助你,我是vba的初学者,我需要帮助,如果你需要额外的解释请问我。

Thank you very much

非常感谢你

2 个解决方案

#1


1  

Please try the code below:

请尝试以下代码:

Sub Move_Folder()

    Dim FSO As Object
    Dim SourcePath As String
    Dim DestPath As String
    Dim NameFile as String

    SourcePath = "C:\Users\Adhy\"  '<< Change as needed
    DestPath = "C:\Users\Mauro\"    '<< Change as needed
    NameFile  = "Sheet_1*.xl*"      '<< Change as needed

    Set FSO = CreateObject("scripting.filesystemobject")

    If FSO.FolderExists(Left(SourcePath,Len(SourcePath)-1)) = False Then
        MsgBox SourcePath & " doesn't exist"
        Exit Sub
    End If

    'to move
    FSO.MoveFile Source:=SourcePath & NameFile , Destination:=DestPath

End Sub

Hope this help.

希望这有帮助。

#2


1  

try this code

试试这段代码

Sub formatchange()

    Dim objFSO As Object
    Dim objFolder As Object
    Dim objFile As Object

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder("your path")

    Application.ScreenUpdating = False 'for a faster code
    For Each objFile In objFolder.Files
       If InStr(1, objFile.Name, "searched value") > 0 Then
         objFSO.MoveFile objFile, "yournewpath"
       End If
    Next

    'Clean up!
    Set objFolder = Nothing
    Set objFile = Nothing
    Set objFSO = Nothing

    Application.ScreenUpdating = True 'turn on updatin again
End Sub

#1


1  

Please try the code below:

请尝试以下代码:

Sub Move_Folder()

    Dim FSO As Object
    Dim SourcePath As String
    Dim DestPath As String
    Dim NameFile as String

    SourcePath = "C:\Users\Adhy\"  '<< Change as needed
    DestPath = "C:\Users\Mauro\"    '<< Change as needed
    NameFile  = "Sheet_1*.xl*"      '<< Change as needed

    Set FSO = CreateObject("scripting.filesystemobject")

    If FSO.FolderExists(Left(SourcePath,Len(SourcePath)-1)) = False Then
        MsgBox SourcePath & " doesn't exist"
        Exit Sub
    End If

    'to move
    FSO.MoveFile Source:=SourcePath & NameFile , Destination:=DestPath

End Sub

Hope this help.

希望这有帮助。

#2


1  

try this code

试试这段代码

Sub formatchange()

    Dim objFSO As Object
    Dim objFolder As Object
    Dim objFile As Object

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder("your path")

    Application.ScreenUpdating = False 'for a faster code
    For Each objFile In objFolder.Files
       If InStr(1, objFile.Name, "searched value") > 0 Then
         objFSO.MoveFile objFile, "yournewpath"
       End If
    Next

    'Clean up!
    Set objFolder = Nothing
    Set objFile = Nothing
    Set objFSO = Nothing

    Application.ScreenUpdating = True 'turn on updatin again
End Sub