?
Solved

Create a vbscript that will copy files which include wildcards

Posted on 2014-11-05
7
Medium Priority
?
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 57

Accepted Solution

by:
Bill Prew earned 1000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
The viewer will learn how to count occurrences of each item in an array.
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…
Suggested Courses

765 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