Login with Facebook

Thursday, December 30, 2010

Copy Files using VBS



Dim nodeName
Dim argsObj
Set argsObj=WScript.Arguments
If argsObj.Count=1 Then
    nodeName = argsObj(0)
    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

This script allow copy selected files on a remote machine

usage : cscript copyfiles.vbs computername


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 “


Anonymous said...

Why don't you just get psexec and do the required tasks "directly" on the remote node - this scripted way for every single operation you need a special piece of code ...

Mahmoud Ibrahim said...

The one who requested the script, was asking for a native windows technology without downloading other tools.

Unknown said...

great idea, to use VB. when I faced a problem how to find all duplicates on WindowsXP, I tried first MS SyncToy, as a free and vendor tool. but it fails too often, not reliable. thus, I wrote a script on batch... but! #fc also fails to compare two files in many conditions, so I had to write some replacement in C :)

Post a Comment