Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB backup script

Status
Not open for further replies.

neutec

Technical User
Apr 26, 2003
343
Hello Guys,
Im attemping to use a VB script to backup some file. I got that working but now I wanted to stop and start a service "mnwatchdog". I cant get it to work once I add the code. Any advice?

'Stop Service
strServiceName = "mnwatchdog"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery("Select * from Win32_Service Where Name ='" & strServiceName & "'")
For Each objService in colListOfServices
objService.StopService()
Next


Option Explicit
Const start = "c:\test\"
Const dest = "c:\test2\"
Const logName = "c:\test2\log.txt"
Dim fso,Logg
Set fso = CreateObject("Scripting.FileSystemObject")
Set logg = fso_OpenTextFile(logName, 8, True)
logg.WriteLine vbNewLine & "Backup started @ " & Now()
DoWork start, dest, Now()
logg.WriteLine "Backup ended @ " & Now()

Sub DoWork(dir, dest, modDate)
Dim objFile, objDir
logg.WriteLine "DIR: " & dir
If Not fso.FolderExists(dir) Then
logg.WriteLine "Can't find the " & dir & " folder"
Exit Sub
End If

With fso.GetFolder(dir)
For Each objFile in.Files
if DateDiff("d", objFile.DateLastModified, modDate) = 0 Then
fso.CopyFile objfile,dest, True
'objFile.CopyFile dest, True
logg.WriteLine objFile.Name & ", Mod-Date: " & objFile.DateLastModified & ", copied to " & dest
End If
Next
For Each objDir in.SubFolders
DoWork objDir.Path, dest, modDate
Next
End With
End Sub

'Start Service
strServiceName = "mnwatchdog"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name ='" & strServiceName & "'")
For Each objService in colListOfServices
objService.StartService()
Next

 
[0] First, you have to move "option explicit" directive to the top of the script, with only possibly comments before it.

[1] Consequence of "option explicit", you have to dim new variables added to it.

[2] You do need to query twice, once will do. And since service name is quite unique, you do not even need to loop twice.

[3] This is the layout.
[tt]
[blue]Option Explicit[/blue]
[blue]dim strServiceName, objWMIService, colListOfService, objService, oservice, nret[/blue]
'Stop Service
strServiceName = "mnwatchdog"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery("Select * from Win32_Service Where Name ='" & strServiceName & "'")

[blue]set oservice=nothing[/blue]
For Each objService in colListOfServices
[blue]nret=[/blue]objService.StopService()
[blue]set oservice=objService
exit for[/blue]
Next
[blue]'you can do these if you like[/blue]
set colListServices=nothing
set objWMIService=nothing

[blue]if nret<>0 then 'better not proceed and look into the service
set oservice=nothing
wscript.echo "Service not stop properly, operation aborted..."
wscript.quit 999
end if[/blue]

[red]'[/red]Option Explicit
'etc etc (existing operation on backing up)

'Start Service
[blue]'these resplace all the existing block
if not (oservice is nothing) then
oservice.StartService()
end if
set oservice=nothing
[/blue][/tt]
 
Thanks for your help. I added your suggestions to the script but I am receiving an error at line 6. Did I do something wrong?

Code:
Option Explicit
dim strServiceName, objWMIService, colListOfService, objService, oservice, nret
'Stop Service
strServiceName = "mnwatchdog"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery("Select * from Win32_Service Where Name ='" & strServiceName & "'")


set oservice=nothing
For Each objService in colListOfServices
    nret=objService.StopService()
    set oservice=objService
    exit for
Next
set colListServices=nothing
set objWMIService=nothing


if nret<>0 then    'better not proceed and look into the service
    set oservice=nothing
    wscript.echo "Service not stop properly, operation aborted..."
    wscript.quit 999
end if


Const start = "c:\test\"
Const dest = "c:\test2\"
Const logName = "c:\test2\log.txt"
Dim fso,Logg
Set fso = CreateObject("Scripting.FileSystemObject")
Set logg = fso.OpenTextFile(logName, 8, True)
logg.WriteLine vbNewLine & "Backup started @ " & Now()
DoWork start, dest, Now()
logg.WriteLine "Backup ended @ " & Now()

Sub DoWork(dir, dest, modDate) 
Dim objFile, objDir 
logg.WriteLine "DIR: " & dir 
If Not fso.FolderExists(dir) Then 
logg.WriteLine "Can't find the " & dir & " folder" 
Exit Sub 
End If 

With fso.GetFolder(dir)
For Each objFile in.Files 
if DateDiff("d", objFile.DateLastModified, modDate) = 0 Then 
fso.CopyFile objfile,dest, True 
'objFile.CopyFile dest, True 
logg.WriteLine objFile.Name & ", Mod-Date: " & objFile.DateLastModified & ", copied to " & dest 
End If
Next
For Each objDir in.SubFolders
DoWork objDir.Path, dest, modDate
Next
End With
End Sub


'Start Service
'these resplace all the existing block
if not (oservice is nothing) then
    oservice.StartService()
end if
set oservice=nothing
 
That should be my typo!
[tt] dim strServiceName, objWMIService, colListOfService[red]s[/red], objService, oservice, nret
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top