VBS script used to overwrite, now makes (unwanted) copy


  1. Posts : 23
    Windows 7 Professional 32 bit
       #1

    VBS script used to overwrite, now makes (unwanted) copy


    I have a simple VBS login script for all my users that checks a location on our server and updates (by overwriting) the same files on the users' PCs if the server copy is newer (FSO.CopyFile etc). This has run swimmingly for several months now, but today I discovered it ISN'T overwriting the local files, but rather creates a copy of any replaced file, in the standard Win 7 format of Filename - Copy.ext.

    Is this some Windows component with its Mom Switch set on high, trying to be helpful/protective? The problem it creates is this: some of the files the script updates are VBA add-ins containing a suite of Word macros; and if 2 files in the macro-supplying folder contain same-named macros, *neither* macro of the conflicting pair is available to the user.

    This is a recent change -- that is, it’s not something that's been happening all along but only recently noticed.

    Even stranger -- or perhaps this is a clue -- this *doesn't* happen on my home PC, which is identically equpped except for different AV programs (work=Comodo, home=Avast).

    All PCs are Win 7 Pro 32-bit with Office 2010.

    Any ideas? I know I could change the script to delete the to-be-updated files before overwriting, but that seems like surrendering to the vampire.
      My Computer


  2. Posts : 9,582
    Windows 8.1 Pro RTM x64
       #2

    Can you post a copy of the code (in text form) so that we can look at it to see if there's anything that may be causing this issue?
      My Computer


  3. Posts : 23
    Windows 7 Professional 32 bit
    Thread Starter
       #3

    It's below. I hope it's decipherable but I fear it may not be. (I'm not so diligent about commenting code.) FYI the notation 'AAI' is just our shorthand for the name of the macro suite. The 'guts' of the code begins after the 'copy or update files' comment.

    Code:
       Dim oFSO 
       Dim oShell
       Dim oAppDataFolder
    
       Set oFSO=CreateObject("Scripting.FileSystemObject")
       Set oServerUT = oFSO.GetFolder("Z:\Users\AAI\UT\")
       Set oServerSU = oFSO.GetFolder("Z:\Users\AAI\SU\")
       Set oShell = CreateObject("Shell.Application")
       Set oAppDataFolder = oShell.Namespace(&H1a&)
    
       WSF = oAppDataFolder.Self.Path & "\Microsoft\Word\Startup\"
    
       Set oShell2 = CreateObject("WScript.Shell")
       Hpath = oShell2.ExpandEnvironmentStrings("%HomePath%")
       
       '===set local AAI file path
       sAAI = "C:" & Hpath & "\AAI\"
       sAAIUT = sAAI & "UT"
       Set oAAI = oFSO.GetFolder(sAAI)
       Set oUserUT = oFSO.GetFolder(sAAIUT)
    
       '====copy or update files
       Set colFilesUT = oServerUT.Files
       For Each oFile In colFilesUT
          If Instr(oFile.Name,"~")=0 Then
             sLocal = Right(oFile, Len(oFile.Name) - InStrRev(oFile.Name, "\"))
             If Not oFSO.FileExists(oUserUT.Path & "\" & sLocal) Then
                'not found on local PC, so copy
                oFSO.CopyFile oFile.Path, oUserUT.Path & "\"
                bUpdated = True
             Else
                Set oLocalFile = oFSO.GetFile(oUserUT.Path & "\" & oFile.Name)
                If oFile.DateLastModified > oLocalFile.DateLastModified Then
                  'found older on local PC, so replace
                   oFSO.CopyFile oFile.Path, oUserUT.Path & "\"
                   bUpdated = True
                End If 
             End If
          End If
       Next
    
       Set colFilesSU = oServerSU.Files
       For Each oFile In colFilesSU
          If Instr(oFile.Name,"~")=0 Then
             sLocal = Right(oFile, Len(oFile.Name) - InStrRev(oFile.Name, "\"))
             If Not oFSO.FileExists(WSF & sLocal) Then
                'not found on local PC, so copy
                oFSO.CopyFile oFile.Path, WSF
                bUpdated = True
             Else
                Set oLocalFile = oFSO.GetFile(WSF & oFile.Name)
                If oFile.DateLastModified > oLocalFile.DateLastModified Then 
                   'found older on local PC, so replace
                   oFSO.CopyFile oFile.Path, WSF
                   bUpdated = True
                End If 
             End If
          End If 
       Next
    
       On Error Resume Next 
       Set oShell = Nothing
       Set oShell2 = Nothing 
       Set oFSO = Nothing
       Set oAppDataFolder = Nothing
       Set oServerUT = Nothing
       Set oUserUT = Nothing
       Set SystemSet = Nothing
       Set oAAI = Nothing
       Set oHT = Nothing
       Set colFilesUT = Nothing 
       Set colFilesSU = Nothing 
       Set oLocalFile = Nothing
       If bNewInstall = True Then 
          MsgBox "Install successful."
       End If
      My Computer


  4. Posts : 313
    Windows 7 Ultimate x64 SP1 clean install
       #4

    Hi, the method CopyFile has a third parameter, overwrite. If it is true, it'll overwrite the file:
    Code:
    oFSO.CopyFile source, destination, True
    Another recommendation: as destination, don't use only path, but include also the filename (i.e., not C:\Temp but C:\Temp\MyFile).

    CyberZeus
      My Computer


  5. Posts : 23
    Windows 7 Professional 32 bit
    Thread Starter
       #5

    Thanks. That's interesting. But is there a reason this third parameter would be required on some systems and not others? (The differently-behaving machines are very similar, even similar Dell models purchased the same month!)

    I appreciate the tip.
      My Computer


  6. Posts : 313
    Windows 7 Ultimate x64 SP1 clean install
       #6

    You're welcome!

    I don't know why the behaviour is different, however if the third parameter solves, use it.

    CyberZeus
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 13:13.
Find Us