Solved

Create a vbscript that will copy files which include wildcards

Posted on 2014-11-05
7
209 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 51

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

707 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

17 Experts available now in Live!

Get 1:1 Help Now