I think I may have made a mistake in how I nested my If statements, however, I can't for the life of me figure out where I went wrong. The script is supposed to compare some file dates and copy files and folders to various places depending on where the newest copy is. This all worked fine. Then I added the if statements to determine how to do the file copies based on which user/machine combo initiated it. Those conditions by themselves work as well. It's when I combine them that they seem to fail. Can I not nest statements like this? I added the WScript.echo statements to help me test. Please help!
Option Explicit
Dim WshShell
Dim fso
Dim USERPROFILE
Dim srcPath, srcPath2
Dim tgtPath, tgtPath2
On Error Resume Next
Set WshShell = WScript.CreateObject("Wscript.Shell")
Set wshNetwork = WScript.CreateObject("WScript.Network")
Set fso = WScript.CreateObject("Scripting.FilesystemObject")
srcPath = "C:\Users\Eric\Desktop\Testscripts\Local1\index.txt"
tgtPath = "C:\Users\Eric\Desktop\Testscripts\Server\index.txt"
srcPath2 = "C:\Users\Eric\Desktop\Testscripts\Local1\INCH"
tgtPath2 = "C:\Users\Eric\Desktop\Testscripts\Server\INCH"
strComputerName = wshNetwork.ComputerName
strUserName = wshNetwork.Username
If ((strUserName = "Eric") AND (strComputerName = "IT-LT")) Then
WScript.Echo "Station 1"
If Not fso.FileExists(tgtPath) Then
fso.CopyFile srcPath, tgtPath, True
ElseIf fso.FileExists(srcPath) Then
ReplaceIfNewer srcPath, tgtPath, srcPath2, tgtPath2
End If
ElseIf ((strUserName = "Dave") AND (strComputerName = "WS61")) Then
WScript.Echo "Station 2"
If Not fso.FileExists(srcPath) Then
fso.CopyFile tgtPath, srcPath, True
ElseIf fso.FileExists(tgtPath) Then
ReplaceIfNewer tgtPath, srcPath, tgtPath2, srcPath2
End If
Else
WScript.Echo "Neither"
End If
Sub ReplaceIfNewer(strSourceFile, strTargetFile, strSourceFolder, strTargetFolder)
Const OVERWRITE_EXISTING = True
Dim objFso
Dim objTargetFile
Dim dtmTargetDate
Dim objSourceFile
Dim dtmSourceDate
Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
Set objTargetFile = objFso.GetFile(strTargetFile)
dtmTargetDate = objTargetFile.DateLastModified
Set objSourceFile = objFso.GetFile(strSourceFile)
dtmSourceDate = objSourceFile.DateLastModified
If (dtmTargetDate < dtmSourceDate) Then
objFso.CopyFile objSourceFile.Path, objTargetFile.Path,OVERWRITE_EXISTING
wshshell.run ("cmd /c xcopy " & strSourceFolder & " " & strTargetFolder & " /Y"),1,true
End If
Set objFso = Nothing
End Sub