[英]How to open text files and move them to a folder using VBA?
我需要一个项目的帮助,在该项目中,我必须打开一个文本文件列表,在其内容中找到一个模式,然后根据该模式移动到其他文件夹。
例如,在文本文件列表中,我必须找到其中有一个单词“ blue”的文件,并且仅将它们移动到另一个名为“ Blue”的文件夹中。
我本来想使用FileSystemObject命令来执行此操作,但是我有点迷失了。
在此先多谢!!
Dim sDir As String
Dim sPath As String
Dim sPattern as String
Dim sReadedData as String
dim sDestiny as string
dim sPathDestiny as string
Dim fso As Object
Set fso = VBA.CreateObject("Scripting.FileSystemObject")
sPath$ = "c:\YourFolder"
sDir$ = Dir(sPath, vbDirectory)
sPattern= "abcdefg"
sDestiny="c:\DestinyFolder"
If sDir = "" Then
MsgBox "Path " & sDir & " Not Found"
End
End If
sDir$ = Dir(sPath & "\*.txt")
Do Until sDir = ""
sPathDestiny=Replace(sDir, sPath, sDestiny)
Open sDir$ For Input As #1
do until EOF(1)
Input #1, sReadedData
loop
if InStr(sReadedData, sPattern)>0 then
Call fso.CopyFile(sDir, sPathDestiny)
end if
Loop
这是主要思想。 玩吧。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.