Link to home
Start Free TrialLog in
Avatar of rakkad
rakkadFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Create a vbscript that will copy files which include wildcards

I need a vbscript that will copy files which include wildcards..

1. Check certain files e.g Y01A*.*,  GPL.MT940*.*, GPL.CRCSV*.* exist in a file location \\cslwinapp08\bacs, check that three files exist if not,  write a log stating 'files don't exist', report a log to state which files are missing.
 2. if these three files exist in \\cslwinapp08\bacs copy these files to \\cslwinapp10\bacs
 3. Check that these files have been copied to \\cslwinapp10\bacs and write a log to confirm 'files copied'

Thanks
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Well this turned out to be a bit of a project but I hope you find it useful:
Option Explicit

Const C_SOURCE = "\\cslwinapp08\bacs"
Const C_DEST = "\\cslwinapp10\bacs"

Dim arrPatterns, objFSO, objLog, intCount, intFound, strPattern, strFile, arrRE(), arrFound()

' configure your file patterns here
arrPatterns = Array("Y01A*.*", "GPL.MT940*.*", "GPL.CRCSV*.*")

ReDim arrRE(UBound(arrPatterns)), arrFound(UBound(arrPatterns))

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLog = objFSO.OpenTextFile("logfile.txt", 2, True)

objLog.WriteLine "start - " & Now()

intCount = -1
intFound = 0

' init Regular Expression objects for file pattern matching
For Each strPattern In arrPatterns
	intCount = intCount + 1
	Set arrRE(intCount) = New RegExp
	arrRE(intCount).Pattern = Replace(Replace(Replace(arrPatterns(intCount), ".", "\."), "?", "."), "*", ".*") ' in a file pattern '.' needs to be literal, '?' is any character and '*' 0 or more characters...
	arrRE(intCount).IgnoreCase = True
Next

' check if each of the patterns has at least 1 matching file
For Each strFile In objFSO.GetFolder(C_SOURCE).Files
	For intCount = 0 To UBound(arrPatterns)
		If Not arrFound(intCount) Then
			If arrRE(intCount).Test(objFSO.GetFileName(strFile)) Then
				'objLog.WriteLine "found: '" & arrPatterns(intCount) & "': " & strFile
				arrFound(intCount) = True
				intFound = intFound + 1
				Exit For
			End If
		End If
	Next
Next

' copy if all are found
If intFound = UBound(arrPatterns) + 1 Then
	For intCount = 0 To UBound(arrFound)
		objFSO.CopyFile objFSO.BuildPath(C_SOURCE, arrPatterns(intCount)), C_DEST
	Next
	objLog.WriteLine "files copied and verified, count of mismatches: " & VerifyFiles()
Else
	objLog.WriteLine "files don't exist: " & ShowMissing()
End If

objLog.WriteLine "done - " & Now()

' clean up
objLog.Close
Set objLog = Nothing
Set objFSO = Nothing
For intCount = 0 To UBound(arrRE)
	Set arrRE(intCount) = Nothing
Next

' helper functions

Function VerifyFiles
	VerifyFiles = 0
	For Each strFile In objFSO.GetFolder(C_SOURCE).Files
		For intCount = 0 To UBound(arrPatterns)
			If arrRE(intCount).Test(objFSO.GetFileName(strFile)) Then
				'objLog.WriteLine "file '" & objFSO.GetFileName(strFile) & "' matches '" & arrRE(intCount).Pattern & "'"
				If objFSO.OpenTextFile(strFile, 1, False).ReadAll = objFSO.OpenTextFile(objFSO.BuildPath(C_DEST, objFSO.GetFileName(strFile)), 1, False).ReadAll Then
					objLog.WriteLine "verify ok: " & strFile
				Else
					VerifyFiles = VerifyFiles + 1
					objLog.WriteLine "verify failed: " & strFile
				End If
				Exit For
			Else
				'objLog.WriteLine "file '" & strFile & "' does not match '" & arrRE(intCount).Pattern & "'"
			End If
		Next
	Next
End Function

Function ShowMissing
	ShowMissing = ""
	For intCount = 0 To UBound(arrFound)
		If Not arrFound(intCount) Then
			If ShowMissing <> "" Then ShowMissing = ShowMissing & ", "
			ShowMissing = ShowMissing & "'" & arrPatterns(intCount) & "'"
		End If
	Next
End Function

Open in new window

Avatar of rakkad

ASKER

I think I may have not explained myself properly...

Essentially the three files will be created on a daily basis which has a date/stamp, so:-


Y01A.100012.20141104
GPL.MT940.100013.20141104
GPL.CRCSV.100013.20141104

So therefore, I want to include wildcards in the filenames, so the files can be copied

e.g

Y01A.*.*
GPL.MT940.*.*
GPL.CRCSV.*.*

Thanks
The patterns are on line 9. I tested with these filenames and the script works. Did you encounter a specific problem or does the script do something you don't want?
If you want some more logging, remove the ' in front of some of the lines like 70, 79.
Avatar of rakkad

ASKER

The files will be presented in the format as specified i.e the date created..

But use wildcards to copy the complete file, similar process if using a copy command in dos using wildcards for filenames

Hope this sounds clear ?

Thanks
That is exactly what the script does, on line 46. But you asked for a check that all 3 files are present so that's what most of the rest of the code is for ;-)
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial