VBA 根据文件名中的字符串移动文件
VBA move files based on string in filename
我们收到 Word 文件,文件名中包含 ISO 3 字母国家/地区代码字符串。我们想将这些文件从保留文件夹移动到每个国家/地区的现有文件夹中,该文件夹以国家/地区名称命名。
我一直在兜圈子试图让 InStr
工作。 InStr
更可取,因为字符串在文件名中的位置可以更改。
有人可以告诉我我的代码有什么问题吗?它运行,但什么都不做。
还有很多国家文件(60+ ISO 代码)。有没有比 For Each
和 ElseIf
更聪明的方法?
非常感谢!
Sub MoveFiles_SpecificFolders()
Dim SrepFSO As FileSystemObject
Dim Srep As File
Dim HldFolder As Folder
Dim HoldingFolder As String
Dim TargetFolder As String
Dim Fname As String
Fname = (HoldingFolder & "*.doc*")
HoldingFolder = "C:\Users\Temp\Test\"
TargetFolder = "C:\Users\DifferentPath\MSfolders\"
Set SrepFSO = New Scripting.FileSystemObject
Set HldFolder = SrepFSO.GetFolder(HoldingFolder)
For Each Srep In HldFolder.Files
If InStr(Fname, "ALB") <> 0 Then
SrepFSO.MoveFile Source:=SrepFSO.GetFile(Srep), _
Destination:=TargetFolder & "Albania\" & Srep.Name
ElseIf InStr(Fname, "AND") <> 0 Then
SrepFSO.MoveFile Source:=SrepFSO.GetFile(Srep), _
Destination:=DestinationFolder & "Andorra\" & Srep.Name
ElseIf InStr(Fname, "ARM") <> 0 Then
SrepFSO.MoveFile Source:=SrepFSO.GetFile(Srep), _
Destination:=DestinationFolder & "Armenia\" & Srep.Name
End If
Next Srep
End Sub
按照您的代码执行此操作:
Dim HoldingFolder As String
Dim TargetFolder As String
Dim Fname As String
Fname = (HoldingFolder & "*.doc*")
HoldingFolder = "C:\Users\Temp\Test\"
Fname
变量不包含HoldingFolder
的部分。 HoldingFolder
变量应在引用前赋值:
Dim HoldingFolder As String
Dim TargetFolder As String
Dim Fname As String
HoldingFolder = "C:\Users\Temp\Test\"
Fname = (HoldingFolder & "*.doc*")
稍后在 For-Each 循环中,您会在这里看到一个问题 -
If InStr(Fname, "ALB") <> 0 Then
-> 应该是If InStr(Srep, "ALB") <> 0 Then
感谢@BigBen 和@Vityata 更正了代码。
Sub MoveFiles_SpecificFolders()
Dim SrepFSO As FileSystemObject
Dim Srep As File
Dim fso As New FileSystemObject
Dim HldFolder As Folder
Dim HoldingFolder As String
Dim TargetFolder As String
HoldingFolder = "C:\Users\xyz\Test_docs\"
TargetFolder = "C:\Users\xyz\MSfolders\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set SrepFSO = New Scripting.FileSystemObject
Set HldFolder = SrepFSO.GetFolder(HoldingFolder)
For Each Srep In HldFolder.Files
If InStr(Srep, "ALB") <> 0 Then
SrepFSO.MoveFile Source:=SrepFSO.GetFile(Srep), _
Destination:=TargetFolder & "Albania\" & Srep.Name
ElseIf InStr(Srep, "AND") <> 0 Then
SrepFSO.MoveFile Source:=SrepFSO.GetFile(Srep), _
Destination:=TargetFolder & "Andorra\" & Srep.Name
ElseIf InStr(Srep, "ARM") <> 0 Then
SrepFSO.MoveFile Source:=SrepFSO.GetFile(Srep), _
Destination:=TargetFolder & "Armenia\" & Srep.Name
ElseIf InStr(Srep, "GEO") <> 0 Then
SrepFSO.MoveFile Source:=SrepFSO.GetFile(Srep), _
Destination:=TargetFolder & "Georgia\" & Srep.Name
End If
Next Srep
End Sub
我们收到 Word 文件,文件名中包含 ISO 3 字母国家/地区代码字符串。我们想将这些文件从保留文件夹移动到每个国家/地区的现有文件夹中,该文件夹以国家/地区名称命名。
我一直在兜圈子试图让 InStr
工作。 InStr
更可取,因为字符串在文件名中的位置可以更改。
有人可以告诉我我的代码有什么问题吗?它运行,但什么都不做。
还有很多国家文件(60+ ISO 代码)。有没有比 For Each
和 ElseIf
更聪明的方法?
非常感谢!
Sub MoveFiles_SpecificFolders()
Dim SrepFSO As FileSystemObject
Dim Srep As File
Dim HldFolder As Folder
Dim HoldingFolder As String
Dim TargetFolder As String
Dim Fname As String
Fname = (HoldingFolder & "*.doc*")
HoldingFolder = "C:\Users\Temp\Test\"
TargetFolder = "C:\Users\DifferentPath\MSfolders\"
Set SrepFSO = New Scripting.FileSystemObject
Set HldFolder = SrepFSO.GetFolder(HoldingFolder)
For Each Srep In HldFolder.Files
If InStr(Fname, "ALB") <> 0 Then
SrepFSO.MoveFile Source:=SrepFSO.GetFile(Srep), _
Destination:=TargetFolder & "Albania\" & Srep.Name
ElseIf InStr(Fname, "AND") <> 0 Then
SrepFSO.MoveFile Source:=SrepFSO.GetFile(Srep), _
Destination:=DestinationFolder & "Andorra\" & Srep.Name
ElseIf InStr(Fname, "ARM") <> 0 Then
SrepFSO.MoveFile Source:=SrepFSO.GetFile(Srep), _
Destination:=DestinationFolder & "Armenia\" & Srep.Name
End If
Next Srep
End Sub
按照您的代码执行此操作:
Dim HoldingFolder As String
Dim TargetFolder As String
Dim Fname As String
Fname = (HoldingFolder & "*.doc*")
HoldingFolder = "C:\Users\Temp\Test\"
Fname
变量不包含HoldingFolder
的部分。 HoldingFolder
变量应在引用前赋值:
Dim HoldingFolder As String
Dim TargetFolder As String
Dim Fname As String
HoldingFolder = "C:\Users\Temp\Test\"
Fname = (HoldingFolder & "*.doc*")
稍后在 For-Each 循环中,您会在这里看到一个问题 -
If InStr(Fname, "ALB") <> 0 Then
-> 应该是If InStr(Srep, "ALB") <> 0 Then
感谢@BigBen 和@Vityata 更正了代码。
Sub MoveFiles_SpecificFolders()
Dim SrepFSO As FileSystemObject
Dim Srep As File
Dim fso As New FileSystemObject
Dim HldFolder As Folder
Dim HoldingFolder As String
Dim TargetFolder As String
HoldingFolder = "C:\Users\xyz\Test_docs\"
TargetFolder = "C:\Users\xyz\MSfolders\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set SrepFSO = New Scripting.FileSystemObject
Set HldFolder = SrepFSO.GetFolder(HoldingFolder)
For Each Srep In HldFolder.Files
If InStr(Srep, "ALB") <> 0 Then
SrepFSO.MoveFile Source:=SrepFSO.GetFile(Srep), _
Destination:=TargetFolder & "Albania\" & Srep.Name
ElseIf InStr(Srep, "AND") <> 0 Then
SrepFSO.MoveFile Source:=SrepFSO.GetFile(Srep), _
Destination:=TargetFolder & "Andorra\" & Srep.Name
ElseIf InStr(Srep, "ARM") <> 0 Then
SrepFSO.MoveFile Source:=SrepFSO.GetFile(Srep), _
Destination:=TargetFolder & "Armenia\" & Srep.Name
ElseIf InStr(Srep, "GEO") <> 0 Then
SrepFSO.MoveFile Source:=SrepFSO.GetFile(Srep), _
Destination:=TargetFolder & "Georgia\" & Srep.Name
End If
Next Srep
End Sub