6 out of 8 rated this helpful - Rate this topic

Folder.CopyHere method

Applies to: desktop apps only

Copies an item or items to a folder.

Syntax

Folder.CopyHere(
  vItem,
  [ vOptions ]
)

Parameters

vItem

Type: Variant

The item or items to copy. This can be a string that represents a file name, a FolderItem object, or a FolderItems object.

vOptions [optional]

Type: Variant

Options for the copy operation. This value can be zero or a combination of the following values. These values are based upon flags defined for use with the fFlags member of the C++ SHFILEOPSTRUCT structure. These flags are not defined as such for Visual Basic, VBScript, or JScript, so you must define them yourself or use their numeric equivalents.

(4)

Do not display a progress dialog box.

(8)

Give the file being operated on a new name in a move, copy, or rename operation if a file with the target name already exists.

(16)

Respond with "Yes to All" for any dialog box that is displayed.

(64)

Preserve undo information, if possible.

(128)

Perform the operation on files only if a wildcard file name (*.*) is specified.

(256)

Display a progress dialog box but do not show the file names.

(512)

Do not confirm the creation of a new directory if the operation requires one to be created.

(1024)

Do not display a user interface if an error occurs.

(2048)

Version 4.71. Do not copy the security attributes of the file.

(4096)

Only operate in the local directory. Do not operate recursively into subdirectories.

(8192)

Version 5.0. Do not copy connected files as a group. Only copy the specified files.

Return value

This method does not return a value.

Remarks

Note  Not all methods are implemented for all folders. For example, the ParseName method is not implemented for the Control Panel folder (CSIDL_CONTROLS). If you attempt to call an unimplemented method, a 0x800A01BD (decimal 445) error is raised.

Examples

The following example uses CopyHere to copy the Autoexec.bat file from the root directory to the C:\Windows directory. Proper usage is shown for JScript, VBScript, and Visual Basic.

JScript:


<script language="JScript">
    function fnCopyHereJ()
    {
        var objShell = new ActiveXObject("shell.application");
        var objFolder = new Object;
        
        objFolder = objShell.NameSpace("C:\\WINDOWS");
        if (objFolder != null)
        {
            objFolder.CopyHere("C:\\AUTOEXEC.BAT");
        }
    }
 </script>


VBScript:

<script language="VBScript">
    function fnCopyHereVB()
        dim objShell
        dim objFolder
        
        set objShell = CreateObject("shell.application")
        set objFolder = objShell.NameSpace("C:\WINDOWS")
 
        if not objFolder is nothing then
            objFolder.CopyHere("C:\AUTOEXEC.BAT")
        end if
 
        set objShell = nothing
        set objFolder = nothing
    end function
</script>

Visual Basic:

Private Sub btnCopyHere_Click()
    Dim objShell  As Shell
    Dim objFolder As Folder
    
    Set objShell = New Shell
    Set objFolder = objShell.NameSpace("C:\WINDOWS")
 
    If (Not objFolder Is Nothing) Then
        objFolder.CopyHere ("C:\AUTOEXEC.BAT")
    End If
 
    Set objFolder = Nothing
    Set objShell = Nothing
End Sub

Requirements

Minimum supported client

Windows 2000 Professional, Windows XP

Minimum supported server

Windows 2000 Server

Header

Shldisp.h

IDL

Shldisp.idl

DLL

Shell32.dll (version 4.71 or later)

See also

Folder

 

 

Send comments about this topic to Microsoft

Build date: 3/7/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
CopyHere option solution?
Has anyone seen a solution to the options being ignored on XP/2003 environments?
2 bugs found (temp dirs are not cleaned up and 99th temp dir limit)
Calling CopyHere() to unzip *.zip creates temporary directories ("Temporary Directory [1-99] for XXX.zip"), in addition to the target folder that the files are extracted to. 2 issues found: 1) These temporary dirs are not cleaned up after extraction. For every call to CopyHere() for the same zip file, it will create another Temporary Directory * 2) If Temporary Directory 99 exists, CopyHere() will fail ("The file exists.") Right now I'm resorting to cleaning up these folders myself... =/ Any suggestions?
Flag Issue.
The flags are Hex values.
Here is my function that I use in vbscript.
To make it completely silent change cFlags to
cFlags = FOF_SILENT + FOF_NOCONFIRMATION + FOF_NOERRORUI

Function CopyFileProgress(Src, Dest)
    Const FOF_SILENT = &H4&
    Const FOF_RENAMEONCOLLISION = &H8&
    Const FOF_NOCONFIRMATION = &H10&
    Const FOF_ALLOWUNDO = &H40&
    Const FOF_FILESONLY = &H80&
    Const FOF_SIMPLEPROGRESS = &H100&
    Const FOF_NOCONFIRMMKDIR = &H200&
    Const FOF_NOERRORUI = &H400&
    Const FOF_NOCOPYSECURITYATTRIBS = &H800&
    Const FOF_NORECURSION = &H1000&
    Const FOF_NO_CONNECTED_ELEMENTS = &H2000&

    cFlags = FOF_NOCONFIRMATION + FOF_SIMPLEPROGRESS
    Set oFSO = Createobject("Scripting.FileSystemObject")
    Set oShell = CreateObject("Shell.Application")
    If Not oFSO.FolderExists(Dest) Then oFSO.CreateFolder(Dest)
    Set oFolder = oShell.NameSpace(Dest)
    oFolder.CopyHere Src, cFlags
End Function
Object Variable Not Set

There ist a Problem and my Solution too.

In some cases you will get: 'Object Variable Not Set' with Namespace .CopyHere

Sub doUnzip(sZipFolder as String, sDest as String)     'use ZipFile as Folder, the Destination sDest must exist
   Dim oShell
   Set  oShell = CreateObject("Shell.Application")
   'Extract the files from the zip into the folder
   oShell.NameSpace(sDest).CopyHere oShell.NameSpace(sZipFolder).Items
End Sub
sub Test()
   'This works fine:
   doUnzip "c:\ZipFile.zip", "c:\ZipFolder"

   'but this raise the 'Object Variable Not Set' Message.
   dim sZipFile as String
dim sZipFolder as String
   sZipFile = "C:\ZipFile.zip": sZipFolder = "c:\ZipFolder"
   doUnzip sZipFile, sZipFolder ' If you use a Variable with the doUnzip Sub or Function, that's the Problem!
end sub

My Solution, I do not know why, but it works:

Sub doUnzip(sZipFolder As String, sDest As String)
   Dim oShell
   Dim oZF        ' this must be Variant
   Dim oD         ' this must be Variant
   oZF = sZipFolder
   oD = sDest
   Set oShell = CreateObject("Shell.Application")
   'Extract the files from the zip into the folder
   oShell.NameSpace(oD).CopyHere oShell.NameSpace(oZF).Items
End Sub

In your Programm you have to change the sub into a function that returns true/false and add Errorhandling too,
and with

      Dim oFSO: Set oFSO = CreateObject("Scripting.FileSystemObject")
      If Not oFSO.FolderExists(oD) Then oFSO.CreateFolder oD

you can guarantee the existence of the Destination-Folder.

flag 4 (Do not display a progress dialog box)

I've used this in a VB-script (vbs) installing fonts and the dialog still shows up.


Kjell
Progress dialog box
If we specify number 4 for the second parameter, it should not display a progress dialog box. But I've tested (in Windows 7 x64) and doesn't work.
Maybe only doesn't work when copy to a zip file, but I want using it just to zip without a dialog.
Zip files
Also zip files are seen as folders. Useful for zipping / unzupping.