locked
vbScript copy a file by running windows 10 file copy GUI RRS feed

  • General discussion

  • I have a script that copies (potentially) a large number of big files from one drive to another. What I want to do is to use vbScript as the front end, but for each file that is copied I want to display the progress window that is normally displayed by Windows 10 when I drag/drop one or more files. Similarly with the drag/drop, the window would close when the copy was completed.
    • Changed type Bill_Stewart Thursday, September 5, 2019 9:29 PM
    Tuesday, May 21, 2019 7:00 PM

All replies

  • Please read the following first:

    This forum is for scripting questions rather than script requests


    -- Bill Stewart [Bill_Stewart]

    Tuesday, May 21, 2019 7:48 PM
  • In this case I am not quite sure of the distinction. I know how to script. The problem is I don't know if the information I require is available (I haven't found it) or if what I want to do is even possible. The documentation on the shell object instantiated by

        set shell = CreateObject("Shell.Application")

    is far from helpful. For example, I had considered the `ShellExecute` method using the verb `copy` but the documentation doesn't list the possible verbs so I guessed at `copy` with the assumption that I would then run `ShellExecute` again with `paste` as a verb. Again, the documentation is either incomplete or difficult to find. I'm not asking someone to write a script for me.

    Basically it boils down to "can I invoke the windows process that copies a file when I drag/drop from vbScript". If so, where can I find where this is documented?
    Tuesday, May 21, 2019 8:05 PM
  • Here are the docs for the shell copy method: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/sidebar/system-shell-folder-copyhere

    The "copy" verb copies a file(s) to the clipboard.


    \_(ツ)_/


    • Edited by jrv Tuesday, May 21, 2019 8:12 PM
    Tuesday, May 21, 2019 8:12 PM
  • Also please note that all VBScript examples have been removed from most MS documentation as VBScript is obsolete and it is considered less secure.  Use PowerShell.


    \_(ツ)_/

    Tuesday, May 21, 2019 8:14 PM
  • I care more about the ease of use of vbScript than the supposed lower level of security (a vague claim that I have not seen any evidence for). I got tired of wading through obtuse documentation so I wrote the following as a command line program in visual studio. Then I could just execute it from within the vbScript module. vbScript is simple, with a simple syntax. I find PowerShell overly complex for what I need it to do. That's the same reason I generally program in vb.Net and C# instead of C++.

    Imports System.Runtime.InteropServices
    
    Module Module1
    
        Private Enum FO_Func As Short
    
            FO_MOVE = &H1
            FO_COPY = &H2
            FO_DELETE = &H3
            FO_RENAME = &H4
    
        End Enum
    
        Private Structure SHFILEOPSTRUCT
    
            Public hwnd As IntPtr                       'A window handle to the dialog box to display information about the status of the file operation
            Public wFunc As FO_Func                     'A value that indicates which operation to perform - see FO_Func Enum
            <MarshalAs(UnmanagedType.LPWStr)>
            Public pFrom As String                      'A pointer to one or more fully qualified source file names
            <MarshalAs(UnmanagedType.LPWStr)>
            Public pTo As String                        'A pointer to the fully qualified destination file or directory name
            Public fFlags As UShort
            Public fAnyOperationsAborted As Boolean
            Public hNameMappings As IntPtr
            <MarshalAs(UnmanagedType.LPWStr)>
            Public lpszProgressTitle As String
    
        End Structure
    
        <DllImport("shell32.dll", CharSet:=CharSet.Unicode)>
        Private Function SHFileOperation(<[In]> ByRef lpFileOp As SHFILEOPSTRUCT) As Integer
        End Function
    
        Private _ShFile As SHFILEOPSTRUCT
    
        Public Sub CopyFile(ByVal srce As String, ByVal dest As String)
    
            If Not My.Computer.FileSystem.FileExists(srce) Then
                Console.WriteLine("File not found: " & srce)
                Exit Sub
            End If
    
            Try
    
                _ShFile.fFlags = &H10
                _ShFile.wFunc = FO_Func.FO_COPY
                _ShFile.pFrom = srce & vbNullChar & vbNullChar
                _ShFile.pTo = dest & vbNullChar & vbNullChar
                SHFileOperation(_ShFile)
    
            Catch ex As Exception
    
                Console.WriteLine(ex.Message)
    
            End Try
    
        End Sub
    
        Sub Main()
    
            With My.Application
                If .CommandLineArgs.Count = 2 Then
                    Dim srce = .CommandLineArgs(0)
                    Dim dest = .CommandLineArgs(1)
                    CopyFile(srce, dest)
                End If
            End With
    
        End Sub
    
    End Module


    Wednesday, May 22, 2019 2:02 AM
  • VBScript will be gone in the next version of Windows.  Almost all references to VBScript have disappeared from MS sites.

    All of that VB6 code can be done in fewer lines in PowerShell.

    Learn PS before it is too late.


    \_(ツ)_/

    Wednesday, May 22, 2019 2:15 AM
  • By "next version" do you mean the May (June?) 2019 update? If that's the case I have a (rude word) ton of scripts I'm going to have to convert. I'll have to decide between Python and Power Shell. In all the columns I have seen about what will be removed in the next major update, vbScript was not mentioned. Granted, all of these columns were from the "experts", not from Microsoft.

    Incidentally, does this mean jscript will also be gaing away?

    Sunday, May 26, 2019 3:37 PM
  • I care more about the ease of use of vbScript than the supposed lower level of security (a vague claim that I have not seen any evidence for). I got tired of wading through obtuse documentation so I wrote the following as a command line program in visual studio. Then I could just execute it from within the vbScript module. vbScript is simple, with a simple syntax. I find PowerShell overly complex for what I need it to do. That's the same reason I generally program in vb.Net and C# instead of C++.

    Imports System.Runtime.InteropServices
    
    Module Module1
    
        Private Enum FO_Func As Short
    
            FO_MOVE = &H1
            FO_COPY = &H2
            FO_DELETE = &H3
            FO_RENAME = &H4
    
        End Enum
    
        Private Structure SHFILEOPSTRUCT
    
            Public hwnd As IntPtr                       'A window handle to the dialog box to display information about the status of the file operation
            Public wFunc As FO_Func                     'A value that indicates which operation to perform - see FO_Func Enum
            <MarshalAs(UnmanagedType.LPWStr)>
            Public pFrom As String                      'A pointer to one or more fully qualified source file names
            <MarshalAs(UnmanagedType.LPWStr)>
            Public pTo As String                        'A pointer to the fully qualified destination file or directory name
            Public fFlags As UShort
            Public fAnyOperationsAborted As Boolean
            Public hNameMappings As IntPtr
            <MarshalAs(UnmanagedType.LPWStr)>
            Public lpszProgressTitle As String
    
        End Structure
    
        <DllImport("shell32.dll", CharSet:=CharSet.Unicode)>
        Private Function SHFileOperation(<[In]> ByRef lpFileOp As SHFILEOPSTRUCT) As Integer
        End Function
    
        Private _ShFile As SHFILEOPSTRUCT
    
        Public Sub CopyFile(ByVal srce As String, ByVal dest As String)
    
            If Not My.Computer.FileSystem.FileExists(srce) Then
                Console.WriteLine("File not found: " & srce)
                Exit Sub
            End If
    
            Try
    
                _ShFile.fFlags = &H10
                _ShFile.wFunc = FO_Func.FO_COPY
                _ShFile.pFrom = srce & vbNullChar & vbNullChar
                _ShFile.pTo = dest & vbNullChar & vbNullChar
                SHFileOperation(_ShFile)
    
            Catch ex As Exception
    
                Console.WriteLine(ex.Message)
    
            End Try
    
        End Sub
    
        Sub Main()
    
            With My.Application
                If .CommandLineArgs.Count = 2 Then
                    Dim srce = .CommandLineArgs(0)
                    Dim dest = .CommandLineArgs(1)
                    CopyFile(srce, dest)
                End If
            End With
    
        End Sub
    
    End Module


    This is VBA and VBA is not part of this form as it is not a scripting language/  You  will have to post in a VBA forum.


    \_(ツ)_/

    Sunday, May 26, 2019 5:58 PM
  • I posted the link to the Shell documentation..  You must use the "CopyHere" method to copy folders and groups of files. 

    \_(ツ)_/

    Sunday, May 26, 2019 5:59 PM
  • I am aware it copies to the clipboard. I had hoped that a "paste" would likewise paste the clipboard to a file.
    Sunday, May 26, 2019 7:39 PM
  • I am aware it copies to the clipboard. I had hoped that a "paste" would likewise paste the clipboard to a file.

    It does.  You can only copy one file at a time.


    \_(ツ)_/

    Sunday, May 26, 2019 7:49 PM
  • Here is how to select multiple items and copy them to a target using the ShellFolderView object:

    https://docs.microsoft.com/en-us/windows/desktop/shell/shellfolderview

    Using "CopyHere" eill allow controlling the UI display.


    \_(ツ)_/

    Sunday, May 26, 2019 7:57 PM
  • That is decidedly not VBA. It is a command line executable created in Visual Studio (vb.Net) 2019. I only posted it to show that I was able to solve my problem, but not in vbScript. I could use the same exe in Python or Power Shell.
    Sunday, May 26, 2019 8:09 PM
  • Most shell methods can only be accessed from a compiled program.   You can execute and compiled program (exe) in any scripting language including VBScript.

    Given that then what is your question?


    \_(ツ)_/

    Sunday, May 26, 2019 8:14 PM
  • To be clear - you cannot use Net code from VBScript as it is not a net language and does not have facilities for calling into the system API which is what the VB code is doing.

    Call the EXE from  vbscript using the "run" or "exec" commands.


    \_(ツ)_/

    Sunday, May 26, 2019 8:16 PM
  • >To be clear - you cannot use Net code from VBScript as it is not a net language and does not have facilities for calling into the system API which is what the VB code is doing.

    Got it. That really should have been my first question.

    >Call the EXE from  vbscript using the "run" or "exec" commands.

    That's exactly what I ended up doing once I had written my gcopy.exe that shows the copy progress window.

    Now I think I'll read my Power Shell book.



    Sunday, May 26, 2019 10:32 PM
  • Now I think I'll read my Power Shell book.



    Excellent choice.

    \_(ツ)_/

    Monday, May 27, 2019 12:35 AM