Automatic Cut and Paste in Common Dialog area

Posted on 2005-04-29
Last Modified: 2008-02-01

I was wondering if a Common Dialog can be used to perform the following task:

1) 'Step one' button is clicked on main interface
2) Calls a Common Dialog box that defaults to the original image folder:
a) User selects the file(s) that they want to cut (using ctrl-A once a folder is opened, or shift select)
b) User clicks 'Open' button after making selection, thus cutting and pasting all files selected into the C:\My Documents\My Pictures folder
c) This closes the Common Dialog
3) On the main interface, user clicks the "Step 2" button, which then converts all images in C:\My Documents\My Pictures from .bmp format into a multipage tiff (I already have all of this worked out)
4) User exits the program

This could be modified to have three steps:
1). Open
2). Save
3). Process

Source code and additional information can be found at this messageboard:

I've been doing research and have found all sorts of possible solutions (none of which have been successful for me), including:

The My.Computer.FileSystem.CopyDirectory command
private static void CopyDirectory(string sourceDirectory, string destinationDirectory)

At this point I'm lost on what to do!  Any help would be greatly appreciated.


Question by:cmjwebservices
    LVL 12

    Expert Comment

    What language are you wanting to do this in.  Your examples seem to hint at some variant of C...
    LVL 2

    Author Comment

    Sorry, left out some of the most important information:

    Visual Basic 6.0 sp6
    Windows XP sp2
    LVL 13

    Accepted Solution

    Hi, this is a way to do it, add this at the beginning of your form code outside any function or sub:

    Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long

        hWnd As Long
        wFunc As Long
        pFrom As String
        pTo As String
        fFlags As Integer
        fAborted As Boolean
        hNameMaps As Long
        sProgress As String
    End Type

    Now modify your sub this way:

    Private Sub cmdMoveImages_Click()
        Dim strOpenFile As String
        On Error GoTo ErrHandler
        ' Set Cancel to True
        CommonDlg1.CancelError = True
        ' Set flags (includes multiple file select)
        CommonDlg1.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly + cdlOFNAllowMultiselect + cdlOFNExplorer Or cdlOFNNoLongNames
        ' Set filters
        ' Account for file extension
        CommonDlg1.Filter = "All Files (*.*)|*.*|Bitmaps (*.bmp)|*.bmp|Jpegs (*.jpg)|*.jpg|Gifs (*.gif)|*.gif"
        ' Change the window title
        CommonDlg1.DialogTitle = "Select Images for Processing"
        ' Set the default folder
        CommonDlg1.InitDir = "C:\Program Files\Sieratechnical\"
        ' Specify default filter
        CommonDlg1.FilterIndex = 2
        strOpenFile = CommonDlg1.FileName
        Dim FilesToMove() As String
        Dim I As Long
        Dim FileOp As SHFILEOPSTRUCT
        Const FO_MOVE = &H1
        FilesToMove = Split(strOpenFile, Chr(0)) 'Get all the selected files in an array
        FileOp.hWnd = 0
        FileOp.wFunc = FO_MOVE  'We are going to move files
       'Prepare the file names we are going to move
        For I = 1 To UBound(FilesToMove)  
            FileOp.pFrom = FileOp.pFrom + FilesToMove(0) + "\" + FilesToMove(I) + Chr(0)
        FileOp.pFrom = FileOp.pFrom + Chr(0) + Chr(0)

        'Prepare destination folder
        FileOp.pTo = "C:\My Documents\My Pictures"
        'Move the files
        SHFileOperation FileOp
        ' User pressed the Cancel button

    End Sub

    LVL 2

    Author Comment

    I'm getting the following:

    Compile error:

    Ambiguous name detected: SHFileOperation

    Any ideas?
    LVL 2

    Author Comment

    Ok, I think I found the source of the error posted above...However, when running the program, I now get:

    "Can't find project or library"

    and the debugger points to this line:

    FilesToMove = Split(strOpenFile, Chr(0)) 'Get all the selected files in an array

    particularly the "Chr" of the above...
    LVL 2

    Author Comment

    The problem could possibly be:

    MethodInfos for functions in mscorlib.dll

    is only present in .net (unless there is a component or library I need to load in addition to what I have already)...
    LVL 11

    Expert Comment

    Once you have the individual filenames (using cmjwebservices code FilesToMove = Split(strOpenFile, Chr(0)))

    use the File Object and the File.Copy method to copy the files from the present path to the My Pictures Folder

    Basically for each file in selected_file
    Dim oFile as File
    Set oFile = New File
    oFile.Path = "Selected File"
    oFile.Copy "C:\My Documemts\my Pictures\", True
    Set oFile = Nothing

    Add a reference to Microsoft Scripting Runtime to get access to the File object. Sorry my VB is crude it has been a while
    LVL 13

    Expert Comment

    Cant use Chr?
    mmm... I haven't seen this before

    You don't need any library to use Chr, it has been there since the old days of qbasic, try using Chr$(0) instead of Chr(0)
    LVL 13

    Expert Comment

    or replace every Chr(0) with vbNullChar, that must fix the proble if Chr is the cause...
    LVL 2

    Author Comment

    Thanks rettiseert!

    Actually, I created a new project and the code you posted worked fine...So, from there I thought that maybe I needed to install (which I, coincidentally had).  But, before I finished installing it, I deleted multiple modules from the code (which did no good), and changed this line to reflect the declaration

    'Move the files
        SHFileOperation FileOp


    SHFileOperationA FileOp ' (i.e., the alias)


    commented out the following declaration I had in the header of the code:

    ' Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long

    and the conflict was solved!!

    Thanks a lot for your help!  It was a good learning experience going through all of this, but I'm glad I have it finished.



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
    I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now