CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2009
    Posts
    46

    Exclamation MenuItem 3 tasks

    Can I make a menu item to execute 3 tasks?
    For instance when I select a file from a ListBox and I press ToolStripMenuItem1, the program will move the selected file to a directory, execute archive.bat file and the move the archived file to another directory?
    Hmmm... I tried but I can make only one operation - to move the file. Can someone post a helping code or something?
    Thank you!

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: MenuItem 3 tasks

    Show us your code that only completed one operation, perhaps the batch file is creating weird issues

  3. #3
    Join Date
    Nov 2009
    Posts
    46

    Re: MenuItem 3 tasks

    Code:
    Private Sub Command20_Click()
    ' identifies the items that have been selected in the listbox
    'and moves them from one folder to another
    Dim intCount As Integer 'counts the number of items in the listbox
    Dim FileToMove As String
    For intCount = 0 To Me.List1.ListCount - 1
    If Me.List1.Selected(intCount) = True Then
    FileToMove = Me.List1.ItemData(intCount)
    MsgBox FileToMove
    Dim fso ' Code below moves files (one by one) from source folder to destination folder
    Dim file As String, sfol As String, dfol As String
    
    file = FileToMove ' change to match the file name
    sfol = "C:\folderone\" ' change to match the source folder path
    dfol = "C:\foldertwo\" ' change to match the destination folder path
    Set fso = CreateObject("Scripting.FileSystemObject")
    On Error Resume Next
    If Not fso.FileExists(sfol & file) And Not fso.FileExists(dfol & file) Then
    MsgBox file & " does not exist in the source directory!", vbExclamation, "File Missing in source file"
    ElseIf fso.FileExists(sfol & file) And fso.FileExists(dfol & file) Then
    MsgBox file & " already exists in the destination folder", vbExclamation, "File Duplication"
    ElseIf Not fso.FileExists(dfol & file) Then
    fso.MoveFile (sfol & file), dfol
    MsgBox file & " has now been moved!", vbExclamation, "Requested action complete"
    
    Else
    MsgBox file & " has already been moved!", vbExclamation, "File Exists in destination folder"
    End If
    If Err.Number = 53 Then MsgBox "File not found"
    End If
    Next
    
    If Me.List1.Selected(intCount) = False Then
    MsgBox "Please use the list box to highlight the files that you would like to move"
    Exit Sub
    End If
    End Sub
    
    Private Sub Form_Load()
    'on loading the form the function is called which displays
    'the contents of a folder in the listbox
    Call DisplayContentsOfDirectory
    End Sub
    
    
    Function DisplayContentsOfDirectory()
    'Displays contents of folder into a list box
    Dim sFileDest As String
    Dim MyDir   As String
    Dim Counter As Long
    Dim i As Integer
    Dim tempstring As String
    
    'Create a dynamic array variable, declare its initial size
    Dim DirectoryListArray() As String
    ReDim DirectoryListArray(1000)
    sFileDest = "C:\folderone\*.*"
    'Loop through all the files in the directory by using Dir$ function
    MyDir = Dir$(sFileDest, vbDirectory)
    Do While sFileDest <> ""
        'DirectoryListArray(Counter) = sFileDest
        DirectoryListArray(Counter) = Mid(sFileDest, 1, 30)
    sFileDest = Dir$
    Counter = Counter + 1
        Loop
    'Reset the size of the array without losing its values using Redim Preserve
    ReDim Preserve DirectoryListArray(Counter - 1)
    'To prove it worked I ran the following:
    For Counter = 2 To UBound(DirectoryListArray)
        'Debug.Print writes the results to the Immediate window (press Ctrl + G to view it)'
        If DirectoryListArray(Counter) <> DirectoryListArray(Counter - 1) Then
        'Debug.Print DirectoryListArray(Counter)
        'MsgBox DirectoryListArray(Counter)
        tempstring = tempstring & DirectoryListArray(Counter) & ";"
    End If
    Next Counter
        '[Forms]![form1]!List1.RowSource = ""  'This would clear rowsource
        [Forms]![form1]!List1.RowSource = tempstring
    
    End Function

  4. #4
    Join Date
    Nov 2009
    Posts
    46

    Re: MenuItem 3 tasks

    Is in the old VB code. That's why I'm trying to learn and need some help in .NET

  5. #5
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: MenuItem 3 tasks

    OK, let us take it step by step

    The fso ( File System Object ), is not necessary here anymore. You could simply use the System.IO namespace. Here is some details on it :

    http://msdn.microsoft.com/en-us/library/system.io.aspx

  6. #6
    Join Date
    Nov 2009
    Posts
    46

    Re: MenuItem 3 tasks

    Resolved.
    CASE CLOSED!

  7. #7
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: MenuItem 3 tasks

    Actually FSO was never needed in VB or any compiled version of basic that I am aware of. It is really for VBScript. In 15 plus years of coding in VB I have used it only for web pages and on handheld devices and then only because it was the only option.

    VB has built in functions that can do all the things you can do with the FSO faster with less memory usage. For that matter even GWBasic did.

    I cringe everytime I see FSO in a VB app.
    Last edited by DataMiser; April 27th, 2011 at 09:55 AM.
    Always use [code][/code] tags when posting code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured