Solved

Create a vbscript that will copy files which include wildcards

Posted on 2014-11-05
7
215 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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 54

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

749 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