Hello all! This is my first script windows script I've ever written and I managed (eventually) to get it to work by scouring here and the internet for snippets and I managed to mangle it together. The problem is that it runs SLOOOW. I'm guessing because I'm searching for the files in a really dumb way, but this the only way I could figure out. 
Basically the script is supposed to:
1) read a text file formatted in a specific way and grab the file name from each line (Does this great)
2) search for those files in a specified directory (using a "BrowseForFolder" window, objPath variable)
3) place found files in specified directory (again, using a "BrowseForFolder" window, objPathTarget variable)
4) Not take 3+ hours to find 50 files, lol
I tried putting the ShowSubFolders bit inside the Do loop, but it crashed on Sub ShowSubFolders(Folder).
Thank you for any guidance!
-brian
I call a BrowseForFolder window to get Source Path (objPath)
And another for Target Path (objPathTarget)... didnt' see the need to post the code and take up space
''''''''''''''''''''''''' Begin reading text file for file names and copy the files from all subdirectories
Set objFSO = CreateObject("Scripting.FileSystemObject")
ShowSubfolders objFSO.GetFolder(objPath)
Sub ShowSubFolders(Folder)
For Each Subfolder In Folder.SubFolders
Set objFile = objFSO.OpenTextFile("C:\Scripts\test.txt")
Do Until objFile.AtEndOfStream
strData = ""
strSearchString = objFile.ReadLine
intStart = InStr(strSearchString, "6) (")
If intStart <> 0 Then
intStart = intStart + 4
strText = Mid(strSearchString, intStart, 50)
For i = 1 To Len(strText)
If Mid(strText, i, 1) = ")" Then
Exit For
Else
strData = strData & Mid(strText, i, 1)
End If
Next
End If
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='" & Subfolder.Path & "'} Where " & "ResultClass = CIM_DataFile")
For Each objFile2 In colFiles
If LCase(strData) = objFile2.Filename Then ' Won't match unless use LCase
strCopy = objPathTarget & "\" & UCase(objFile2.FileName) & "." & UCase(objFile2.Extension) 'Put the file back to uppercase, the way it was
objFile2.Copy(strCopy)
End If
Next
Loop
ShowSubFolders Subfolder
Next
End Sub
Basically the script is supposed to:
1) read a text file formatted in a specific way and grab the file name from each line (Does this great)
2) search for those files in a specified directory (using a "BrowseForFolder" window, objPath variable)
3) place found files in specified directory (again, using a "BrowseForFolder" window, objPathTarget variable)
4) Not take 3+ hours to find 50 files, lol
I tried putting the ShowSubFolders bit inside the Do loop, but it crashed on Sub ShowSubFolders(Folder).
Thank you for any guidance!
-brian
I call a BrowseForFolder window to get Source Path (objPath)
And another for Target Path (objPathTarget)... didnt' see the need to post the code and take up space
''''''''''''''''''''''''' Begin reading text file for file names and copy the files from all subdirectories
Set objFSO = CreateObject("Scripting.FileSystemObject")
ShowSubfolders objFSO.GetFolder(objPath)
Sub ShowSubFolders(Folder)
For Each Subfolder In Folder.SubFolders
Set objFile = objFSO.OpenTextFile("C:\Scripts\test.txt")
Do Until objFile.AtEndOfStream
strData = ""
strSearchString = objFile.ReadLine
intStart = InStr(strSearchString, "6) (")
If intStart <> 0 Then
intStart = intStart + 4
strText = Mid(strSearchString, intStart, 50)
For i = 1 To Len(strText)
If Mid(strText, i, 1) = ")" Then
Exit For
Else
strData = strData & Mid(strText, i, 1)
End If
Next
End If
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='" & Subfolder.Path & "'} Where " & "ResultClass = CIM_DataFile")
For Each objFile2 In colFiles
If LCase(strData) = objFile2.Filename Then ' Won't match unless use LCase
strCopy = objPathTarget & "\" & UCase(objFile2.FileName) & "." & UCase(objFile2.Extension) 'Put the file back to uppercase, the way it was
objFile2.Copy(strCopy)
End If
Next
Loop
ShowSubFolders Subfolder
Next
End Sub