VBS to Remotely Make a copy of a File on Multiple Systems

115 pts.
Tags:
VBScript
Visual Basic Script
Windows Server 2003
Hello, I'm trying to edit a vbs script that I found online to help me remotely connect to a few servers (using a servers.txt file) and make a copy of a specific file called test.txt (it’s the same file name and path on all servers but with different configuration for each server) and add the date to the file name (test_todays data.txt) but not overwrite the previous days file. I’m also trying to copy that test_date.txt file to a staging folder on the same directory to make some changes to it. Thank you, David

Dim oFSO, sCList, sComputer, sWinDir, colOperatingSystems, oOperatingSystem Dim oWMIService, Error, sWinDirRaw, fso, drv, str Const ForReading = 1 Const OverwriteExisting = True 'Get computers -------------------------------------------- Set oFSO = CreateObject("Scripting.FileSystemObject") Set sCList = oFSO.OpenTextFile("c:scriptsservers.txt") Do Until sCList.AtEndOfStream sComputer = sCList.ReadLine 'Get WinDir name ------------------------------------------------------------ Set oWMIService = GetObject("winmgmts:\" & sComputer) Set colOperatingSystems = oWMIService.InstancesOf("Win32_OperatingSystem") For Each oOperatingSystem In colOperatingSystems sWinDirRaw = oOperatingSystem.WindowsDirectory sWinDir = StripDriveInfo(sWinDirRaw) Next 'If test.txt exists, make a backup copy named test_todaysdate.txt ----------------------------------------- If oFSO.FileExists("\" & sComputer & "d$" & sWinDir & "ABCMy SystemTestConftest.txt") Then oFSO.CopyFile "\" & sComputer & _ "d$" & sWinDir & "ABCMy SystemTestConftest.txt", "\" & sComputer & _ "d$" & sWinDir & "ABCMy SystemTestConftest_20110907.txt" End If 'Copy tests file --------------------------------------------------------------- oFSO.CopyFile "d$ABCMy SystemTestConftest_20110907.txt", "\" & sComputer &_ "d$" & sWinDir & "ABCMy SystemTestConfstagingtest_20110907.txt", OverwriteExisting If Error <> 0 Then WScript.Echo "Error copying file to " & sComputer Else WScript.Echo "Finished script On " & sComputer WScript.Echo "----------------------------------" End If Loop sCList.Close WScript.Echo "Finished Updates on all computers" MsgBox ("Exit?") Function StripDriveInfo(path) 'Set fso = CreateObject("Scripting.FileSystemObject") drv = oFSO.GetDriveName(path) If Len(drv) >0 Then str = Mid(path, Len(drv) + 2) Else str = path End If StripDriveInfo = str End Function



Software/Hardware used:
Windows Server 2003

Answer Wiki

Thanks. We'll let you know when a new response is added.

Hi Carlosdl,

So Adding the script, FSO.CopyFile “\\machine\d$\file.txt” “\\machine\d$\staging\file.txt” to the code below will allow me to move a copy of the file to the staging folder?

added copyfile statement to the script below;

Dim oFSO, sCList, sComputer
Dim file
Const ForReading = 1
Const OverwriteExisting = True
‘Get computers ——————————————–
Set oFSO = CreateObject(”Scripting.FileSystemObject”)
Set sCList = oFSO.OpenTextFile(”c:\scripts\servers.txt”)
Do Until sCList.AtEndOfStream
sComputer = sCList.ReadLine
msgbox(sComputer)

file = “\\” & sComputer & “\d$\ABCMy SystemTestConftest.txt”
If oFSO.FileExists(file) Then
msgbox(”file exists”)
oFSO.CopyFile file,replace(file,”.txt”,”_20110907.txt”)
else
FSO.CopyFile “\\machine\d$\file.txt” “\\machine\d$\staging\file.txt”
else
msgbox(”file doesn’t exist”)
end if

Loop
sCList.Close
WScript.Echo “Finished Updates on all computers”

Discuss This Question: 13  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • carlosdl
    Blackbird21, you tell us what you are trying to do, but you don't mention the problems you are facing, nor the results you are getting. The code you posted: does it (partially) work ? are you getting errors ? Please, provide more details. Thanks,
    68,805 pointsBadges:
    report
  • Blackbird21
    Thank you Carlosdl, sorry about that and one of the many errors that I was receiving was line 24/6 disk not ready I was also getting some errors about the placement of the next or if commands. I search the web in frustration to understand what this error exactly meant. The systems all have d drives and can be reach via D$ and are not fully. I have full admin permisisons on the drives as well. I'm sure there might be a better and cleaner script to use but I haven't found one yet. Thank you.
    115 pointsBadges:
    report
  • TomLiotta
    If you can, please post the code inside of a {code} block. Regardless, at this point:
    Function StripDriveInfo(path) 'Set fso = 
    Do you really want a quote character before Set? Tom
    125,585 pointsBadges:
    report
  • Blackbird21
    Dim oFSO, sCList, sComputer, sWinDir, colOperatingSystems, oOperatingSystem Dim oWMIService, Error, sWinDirRaw, fso, drv, str Const ForReading = 1 Const OverwriteExisting = True 'Get computers -------------------------------------------- Set oFSO = CreateObject("Scripting.FileSystemObject") Set sCList = oFSO.OpenTextFile("c:scriptsservers.txt") Do Until sCList.AtEndOfStream sComputer = sCList.ReadLine 'Get WinDir name ------------------------------------------------------------ Set oWMIService = GetObject("winmgmts:" & sComputer) Set colOperatingSystems = oWMIService.InstancesOf("Win32_OperatingSystem") For Each oOperatingSystem In colOperatingSystems sWinDirRaw = oOperatingSystem.WindowsDirectory sWinDir = StripDriveInfo(sWinDirRaw) Next 'If test.txt exists, make a backup copy named test_todaysdate.txt ----------------------------------------- If oFSO.FileExists("" & sComputer & "d$" & sWinDir & "ABCMy SystemTestConftest.txt") Then oFSO.CopyFile "" & sComputer & _ "d$" & sWinDir & "ABCMy SystemTestConftest.txt", "" & sComputer & _ "d$" & sWinDir & "ABCMy SystemTestConftest_20110907.txt" End If 'Copy tests file --------------------------------------------------------------- oFSO.CopyFile "d$ABCMy SystemTestConftest_20110907.txt", "" & sComputer &_ "d$" & sWinDir & "ABCMy SystemTestConfstagingtest_20110907.txt", OverwriteExisting If Error <> 0 Then WScript.Echo "Error copying file to " & sComputer Else WScript.Echo "Finished script On " & sComputer WScript.Echo "----------------------------------" End If Loop sCList.Close WScript.Echo "Finished Updates on all computers" MsgBox ("Exit?") Function StripDriveInfo(path) 'Set fso = CreateObject("Scripting.FileSystemObject") drv = oFSO.GetDriveName(path) If Len(drv) >0 Then str = Mid(path, Len(drv) + 2) Else str = path End If StripDriveInfo = str End Function
    115 pointsBadges:
    report
  • carlosdl
    Sorry for the late response. I'm starting to look into this now. I would start with a simplified version. Try this. It should display the list of servers in your input file. (Not all defined variables are used by now)
    Dim oFSO, sCList, sComputer, sWinDir, colOperatingSystems, oOperatingSystem
    Dim oWMIService, Error, sWinDirRaw, fso, drv, str
    Const ForReading = 1
    Const OverwriteExisting = True
    'Get computers ——————————————–
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set sCList = oFSO.OpenTextFile("c:scriptsservers.txt")
    Do Until sCList.AtEndOfStream
    	sComputer = sCList.ReadLine
    	msgbox(sComputer)
    
    
    Loop
    sCList.Close
    WScript.Echo "Finished Updates on all computers"
    68,805 pointsBadges:
    report
  • carlosdl
    I forgot to escape the backslashes (in the file path), and the editor ate them. Also, the editor transforms single and double quotes, so make sure to correct them if you are copy-pasting the code.
    68,805 pointsBadges:
    report
  • carlosdl
    This version checks for the file, and creates the desired copy (with a specific date as part of the name, which can be modified to read the system date). I've removed all unnecessary variables and functions.
    Dim oFSO, sCList, sComputer
    Dim file
    Const ForReading = 1
    Const OverwriteExisting = True
    'Get computers ——————————————–
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set sCList = oFSO.OpenTextFile("c:\scripts\servers.txt")
    Do Until sCList.AtEndOfStream
    	sComputer = sCList.ReadLine
    	msgbox(sComputer)
    
    	file = "\" & sComputer & "\d$\ABCMy SystemTestConftest.txt"
    	If oFSO.FileExists(file) Then
    		msgbox("file exists")
    		oFSO.CopyFile file,replace(file,".txt","_20110907.txt")
    	else
    		msgbox("file doesn't exist")
    	end if
    
    Loop
    sCList.Close
    WScript.Echo "Finished Updates on all computers"
    This should serve as a starting point. Please test it, and post back with any further questions or problems you encounter.
    68,805 pointsBadges:
    report
  • Blackbird21
    Hi Carlosdl Thank you for taking another look at the code and I'll test the code today and post back the results. BB21
    115 pointsBadges:
    report
  • Blackbird21
    Hi Carlosdl, The script works great and thank you for cleaning it up for me as well! Sorry it took so long to reply back!. I do have one other thing I would like the script to do and that is to now move a copy of the renamed file to a folder called stagging (ABCMy SystemTestConfstagingtest) that's within the same directory. I'm still learning scripting but I tried using oFSO.CopyFile "d$ABCMy SystemTestConfstagingfile.txt" and received the error path not found.
    115 pointsBadges:
    report
  • Blackbird21
    Hi Carlosdl, The script works great and thank you for cleaning it up for me as well! Sorry it took so long to reply back!. I do have one other thing I would like the script to do and that is to now move a copy of the renamed file to a folder called stagging (ABCMy SystemTestConfstagingtest) that’s within the same directory. I’m still learning scripting but I tried using oFSO.CopyFile “d$ABCMy SystemTestConfstagingfile.txt” and received the error path not found.
    115 pointsBadges:
    report
  • carlosdl
    Hi Blackbird21. Sorry for the late response. The CopyFile method takes 2 parameters, the source and target files (including path). Something like this:
    FSO.CopyFile "\machine\d$file.txt" "\machine\d$\staging\file.txt"
    68,805 pointsBadges:
    report
  • carlosdl
    Correction:
    FSO.CopyFile "\\machine\d$\file.txt" "\\machine\d$\staging\file.txt"
    68,805 pointsBadges:
    report
  • Blackbird21
    Hi Carlosdl, So Adding the script, FSO.CopyFile “\machined$file.txt” “\machined$stagingfile.txt” to the code below will allow me to move a copy of the file to the staging folder? added copyfile statement to the script below; Dim oFSO, sCList, sComputer Dim file Const ForReading = 1 Const OverwriteExisting = True ‘Get computers ——————————————– Set oFSO = CreateObject(”Scripting.FileSystemObject”) Set sCList = oFSO.OpenTextFile(”c:scriptsservers.txt”) Do Until sCList.AtEndOfStream sComputer = sCList.ReadLine msgbox(sComputer) file = “\” & sComputer & “d$ABCMy SystemTestConftest.txt” If oFSO.FileExists(file) Then msgbox(”file exists”) oFSO.CopyFile file,replace(file,”.txt”,”_20110907.txt”) else FSO.CopyFile “\machined$file.txt” “\machined$stagingfile.txt” else msgbox(”file doesn’t exist”) end if Loop sCList.Close WScript.Echo “Finished Updates on all computers”
    115 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following