Sub CopyFilesContainingString(sourceFolder As String, targetFolder As String, searchString As String)
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
' 대상 폴더가 존재하지 않으면 생성
If Not fso.FolderExists(targetFolder) Then fso.CreateFolder(targetFolder)
' 폴더 탐색 및 파일 복사
SearchAndCopyFiles fso.GetFolder(sourceFolder), searchString, targetFolder, fso, 0
MsgBox "파일 복사가 완료되었습니다!"
End Sub
Sub SearchAndCopyFiles(folder As Object, searchString As String, targetFolder As String, fso As Object, depth As Integer)
Dim file As Object, subFolder As Object
' 현재 폴더의 파일 탐색 및 복사
For Each file In folder.Files
If InStr(file.Name, searchString) > 0 Then file.Copy fso.BuildPath(targetFolder, file.Name), True
Next
' 하위 폴더 탐색 (재귀 호출)
For Each subFolder In folder.SubFolders
' 하위 폴더 깊이에 따라 조건 결정
If depth < 2 Or InStr(subFolder.Name, searchString) > 0 Then
SearchAndCopyFiles subFolder, searchString, targetFolder, fso, depth + 1
End If
Next
End Sub
Sub ExecuteCopy()
' 초기 설정
CopyFilesContainingString "C:\abc", "C:\bbc", "특정 문자열" ' 여기에 찾고자 하는 문자열을 입력하세요
End Sub
카테고리 없음
댓글