Solved

Create a vbscript that will copy files which include wildcards

Posted on 2014-11-05
7
211 Views
Last Modified: 2014-11-10
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
0
Comment
Question by:rakkad
  • 4
  • 2
7 Comments
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 40424884
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

0
 

Author Comment

by:rakkad
ID: 40425230
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
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 40425243
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?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 35

Expert Comment

by:Robert Schutt
ID: 40425246
If you want some more logging, remove the ' in front of some of the lines like 70, 79.
0
 

Author Comment

by:rakkad
ID: 40425261
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
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 40425264
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 ;-)
0
 
LVL 52

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 40425446
Feels like BAT might be a better fit for this, something like:

@echo off
setlocal

REM Define files to copy
set BaseDir=\\cslwinapp08\bacs
set DestDir=\\cslwinapp10\bacs
set CopyFiles="Y01A*.*","GPL.MT940*.*","GPL.CRCSV*.*"

REM Make sure all desired files exist
for %%A in (%CopyFiles%) do (
  if not exist "%BaseDir%\%%~A" goto :FilesMissing
)

REM Copy all desired files
for %%A in (%CopyFiles%) do (
  copy "%BaseDir%\%%~A" "%DestDir%\%%~A"
)

REM Make sure all files copied
for %%A in (%CopyFiles%) do (
  if not exist "%DestDir%\%%~A" (
    echo Files not copied: "%DestDir%\%%~A"
  )
)

REM Exit script
exit /b

:FilesMissing
  echo Files missing from base dir.

Open in new window

~bp
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

863 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

19 Experts available now in Live!

Get 1:1 Help Now