Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBScript that copy files with similiar string to new folder

Status
Not open for further replies.
Jan 28, 2004
19
US
I am new to VBScript and I was task to create a VBScript that would read a list of (.txt)files in folderA and to copy all files with a matching string to a new folder like folderB


Thanks for your help in this regard...

my current code only copy files from one folder to the next

my code is below:

Option Explicit
dim kfso
Set kfso=CreateObject("Scripting.FileSystemObject")
kfso.CopyFile "c:\backup\test01\*.txt", "c:\backup\test02\report\"


 
Here is one way to do it.

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colFiles = objFSO.GetFolder("C:\FolderA").Files
strMatch="Copy" 'This is the string to search for.

For Each objFile In colFiles
i=0
i=InStr(1,objFile.Name,strMatch,vbTextCompare)'vbTextCompare or vbBinaryCompare
If i<>0 Then
objFile.Copy "C:\FolderB\", True
Wscript.Echo "Matching file name found"
End If
Next
 
I am new to VBScript and I was task to create a VBScript that would read a list of (.txt)files in folderA and to copy all files with a matching string in each file to a new folder like folderB.

Thanks for your help in this regard...

The VBscript I received checked the filenames for the string but the string should match the contents of the files
then copy the matches to folderB.

Thanks Again for your help...

Here is the current script:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colFiles = objFSO.GetFolder("C:\BACKUP\TEST01").Files
strMatch="ellis" 'This is the string to search for.

For Each objFile In colFiles
i=0
i=InStr(1,objFile.name,strMatch,vbTextCompare)'vbTextCompare or vbBinaryCompare
If i<>0 Then
objFile.Copy "C:\backup\test02\", True
Wscript.Echo "Matching file name found"
End If
Next

 
Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
strMatch="ellis" 'This is the string to search for.
For Each objFile In objFSO.GetFolder("C:\BACKUP\TEST01").Files
  If InStr(1,objFSO.OpenTextFile(objFile.Path).ReadAll,strMatch,vbTextCompare) > 0 Then
    objFile.Copy "C:\backup\test02\", True
  End If
Next

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I am new to VBScript and I was task to create a VBScript that would read a list of (.txt)files in folderA and to copy all files with a matching string in each file to a new folder like folderB.

Thanks for your help in this regard...

The VBscript I received is working but If there is an empty file in the folder it give an error message stating:" Input past end of file" I checked on the error and find out i need to check the size of the files before input, however when I try the script don't run.

also I Would like to copy the unmatched file to another folder like FolderC

Thanks Again for your help...

Here is the current script:

Set objFSO = CreateObject("Scripting.FileSystemObject")
strMatch="summer Lab" 'This is the string to search for.
For Each objFile In objFSO.GetFolder("C:\BACKUP\TEST01").Files
if InStr(1,objFSO.OpenTextFile(objFile.Path).ReadAll,strMatch,vbTextCompare) > 0 Then
objFile.Move "C:\backup\test02\"

'objFile.Copy "C:\backup\test02\", True
'objFSO.CopyFile "c:\backup\test01\*.*","c:\backup\TESTSBK\"

End If

Next

 
See if this works.

Set objFSO = CreateObject("Scripting.FileSystemObject")
strMatch="ellis" 'This is the string to search for.
For Each objFile In objFSO.GetFolder("C:\backup\test01\").Files
If objFile.size>0 Then
If InStr(1,objFSO.OpenTextFile(objFile.Path).ReadAll,strMatch,vbTextCompare) > 0 Then
objFile.Copy "C:\backup\test02\", True
Else objFile.Copy "C:\backup\test03\", True
End if
End If
Next
 
I am new to VBScript and I was task to create a VBScript that would read a list of (.txt)files in folderA and to copy all files with a matching string in each file to a new folder like folderB.

Thanks for your help in this regard...

Just few modifications and it worked...

THANKS A MILLION...

The script below HELPED...


Set objFSO = CreateObject("Scripting.FileSystemObject")
strMatch="ellis" 'This is the string to search for.
For Each objFile In objFSO.GetFolder("C:\backup\test01\").Files
If objFile.size>0 Then
If InStr(1,objFSO.OpenTextFile(objFile.Path).ReadAll,strMatch,vbTextCompare) > 0 Then
objFile.Copy "C:\backup\test02\", True
Else objFile.Copy "C:\backup\test03\", True
End if
End If
Next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top