Results 1 to 3 of 3
  1. #1
    New Lounger
    Join Date
    Jan 2015
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    XCOPY in VBS not working

    I am trying to write up a script whereby if a printer goes down for maintenance or whatever, we can copy over a replacement normal file (which has scripts in to reroute printers in word etc), however, the copy of the file keeps failing and I can't think why. Its saying XCOPY is not a variable, yet when I add xcopy variables, it doesn't recognise them.

    I want to do this via script rather than using robocopy or anything. Below is the code. I will add in the printer bits later (which changes the default printer), but otherwise, it keeps failing. Any and all help appreciated.

    Code:
    Option Explicit
    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")
    If (fso.FileExists("C:\Program Files (x86)\Microsoft Office\Templates\Backup\NormalOriginal.dotm")) Then 
    	XCOPY "C:\Program Files (x86)\Microsoft Office\Templates\Normal.dotm C:\Program Files (x86)\Microsoft Office\Templates\Backup\Normal.dotm" 
    ElseIf (fso.FileExists("C:\Program Files (x86)\Microsoft Office\Templates\Backup\Normal.dotm")) Then 
    	XCOPY "C:\Program Files (x86)\Microsoft Office\Templates\NormalOriginal.dotm C:\Program Files (x86)\Microsoft Office\Templates\NormalOriginal.dotm" 
    ElseIf (fso.FileExists("C:\Program Files (x86)\Microsoft Office\Templates\EmergencyNormal.dotm")) Then 
    	rename "C:\Program Files (x86)\Microsoft Office\Templates\Backup\Normal.dotm C:\Program Files (x86)\Microsoft Office\Templates\Backup\EmergencyNormal.dotm" 
    ElseIf (fso.FileExists("C:\Program Files (x86)\Microsoft Office\Templates\NormalOriginal.dotm")) Then 
    	rename "C:\Program Files (x86)\Microsoft Office\Templates\NormalOriginal.dotm C:\Program Files (x86)\Microsoft Office\Templates\Normal.dotm" 
    Else
    WScript.Echo("Your default printer has been changed")
    End If
    
    
    If (fso.FileExists("C:\Program Files (x86)\Microsoft Office\Templates\Normal.dotm"))Then 
    	XCOPY "C:\Program Files (x86)\Microsoft Office\Templates\Normal.dotm C:\Program Files (x86)\Microsoft Office\Templates\Backup\Normal.dotm" 
    ElseIf (fso.FileExists("C:\Program Files (x86)\Microsoft Office\Templates\backup\Normal.dotm")) Then 
    	XCOPY "C:\Program Files (x86)\Microsoft Office\Templates\EmergencyNormal.dotm C:\Program Files (x86)\Microsoft Office\Templates\EmergencyNormal.dotm" 
    ElseIf (fso.FileExists("C:\Program Files (x86)\Microsoft Office\Templates\EmergencyNormal.dotm")) Then 
    	rename "C:\Program Files (x86)\Microsoft Office\Templates\Backup\Normal.dotm C:\Program Files (x86)\Microsoft Office\Templates\Backup\NormalOriginal.dotm" 
    ElseIf (fso.FileExists("C:\Program Files (x86)\Microsoft Office\Templates\backup\Normal.dotm")) Then 
    	rename "C:\Program Files (x86)\Microsoft Office\Templates\Backup\EmergencyNormal.dotm C:\Program Files (x86)\Microsoft Office\Templates\Backup\Normal.dotm" 
    Else
    WScript.Echo("Your default printer has been changed")
    End If
    
    
    'Exit Script
    WScript.Quit()

  2. #2
    Lounger
    Join Date
    Dec 2009
    Location
    Gillingham, Dorset, UK
    Posts
    30
    Thanks
    0
    Thanked 13 Times in 11 Posts
    The lines where files are renamed would also fail.

    Take a look at the FileSystem CopyFile and MoveFile functions.

    e.g. fso.CopyFile "source file", "target file"
    and fso.MoveFile "original name", "target name"

    HTH

  3. #3
    New Lounger
    Join Date
    Jan 2015
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts
    yeah that worked, and also cut down some of the code. Thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •