Click to See Complete Forum and Search --> : MenuItem 3 tasks


MadSkillz
December 11th, 2009, 05:40 AM
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!

HanneSThEGreaT
December 11th, 2009, 06:07 AM
Show us your code that only completed one operation, perhaps the batch file is creating weird issues

MadSkillz
December 11th, 2009, 08:43 AM
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

MadSkillz
December 11th, 2009, 02:38 PM
Is in the old VB code. That's why I'm trying to learn and need some help in .NET

HanneSThEGreaT
December 12th, 2009, 01:08 AM
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

MadSkillz
April 27th, 2011, 05:15 AM
Resolved.
CASE CLOSED!

DataMiser
April 27th, 2011, 09:53 AM
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. :(