Check for updated version of a file by filename

I would like to write a script or batch file that sits on a client machine and checks a specified folder for an access database and compares it to the copy on the client machine.  If it finds an updated version number in the specified folder, referenced by the characters following the "v" in the filename ie. dbv4.1 (4.1), it copies the Access database from that location into a folder on the client machine and opens it.  If it does not find an updated version, it opens the most up to date in the folder in the client machine.

Many thanks in advance
cjohnson300Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobSampsonCommented:
Hi there,

Something like this might work, although I haven't tested it.

Regards,

Rob.
strLocalMDBFolder = "C:\Program Files\MyApp\"
strRemoteMDBFolder = "\\server\share\LatestDB\"
If Right(strLocalMDBFolder, 1) <> "\" Then strLocalMDBFolder = strLocalMDBFolder & "\"
Set objFSO = CreateObject("Scripting.FileSystemObject")
boolNeedToCopy = False
strLocalVersion = ""
strRemoteVersion = ""
If objFSO.FolderExists(strLocalMDBFolder) = True And objFSO.FolderExists(strRemoteMDBFolder) = True Then
	For Each objFile In objFSO.GetFolder(strLocalMDBFolder).Files
		If Right(LCase(objFile.Name), 4) = ".mdb" Then
			strLocalVersion = Mid(objFile.Name, InStrRev(objFile.Name, "v") + 1)
			strLocalVersion = Left(strLocalVersion, Len(strLocalVersion) - 4)
			strLocalFile = objFile.Path
		End If
	Next
	If strLocalVersion <> "" Then
		For Each objFile In objFSO.GetFolder(strRemoteMDBFolder).Files
			If Right(LCase(objFile.Name), 4) = ".mdb" Then
				strRemoteVersion = Mid(objFile.Name, InStrRev(objFile.Name, "v") + 1)
				strRemoteVersion = Left(strRemoteVersion, Len(strRemoteVersion) - 4)
				strRemoteFile = objFile.Path
			End If
		Next
		If strRemoteVersion <> "" Then
			If CInt(strLocalVersion) < CInt(strRemoteVersion) Then
				boolNeedToCopy = True
			End If
		Else
			MsgBox "Unable to find database in remote folder."
		End If
	Else
		MsgBox "No database was found in the local database folder. Copying from remote folder."
		boolNeedToCopy = True
	End If
	If boolNeedToCopy = True Then
		MsgBox "Newer database version found. Copying to local folder."
		objFSO.CopyFile strRemoteFile, strLocalMDBFolder, True
		strLocalFile = strLocalMDBFolder & Mid(strRemoteFile, InStrRev(strRemoteFile, "\") + 1)
	Else
		MsgBox "No newer database was found. Using local version."
	End If
	Set objShell = CreateObject("WScript.Shell")
	objShell.Run """" & strLocalFile & """", 1, False
Else
	If objFSO.FolderExists(strLocalMDBFolder) = False Then
		MsgBox "Unable to find local database folder."
	Else
		MsgBox "Unable to find remote database folder."
	End If
End If

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cjohnson300Author Commented:
Thanks Rob, that was a great starting point!

I have changed the code through testing to the below.  I had to comment out the very last line, what does that refer to?

Many thanks again
strLocalMDBFolder = "C:\local\"
'strRemoteMDBFolder = "\\server\share\LatestDB\"
strRemoteMDBFolder = "C:\master\"
 
If Right(strLocalMDBFolder, 1) <> "\" Then strLocalMDBFolder = strLocalMDBFolder & "\"
Set objFSO = CreateObject("Scripting.FileSystemObject")
boolNeedToCopy = False
strLocalVersion = ""
strRemoteVersion = ""
If objFSO.FolderExists(strLocalMDBFolder) = True And objFSO.FolderExists(strRemoteMDBFolder) = True Then
	For Each objFile In objFSO.GetFolder(strLocalMDBFolder).Files
		If Right(LCase(objFile.Name), 4) = ".mdb" Then
			strLocalVersion = Mid(objFile.Name, InStrRev(objFile.Name, "v") + 1)
			strLocalVersion = Left(strLocalVersion, Len(strLocalVersion) - 4)
			strLocalFile = objFile.Path
		End If
	Next
	If strLocalVersion <> "" Then
		For Each objFile In objFSO.GetFolder(strRemoteMDBFolder).Files
			If Right(LCase(objFile.Name), 4) = ".mdb" Then
				strRemoteVersion = Mid(objFile.Name, InStrRev(objFile.Name, "v") + 1)
				strRemoteVersion = Left(strRemoteVersion, Len(strRemoteVersion) - 4)
				strRemoteFile = objFile.Path
			End If
		Next
		If strRemoteVersion <> "" Then
			If CInt(strLocalVersion) < CInt(strRemoteVersion) Then
				boolNeedToCopy = True
			End If
		Else
			MsgBox "Unable to find database in remote folder."
		End If
	Else
		MsgBox "No database was found in the local database folder. Copying from remote folder."
		For Each objFile In objFSO.GetFolder(strRemoteMDBFolder).Files
			If Right(LCase(objFile.Name), 4) = ".mdb" Then
				strRemoteVersion = Mid(objFile.Name, InStrRev(objFile.Name, "v") + 1)
				strRemoteVersion = Left(strRemoteVersion, Len(strRemoteVersion) - 4)
				strRemoteFile = objFile.Path
			End If
		Next
		boolNeedToCopy = True
	End If
	If boolNeedToCopy = True Then
		MsgBox "Newer database version found. Copying" & strRemoteFile & " to " & strLocalMDBFolder
		objFSO.CopyFile strRemoteFile, strLocalMDBFolder, True
		strLocalFile = strLocalMDBFolder & Mid(strRemoteFile, InStrRev(strRemoteFile, "\") + 1)
	Else
		'MsgBox "No newer database was found. Using local version."
	End If
	Set objShell = CreateObject("WScript.Shell")
	objShell.Run """" & strLocalFile & """", 1, False
Else
	If objFSO.FolderExists(strLocalMDBFolder) = False Then
		MsgBox "Unable to find local database folder."
	Else
		MsgBox "Unable to find remote database folder."
	End If
End If
 
'Open in New WindowSelect AllAccept as Solution 

Open in new window

0
RobSampsonCommented:
The last line in the code snippet is
End If

which should just end the If statement.  Did you highlight the Select All or Open In New Window text that is in the buttons under the code snippet, and paste that into your code?

Glad to help.

Regards,

Rob.
0
cjohnson300Author Commented:
Ah yes, my mistake.
0
RobSampsonCommented:
No problem. Thanks for the grade.

Regards,

Rob.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.