Solved

search all folders, and copy files with a matching text string (regardless of file ext) into a target folder.

Posted on 2013-11-21
10
287 Views
Last Modified: 2013-12-03
I'm looking for a bat file or macro to run through an existing excel or text file listing around 57000 numbers (which consist of both numbers and text). Find the corresponding files which contains an identical text string (but which may have additional characters in the string at the end not limited to the file extension). The search function should search all sub folders within an identified root folder. The identified files should then be copied (not moved) to an identified  target folder.
0
Comment
Question by:Alistair_Mair
  • 5
  • 5
10 Comments
 
LVL 35

Expert Comment

by:Kimputer
Comment Utility
VBScript:

Those things need to be adjusted to your own:


scan_dir = "c:\windows\" (folder to be scanned)
textfile = "c:\temp\temp.txt" (here are the parts of the file names)
copy_dir = "c:\temp\" (files will be copied here)



Option explicit

Const ForReading = 1
Const ForWriting = 2

Dim scan_dir,copy_dir,folder,fileCollection,file_read,test_text,textfile,ObjFSO,folderCollection,file,subfolder

scan_dir = "c:\windows\"
textfile = "c:\temp\temp.txt"
copy_dir = "c:\temp\"


Set ObjFSO = CreateObject("Scripting.FileSystemObject")

Set file_read = ObjFSO.OpenTextFile(textfile, ForReading)
	Do Until file_read.AtEndOfStream
		test_text = file_read.Readline
		searchsub scan_dir, test_text
	Loop	
Set file_read = nothing
			
	
	
	
	
Sub searchsub(Source, text)
	Set folder = ObjFSO.GetFolder(Source)
	Set fileCollection = folder.Files
	Set folderCollection = folder.Subfolders
	
	For Each file In fileCollection
		if instr(file.name, text) then
			ObjFSO.CopyFile file, copy_dir & file.name
		end if
	Next
	
	For Each subfolder In folderCollection
		'wscript.echo subfolder & text
		searchsub subfolder, text
	Next

End sub

	

Open in new window

0
 

Author Comment

by:Alistair_Mair
Comment Utility
Hi Kimputer,

changed the parameters as requested and Copied your script as detailed below into the macro application on excel 2010 and I get the error "compile error, invalid outside procedure"  ??

--

Option Explicit

Const ForReading = 1
Const ForWriting = 2

Dim scan_dir, copy_dir, folder, fileCollection, file_read, test_text, textfile, ObjFSO, folderCollection, file, subfolder

scan_dir = "c:\DRAGADOS\"
textfile = "c:\temp\temp.txt"
copy_dir = "c:\temp\P3FILES\"


Set ObjFSO = CreateObject("Scripting.FileSystemObject")

Set file_read = ObjFSO.OpenTextFile(textfile, ForReading)
    Do Until file_read.AtEndOfStream
        test_text = file_read.Readline
        searchsub scan_dir, test_text
    Loop
Set file_read = Nothing
           
   
   
   
   
Sub searchsub(Source, text)
    Set folder = ObjFSO.GetFolder(Source)
    Set fileCollection = folder.Files
    Set folderCollection = folder.Subfolders
   
    For Each file In fileCollection
        If InStr(file.Name, text) Then
            ObjFSO.CopyFile file, copy_dir & file.Name
        End If
    Next
   
    For Each subfolder In folderCollection
        'wscript.echo subfolder & text
        searchsub subfolder, text
    Next

End Sub
0
 
LVL 35

Expert Comment

by:Kimputer
Comment Utility
I said it was VBscript (because it's easier that way, inside Excel it's VBA). Run it at the command prompt. Copy all the code into a text file, then give the script a name, something like "search.vbs"
0
 

Author Comment

by:Alistair_Mair
Comment Utility
Sorry, got it and ran it. It does seem to be working but very slowly (been running for half and hour and so far copies 8 files). It's looking through 400gb of data for these files, Does this code specify to look for each file individually and cycle through the folders and then look for the next file with a corresponding file name and cycle through again or cycles through once virtually stores the results and then copies across to the target folder?
0
 
LVL 35

Expert Comment

by:Kimputer
Comment Utility
It could be slow indeed. I was hoping your disk and cache would kick in, but seems it doesn't. I was trying to keep the code clean. Would need a lot of time to code something to load all the files on the disk into memory and do the matching there.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:Alistair_Mair
Comment Utility
OK, it's just that at this rate it will take around 50 days to cycle through and find all the files. If it is unreasonable to expect a more workable solution I will credit you with the points?
0
 
LVL 35

Expert Comment

by:Kimputer
Comment Utility
If you could give me a day? (Could still fail though, if memory not sufficient)
0
 

Author Comment

by:Alistair_Mair
Comment Utility
I can certainly wait a day. I will credit you with at least half the points tomorrow one way or the other. If someone finds a more workable solution in the interim then half the points go to them, does that sound fair?
0
 
LVL 35

Accepted Solution

by:
Kimputer earned 500 total points
Comment Utility
Same routines, but now searches from memory (will scan folder once):

Option explicit

On Error Resume Next

Const ForReading = 1
Const ForWriting = 2

Dim scan_dir,copy_dir,folder,fileCollection,file_read,test_text,textfile,ObjFSO,folderCollection,file,subfolder
Dim FilesArray

FilesArray = Split("0")

scan_dir = "c:\DRAGADOS\"
textfile = "c:\temp\temp.txt"
copy_dir = "c:\temp\P3FILES\"

Set ObjFSO = CreateObject("Scripting.FileSystemObject")

FillArray scan_dir

wscript.echo ubound(FilesArray)

Set file_read = ObjFSO.OpenTextFile(textfile, ForReading)
	Dim j
	Do Until file_read.AtEndOfStream
		test_text = file_read.Readline
		'searchsub scan_dir, test_text
		For j = 0 to Ubound(FilesArray)
			if instr(FindName(FilesArray(j),"filename"),test_text) then
				ObjFSO.CopyFile FilesArray(j), copy_dir & FindName(FilesArray(j), "filename")
			end if
		Next
		
	Loop	
Set file_read = nothing
Wscript.echo "Done"

Sub FillArray(Source)
	Set folder = ObjFSO.GetFolder(Source)
	Set fileCollection = folder.Files
	Set folderCollection = folder.Subfolders
	
	For Each file In fileCollection
		FilesArray(Ubound(FilesArray)) = file
		redim preserve FilesArray(ubound(FilesArray) + 1)
	Next
	
	For Each subfolder In folderCollection
		FillArray subfolder
	Next

End sub

Function FindName(string, mode)
Dim a,i

if not(string = "") then

	a = split(string,"\")
	if mode = "path" then
		for i = 0 to Ubound(a) - 1
			if i = 0 then
				FindName = a(i)
			else
				if i = Ubound(a) -1 then
					FindName = FindName & "\" & a(i) & "\"
				else
					FindName = FindName & "\" & a(i)
				end if
			end if
			
		Next

	end if
	if mode = "filename" then
		FindName = a(Ubound(a))
	end if

end if
End Function

Open in new window

0
 

Author Closing Comment

by:Alistair_Mair
Comment Utility
Sorry, I got caught up in other stuff and this slipped my mind. Not tested it but appreciate the work you've put in and have assigned you the points, I apologise for the delay in getting back to you.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now