Solved

Check for updated version of a file by filename

Posted on 2009-07-01
5
215 Views
Last Modified: 2012-05-07
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
0
Comment
Question by:cjohnson300
[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
  • 3
  • 2
5 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 24761719
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
 

Author Comment

by:cjohnson300
ID: 24762064
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 24762339
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
 

Author Comment

by:cjohnson300
ID: 24762383
Ah yes, my mistake.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 24762771
No problem. Thanks for the grade.

Regards,

Rob.
0

Featured Post

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!

Question has a verified solution.

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

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

690 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