Compare DLL using the version

cutecupid
cutecupid used Ask the Experts™
on
I need to compare the dll's using the version number and copy the latest version number to destination folder.

Ex:

Source:
abc.dll 23/05/2012 1.0.0.14

Source1:
abc.dll 24/05/2012 1.0.0.15

Destination: Copy the source1 dll " Destination
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014

Commented:
Hi, I think this should cover the basic requirement.

Regards,

Rob.

strSource = "\\server\share\mydll.dll"
strDestination = "c:\program files\myapp\mydll.dll"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(strDestination) = True Then
	If objFSO.GetFileVersion(strSource) > objFSO.GetFileVersion(strDestination) Then objFSO.CopyFile strSource, strDestination, True
End If

Open in new window

Author

Commented:
Not only to a particular file. I need to check all the files in the listed folder. THat too VBScript I need....thank you
Most Valuable Expert 2012
Top Expert 2014

Commented:
So you have an identical set of DLLs on a server share somewhere for comparison?  If so, then this should work, with folders specified.

Regards,

Rob.

strSource = "\\server\share\myapp\"
strDestination = "c:\program files\myapp\"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Right(strDestination, 1) <> "\" Then strDestination = strDestination & "\"
If objFSO.FolderExists(strDestination) = True Then
	For Each objFile In objFSO.GetFolder(strSource).Files
		strDestFile = strDestination & objFile.Name
		If objFSO.GetFileVersion(objFile.Path) > objFSO.GetFileVersion(strDestFile) Then objFSO.CopyFile strSource, strDestination, True
	Next
End If

Open in new window

OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Author

Commented:
Hi Rob, Thanks for your time and effort...

Fine....

The vbscript should check the dll in the server and desktop or within the desktop from the mnetioned path. Both dll's should get the file revision and compare, if the destination having old dd then it should copy the dll. Then if the dll's is not available in the destination then it should copy from the destination folder. Once the task is completed it should generate log which all files has been copied.
Most Valuable Expert 2012
Top Expert 2014

Commented:
OK, try this.  It will create a log file in the path you specify.

Regards,

Rob.

Set objNetwork = CreateObject("WScript.Network")
strSource = "\\server\share\myapp\"
strDestination = "c:\program files\myapp\"
strLog = "\\server\share\DLLCopyLog_" & objNetwork.ComputerName & ".txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLog = objFSO.CreateTextFile(strLog, True)
If Right(strDestination, 1) <> "\" Then strDestination = strDestination & "\"
If objFSO.FolderExists(strDestination) = True Then
	If objFSO.FolderExists(strSource) = True Then
		For Each objFile In objFSO.GetFolder(strSource).Files
			strDestFile = strDestination & objFile.Name
			If objFSO.FileExists(strDestFile) = False Then
				objFSO.CopyFile strSource, strDestination, True
				objLog.WriteLine objFile.Path & " was copied to " & strDestFile
			ElseIf objFSO.GetFileVersion(objFile.Path) > objFSO.GetFileVersion(strDestFile) Then
				objFSO.CopyFile strSource, strDestination, True
				objLog.WriteLine objFile.Path & " was copied to " & strDestFile & " because it was newer"
			Else
				objLog.WriteLine objFile.Path & " was not copied to " & strDestFile & " because it was not newer"
			End If
		Next
	Else
		objLog.WriteLine strSource & " was not found."
	End If
Else
	objLog.WriteLine strDestination & " was not found."
End If
objLog.Close

Open in new window

Author

Commented:
---------------------------
Windows Script Host
---------------------------
Script:      D:\hello\dlltest.vbs
Line:      14
Char:      5
Error:      File not found
Code:      800A0035
Source:       Microsoft VBScript runtime error

---------------------------
OK  
---------------------------

Modified Code

Set objNetwork = CreateObject("WScript.Network")
strSource = "D:\hello\Source\"
strDestination = "D:\hello\Destination\"
strLog = "D:\hello\Log_" & objNetwork.ComputerName & ".txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLog = objFSO.CreateTextFile(strLog, True)
If Right(strDestination, 1) <> "\" Then strDestination = strDestination & "\"
If objFSO.FolderExists(strDestination) = True Then
      If objFSO.FolderExists(strSource) = True Then
            For Each objFile In objFSO.GetFolder(strSource).Files
                  strDestFile = strDestination & objFile.Name
                  If objFSO.FileExists(strDestFile) = False Then
                        objFSO.CopyFile strSource, strDestination, True
                        objLog.WriteLine objFile.Path & " was copied to " & strDestFile
                  ElseIf objFSO.GetFileVersion(objFile.Path) > objFSO.GetFileVersion(strDestFile) Then
                        objFSO.CopyFile strSource, strDestination, True
                        objLog.WriteLine objFile.Path & " was copied to " & strDestFile & " because it was newer"
                  Else
                        objLog.WriteLine objFile.Path & " was not copied to " & strDestFile & " because it was not newer"
                  End If
            Next
      Else
            objLog.WriteLine strSource & " was not found."
      End If
Else
      objLog.WriteLine strDestination & " was not found."
End If
objLog.Close
Most Valuable Expert 2012
Top Expert 2014
Commented:
Oops, I had CopyFile strSource instead of CopyFile objFile.Path

Try this.

Regards,

Rob.

Set objNetwork = CreateObject("WScript.Network")
strSource = "D:\hello\Source\"
strDestination = "D:\hello\Destination\"
strLog = "D:\hello\Log_" & objNetwork.ComputerName & ".txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLog = objFSO.CreateTextFile(strLog, True)
If Right(strDestination, 1) <> "\" Then strDestination = strDestination & "\"
If objFSO.FolderExists(strDestination) = True Then
      If objFSO.FolderExists(strSource) = True Then
            For Each objFile In objFSO.GetFolder(strSource).Files
                  strDestFile = strDestination & objFile.Name
                  If objFSO.FileExists(strDestFile) = False Then
                        objFSO.CopyFile objFile.Path, strDestination, True
                        objLog.WriteLine objFile.Path & " was copied to " & strDestFile
                  ElseIf objFSO.GetFileVersion(objFile.Path) > objFSO.GetFileVersion(strDestFile) Then
                        objFSO.CopyFile objFile.Path, strDestination, True
                        objLog.WriteLine objFile.Path & " was copied to " & strDestFile & " because it was newer"
                  Else
                        objLog.WriteLine objFile.Path & " was not copied to " & strDestFile & " because it was not newer"
                  End If
            Next
      Else
            objLog.WriteLine strSource & " was not found."
      End If
Else
      objLog.WriteLine strDestination & " was not found."
End If
objLog.Close 

Open in new window

Author

Commented:
Yes, its working fine...Final query on the question
Will it work when comparing 2 different servers folders?

Author

Commented:
Thanks Rob...Very much appreciated.
Most Valuable Expert 2012
Top Expert 2014

Commented:
No problem. Thanks for the grade.

It will work from any source folder to any destination folder.  For example, if you specify
strSource = "\\servera\masterfiles\"
strDestination = "\\server\backupfiles\"

it will copy the files from the masterfiles folder to the backupfiles folder.

Regards,

Rob.

Author

Commented:
Hi Rob, Its not comparing the subfolders. Can you pls modify the script for comparing sub folders too.
Most Valuable Expert 2012
Top Expert 2014

Commented:
OK, I've got to run, so I rushed this, but hopefully it works.

Rob.

Set objNetwork = CreateObject("WScript.Network")
strSource = "D:\hello\Source\"
strDestination = "D:\hello\Destination\"
strLog = "D:\hello\Log_" & objNetwork.ComputerName & ".txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLog = objFSO.CreateTextFile(strLog, True)
If Right(strDestination, 1) <> "\" Then strDestination = strDestination & "\"

CopyNewerDLLs strSource, strDestination

objLog.Close

Sub CopyNewDLLs(strSourceF, strDestinationF)
	If objFSO.FolderExists(strDestinationF) = True Then
	      If objFSO.FolderExists(strSourceF) = True Then
	            For Each objFile In objFSO.GetFolder(strSourceF).Files
	                  strDestFile = strDestinationF & objFile.Name
	                  If objFSO.FileExists(strDestFile) = False Then
	                        objFSO.CopyFile objFile.Path, strDestFile, True
	                        objLog.WriteLine objFile.Path & " was copied to " & strDestFile
	                  ElseIf objFSO.GetFileVersion(objFile.Path) > objFSO.GetFileVersion(strDestFile) Then
	                        objFSO.CopyFile objFile.Path, strDestFile, True
	                        objLog.WriteLine objFile.Path & " was copied to " & strDestFile & " because it was newer"
	                  Else
	                        objLog.WriteLine objFile.Path & " was not copied to " & strDestFile & " because it was not newer"
	                  End If
	            Next
	      Else
	            objLog.WriteLine strSourceF & " was not found."
	      End If
	      For Each objSubFolder In objFSO.GetFolder(strSourceF).SubFolders
	      	CopyNewDLLs objSubFolder.Path, strDestinationF & objSubFolder.Name & "\"
	      Next
	Else
	      objLog.WriteLine strDestinationF & " was not found."
	End If
End Sub

Open in new window

Author

Commented:
---------------------------
Windows Script Host
---------------------------
Script:      D:\hello\dlltest.vbs
Line:      10
Char:      1
Error:      Type mismatch: 'CopyNewerDLLs'
Code:      800A000D
Source:       Microsoft VBScript runtime error

---------------------------
OK  
---------------------------
Most Valuable Expert 2012
Top Expert 2014

Commented:
Sorry, small typo.  On line 10, change this
CopyNewerDLLs strSource, strDestination

to this
CopyNewDLLs strSource, strDestination

Rob.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial