Thursday, December 30, 2010

Copy Files using VBS

 

-----copyfiles.vbs----

Dim nodeName
Dim argsObj
Set argsObj=WScript.Arguments
If argsObj.Count=1 Then
    nodeName = argsObj(0)
Else
    WScript.echo "usage: cscript.exe copyfiles.vbs <nodeName>"
    WScript.Quit (1)
End If
Set objWMIService = GetObject _
    ("winmgmts:\\" & nodeName & "\root\cimv2:Win32_Process")
errReturn = objWMIService.Create _
    ("cmd.exe /c md c:\Temp", Null, Null, intProcessID)
Wscript.Echo errReturns
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & nodeName & "\root\cimv2")
Set colFiles = _
    objWMIService.ExecQuery("Select * From CIM_DataFile Where Drive = 'C:' and Path = '\\Scripts\\' and FileName Like 'coda%'") 
	'Wscript.Echo colFiles.Count echo number of files
For Each objFile in colFiles
    strNewFile = "C:\Temp\" & objFile.FileName & "." & objFile.Extension 
   	errResults = objFile.Copy(strNewFile)
	Wscript.Echo errResults 'displaying error code
Next
------ 

This script allow copy selected files on a remote machine

usage : cscript copyfiles.vbs computername

Notes:

The script is doing the following:



  1. Create Folder Named C:\temp this done by this statement " ("cmd.exe /c md c:\Temp", Null, Null, intProcessID)

  2. Select files located on drive C: on folder named \scripts\ and all files starts with coda this is done by " objWMIService.ExecQuery("Select * From CIM_DataFile Where Drive = 'C:' and Path = '\\Scripts\\' and FileName Like 'coda%'") "

  3. Writes those files into folder c:\temp “strNewFile = "C:\Temp\" & objFile.FileName & "." & objFile.Extension “