Thursday, June 13, 2013

copy, move, and create files &/or folders with VBScript

We can use the methods in Object FileSystemObject or Shell to copy, move, and create files &/or folders.

The information about Object FileSystemObject is in this page:
http://msdn.microsoft.com/en-us/library/hww8txat(v=vs.84).aspx

The information about Object Shell is in this page:
http://msdn.microsoft.com/en-us/library/windows/desktop/bb774094(v=vs.85).aspx


Example:

To copy files from one folder to another folder:

Using FileSystemObject:
===============
    dim sourcePath
    dim destPath
    dim filesys

    sourcePath="c:\temp\*.*"
    destPath="d:\temp2\"
    set filesys=CreateObject("Scripting.FileSystemObject")
    filesys.CopyFile sourcePath, destPath, True
    set filesys=nothing
================
This code copys all the files from c:\temp to c:\temp2. The script runs at the backgroud


Using Shell:
=============== 
    dim sourcePath
    dim destPath
    sourcePath="c:\temp\*.*"
    destPath="d:\temp2\"

    dim objShell
    dim objFolder   
    dim cFlag
    set objShell = CreateObject("shell.application")
    set objFolder = objShell.NameSpace(destPath)
    cFlage = 48   'only copy the files
    objFolder.CopyHere sourcePath,cFlag
    set objShell = nothing
    set objFolder = nothing
    WScript.Sleep 100
==========================
The function displays a progress dialog box.
Note: for some reason, the option in the method CopyHere does not work on my computer.

No comments:

Post a Comment