115 pts.
 VBS to Remotely Make a copy of a File on Multiple Systems
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
ASKED: September 8, 2011  6:18 PM
UPDATED: March 31, 2012  4:56 PM
  Help
 Approved Answer - Chosen by ITKE

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: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
		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.

ANSWERED:  Sep 14, 2011  3:16 PM (GMT)  by ITKE

 
Other Answers:

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”

Last Wiki Answer Submitted:  December 20, 2011  6:17 pm  by  Blackbird21   115 pts.
Latest Answer Wiki Contributors:  Blackbird21   115 pts.
To see other answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

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,

 63,535 pts.

 

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 pts.

 

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

 108,055 pts.

 

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 pts.

 

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"
 63,535 pts.

 

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.

 63,535 pts.

 

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 pts.

 

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 pts.

 

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 pts.

 

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"
 63,535 pts.

 

Correction:

FSO.CopyFile "\\machine\d$\file.txt" "\\machine\d$\staging\file.txt"
 63,535 pts.

 

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 pts.