簡體   English   中英

如何使用VBA打開文本文件並將其移動到文件夾?

[英]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.

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