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

mark99k

New member
Member
Local time
3:11 AM
Messages
23
Location
San Francisco, California
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 My Computer

At a glance

Windows 7 Professional 32 bit
OS
Windows 7 Professional 32 bit
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 My Computer

At a glance

Windows 8.1 Pro RTM x64Intel Core-i5-3570K 4-core @ 3.4GHz (Ivy Brid...4 x 4GB DDR3-1600 Corsair Vengeance CMZ8GX3M2...MSI GeForce GTX770 Gaming OC 2GB
Computer type
PC/Desktop
Computer Manufacturer/Model Number
Dwarf Dwf/11/2012 r09/2013
OS
Windows 8.1 Pro RTM x64
CPU
Intel Core-i5-3570K 4-core @ 3.4GHz (Ivy Bridge) (OC 4.4GHz)
Motherboard
ASRock Z77 Extreme4-M
Memory
4 x 4GB DDR3-1600 Corsair Vengeance CMZ8GX3M2A1600C9B (16GB)
Graphics Card(s)
MSI GeForce GTX770 Gaming OC 2GB
Sound Card
Realtek High Definition on board solution (ALC 898)
Monitor(s) Displays
ViewSonic VA1912w Widescreen (VGA)
Screen Resolution
1440x900
Hard Drives
OCZ Agility 3 SSD 120GB SATA III x2 (RAID 0)
Samsung HD501LJ 500GB SATA II x2
Hitachi HDS721010CLA332 1TB SATA II
Iomega 1.5TB Ext USB 2.0
WD 2.0TB Ext USB 3.0
PSU
XFX Pro Series 850W Semi-Modular
Case
Gigabyte IF233
Cooling
1 x 120mm Front Inlet 1 x 120mm Rear Exhaust
Keyboard
Microsoft Comfort Curve Keyboard 3000 (USB)
Mouse
Microsoft Comfort Mouse 3000 for Business (USB)
Internet Speed
NetGear DG834Gv3 ADSL Modem/Router (Ethernet) ~4.0 Mb/s (O2)
Antivirus
Avast! 8.0.1497
Browser
IE 11
Other Info
Optical Drive: HL-DT-ST BD-RE BH10LS30 SATA Bluray
Lexmark S305 Printer/Scanner/Copier (USB)
WEI Score: 8.1/8.1/8.5/8.5/8.25
Asus Eee PC 1011PX Netbook (Windows 7 x86 Starter)
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 My Computer

At a glance

Windows 7 Professional 32 bit
OS
Windows 7 Professional 32 bit
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 My Computer

At a glance

Windows 7 Ultimate x64 SP1 clean installAMD Athlon 64 X2 6400+4 GB2 x NVidia Geforce 8600 GTS
Computer Manufacturer/Model Number
Custom Build
OS
Windows 7 Ultimate x64 SP1 clean install
CPU
AMD Athlon 64 X2 6400+
Motherboard
Asus M2N-E SLI
Memory
4 GB
Graphics Card(s)
2 x NVidia Geforce 8600 GTS
Sound Card
Trust 5.1 Surround USB
Monitor(s) Displays
Benq FP931 19"
Screen Resolution
1280x1024@32bit@75MHz
Hard Drives
1 x Western Digital 500GB SATA (OS installation), 2 x Seagate 320GB SATA, 1 x Seagate 250GB IDE (in external USB box), 1 x TrekStor 750GB USB
PSU
650W
Keyboard
Logitech Cordless Desktop EX 100
Mouse
Logitech Cordless Optical
Internet Speed
8192 kbps / 640 kbps
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 My Computer

At a glance

Windows 7 Professional 32 bit
OS
Windows 7 Professional 32 bit
You're welcome!

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

CyberZeus
 

My Computer My Computer

At a glance

Windows 7 Ultimate x64 SP1 clean installAMD Athlon 64 X2 6400+4 GB2 x NVidia Geforce 8600 GTS
Computer Manufacturer/Model Number
Custom Build
OS
Windows 7 Ultimate x64 SP1 clean install
CPU
AMD Athlon 64 X2 6400+
Motherboard
Asus M2N-E SLI
Memory
4 GB
Graphics Card(s)
2 x NVidia Geforce 8600 GTS
Sound Card
Trust 5.1 Surround USB
Monitor(s) Displays
Benq FP931 19"
Screen Resolution
1280x1024@32bit@75MHz
Hard Drives
1 x Western Digital 500GB SATA (OS installation), 2 x Seagate 320GB SATA, 1 x Seagate 250GB IDE (in external USB box), 1 x TrekStor 750GB USB
PSU
650W
Keyboard
Logitech Cordless Desktop EX 100
Mouse
Logitech Cordless Optical
Internet Speed
8192 kbps / 640 kbps
Back
Top