|
Use the DirectoryInfo and FileInfo classes.
--Colin Mackay--
EuroCPian Spring 2004 Get Together[^]
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
|
|
|
|
|
Could you give me an example??
it will be very helpfull
Thanks in advance
|
|
|
|
|
|
Hi,
I have already seen some these web pages, but by problem is to look at all subdirectories, I am going to make a kind of loop over each directory. I hope that it help me.
Thanks, you helped me a lot too.
|
|
|
|
|
ltt19 wrote:
but by problem is to look at all subdirectories, I am going to make a kind of loop over each directory
That sounds about right. For each directory you then have to loop over the subdirectories, for each of the subdirectories you have to loop over the subdirectories, and so on...
--Colin Mackay--
EuroCPian Spring 2004 Get Together[^]
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
|
|
|
|
|
This code is for VB. VB.Net is quite similar though.
Set a reference to Microsoft Scripting Runtime
Dim srt As New Scripting.FileSystemObject
Dim drv As Drive
Dim fld As Folder
Dim f As File
Set drv = srt.Drives("C:\")
Set fld = drv.RootFolder
For Each f In fld.Files
Debug.Print f.Name
Next
If you want to loop through each directory as well you will need to create another function to return the folder object. To check to see if a folder has sub folder use the following code: fld.SubFolders
Michael
|
|
|
|
|
There are some classes in System.IO that do the same that you are doing. The problem is to loop over all directories with this classes. I will try to do a loop with your code.
Thanks again!!!
|
|
|
|
|
Here is something I wrote a few years back in VB6 to simple create a text file of all of my MP3s. It doesn't filter them by *.MP3 since that is all that is in that particular shared drive but you get the idea. I haven't had to mess around with the system.io yet. But the below does work. Give it a try.
Dim fso As New FileSystemObject
Dim List As TextStream
Const C_PLUS As String = "+ "
Dim i As Integer ' Indent
Private Sub Main()
Dim d As Folder
Set d = fso.GetFolder("N:\")
Set List = fso.CreateTextFile(d.Path & "NewSongList.txt", True)
DoFolders d
List.Close
End Sub
Private Sub DoFolders(fldr As Folder)
Dim d As Folder
If i = 0 Then
List.WriteLine Space(i) & C_PLUS & fldr.Path
Else
List.WriteLine Space(i) & C_PLUS & fldr.Name
End If
i = i + 2
DoFiles fldr
For Each d In fldr.SubFolders
DoFolders d
Next
i = i - 2
End Sub
Private Sub DoFiles(fldr As Folder)
Dim f As File
For Each f In fldr.Files
List.WriteLine Space(i + 2) & f.Name
Next
End Sub
Michael
|
|
|
|
|
Hi,
Yes, the idea is great, I am going to use your example, but I will try to use the system.io class, if I can´t, I will use your example as it is.....
Thanks, it is what I was looking for....
|
|
|
|
|
This is some code I have used. It writes all files in a directory and sub directories to an xml document. The functions are recusrsive. Modify to suite your needs as appropriate.
'Create a new xml textwriter and write to a stringwriter object
Dim sw As New StringWriter
Dim writer As New XmlTextWriter(sw)
'Write document element
writer.WriteStartElement("Files")
'Get the files here
ListFoldersAndFiles(Server.MapPath("MyFolder"), writer)
writer.WriteEndElement()
'Write end document element
'Clean up objects
writer.Flush()
writer.Close()
writer = Nothing
Dim xmldoc As New XmlDocument
xmldoc.LoadXml(sw.ToString())
Private Sub ListFoldersAndFiles(ByVal pth As String, ByRef writer As XmlTextWriter)
AddFiles(pth, writer)
AddFolders(pth, writer)
End Sub
Public Sub AddFiles(ByVal pth As String, ByRef writer As XmlTextWriter)
Dim sFiles() As String = Directory.GetFiles(pth)
Dim iFiles As Integer
For iFiles = 0 To UBound(sFiles)
writer.WriteStartElement("Files")
writer.WriteStartElement("Path")
writer.WriteString(pth)
writer.WriteEndElement()
writer.WriteStartElement("Filename")
writer.WriteString(Path.GetFileName(sFiles(iFiles)))
writer.WriteEndElement()
writer.WriteEndElement()
Next
End Sub
Public Sub AddFolders(ByVal pth As String, ByRef writer As XmlTextWriter)
Dim sDirectories() As String = Directory.GetDirectories(pth)
Dim iDirectories As Integer
Dim sDirectoryName As String
For iDirectories = 0 To UBound(sDirectories)
AddFolders(sDirectories(iDirectories), writer)
AddFiles(sDirectories(iDirectories), writer)
Next
End Sub
Jim
|
|
|
|
|
Hi, you are using XML, but the "idea" is the same, it could help me!!!, so I will try to do it with the System.IO classes, if not your code will be very usefull!
Thanks!
|
|
|
|
|
I did it, it works, for me, the code is above, if you want to make any changes...
Thanks for everybody who helped me.
Here is the code, using a ListBox named as 'lst'
Dim folders As New System.Collections.Specialized.StringCollection<br />
<br />
Private Function GetAllDirectories(ByVal Name As String)<br />
On Error Resume Next<br />
Dim finder As System.IO.Directory<br />
Dim cdir As New System.Collections.Specialized.StringCollection<br />
Dim cdir2 As New System.Collections.Specialized.StringCollection<br />
<br />
Dim cancel As Boolean = False<br />
Dim dir As String<br />
<br />
cdir.AddRange(finder.GetDirectories(Name))<br />
folders.AddRange(finder.GetDirectories(Name))<br />
<br />
Do While cancel = False<br />
<br />
For Each dir In cdir<br />
folders.AddRange(finder.GetDirectories(dir))<br />
cdir2.AddRange(finder.GetDirectories(dir))<br />
Next<br />
If cdir2.Count = 0 Then<br />
cdir = Nothing<br />
cdir2 = Nothing<br />
Dim i As Integer<br />
For i = 0 To folders.Count<br />
lst.Items.Add(folders(i).ToString)<br />
Next<br />
cancel = True<br />
Exit Do<br />
Else<br />
cdir.Clear()<br />
Dim y As Integer<br />
For y = 0 To cdir2.Count<br />
cdir.Add(cdir2(y))<br />
Next<br />
cdir2.Clear()<br />
dir = Nothing<br />
y = Nothing<br />
End If<br />
<br />
Loop<br />
<br />
MessageBox.Show(lst.Items.Count)<br />
End Function<br />
<br />
Private Function GetAllFiles()<br />
On error resume next<br />
Dim files As System.IO.Directory<br />
Dim dir As String<br />
For Each dir In folders<br />
lst.Items.AddRange(files.GetFiles(dir))<br />
Next<br />
End Function
|
|
|
|
|
|
I need help on how to use threading to close a form
|
|
|
|
|
Anonymous wrote:
I need help on how to use threading to close a form
I bit more information on the background would be helpful.
For instance. I don't know if you already have a second thread, or you want to launch a thread to close the form or what?
--Colin Mackay--
EuroCPian Spring 2004 Get Together[^]
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
|
|
|
|
|
we run an ERP system at work that is in a client server environment. On the server side there are hundreds of exe files that could be ran from the client machine. I was wanting to write an application that would poll the client machine periodically and somehow monitor if the user has any inactivity by looking at the user proccesor time and kill it if there hasnt been any activity in a pre deteremined time period. This seems to be cumbersome with the amount of exe files i would have to monitor. I did notice that the ERP systems uses some DLL files and wonder if there is a way to monitor them instead for activity? or maybe monitor something on the netowrk that shows data being sent back and forth. Any ideas would be great.
|
|
|
|
|
Hi,
I have a problem to get a Image file in the bachground of the treeview.
Here is my code:
Public Class e_TreeView
Inherits TreeView
Dim e_Bild As Image = Image.FromFile("D:\VBPATENT2000\Icons\logo_hintergrund.bmp")
Public Overrides Property backgroundImage() As System.Drawing.Image
Get
Return e_Bild
End Get
Set(ByVal Value As System.Drawing.Image)
e_Bild = Value
End Set
End Property
End Class
Why does it not work?????
|
|
|
|
|
I'm trying to register the new key or resetting the key with its associated application name. such as all txt file extension associated with Notepad.
My code is working fine up to setting/ modifying key with its associated application for printing by simply using /p "%1"
but for microsoft office file... I want to check "use DDE" checkbox and access DDE's feathers.
I want to add some new command to the
DDE Messages: .. etc.
Can anyone tell me how can I do that programmmatically.
thank you
|
|
|
|
|
I can use Knowledge base article 243058 to open a office application like Word or Excel but when I try to open PowerPoint it open the presentation of the .ppt file and not powerpoint as the other file types do. Is there another ppt file type that will trip the opening of the product itself with a presentation loaded or at least can I open a power point in the control and then open a ppt presentation?
Is there another way to embed an Office application in a windows form other than using the a Webbrowser control?
Thanks, GregC
|
|
|
|
|
Iam developong a tab control on an MDI form. I want to add a tab page with a menue click event, and under certain circumstances, I would like the tab on the new tab page to be a different color (LlightGrey). I have written a sub which I think should work to draw the new color, but I can't seem to trigger the DrawItem Event when the new tab page is drawn, so the routine never runs. does anyone have any suggestions?
Thanks!
|
|
|
|
|
From my windows.net application I can shell to an external application and run it using sendKeys.Wait keystrokes. … This all works fine but it does it out in the open.
When I use AppWinStyle.Hide or ApWinStyle.MinimizedFocus or ApWinStyle.MinimizedNoFocus the application appears when the “ENTER” is execute and again when the “F9” is executed Otherwise it is hidden or minimized.
Any Ideas? Thanks GregC
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ProcID = Shell("C:\wincopy\wincopy.exe", AppWinStyle.Hide)
AppActivate(ProcID)
SendKeys.SendWait("{ENTER}")
SendKeys.SendWait("{F9}")
End Sub
Thanks, GregC
|
|
|
|
|
:(Hi all who know all
I have a problem in that I need to automate an excel spreadsheet with external data sources and update the data from a remote server. Problem is, this is a standard spreadsheet which I cannot (not allowed) change yet want to read certain data from it once it has updated.
Starting Excel and automating the update is not a problem (see code below). However, I need to know when the update is complete since I then have to extract specific data from this spreadsheet in my code in VB to use. How can I get feedback from Excel to say that the update is complete or alternatively pass execution to another piece of code once the update is done.
"Command1_Click" section of code works fine - its the "AfterRefresh" bit thats got me stumped since it don't work!!!!!!!! The "AfterRefresh" section apparently refers to a PivotTable according to the excel Help section but I am not using a PivotTable.
Any help would be GREATLY appreciated
Regards
Jeremy
Private Sub Command1_Click()
Set m_objExcel = Excel.Workbooks
Dim msExcelWorkbook As String
msExcelWorkbook = App.Path & "\ExcelReports\Update.xls"
m_objExcel.Open msExcelWorkbook
m_objExcel.Application.Visible = True
m_objExcel.Application.ActiveWorkbook.RefreshAll
End Sub
Private Sub ActiveWorkBook_AfterRefresh(Success As Boolean)
If Success = True Then
m_objExcel.Application.ActiveWorkbook.Save
m_objExcel.Application.ActiveWorkbook.Close
m_objExcel.Application.Quit
Call MsgBox("Refreshed all Data", vbOKOnly)
Else
' failed or was cancelled
End If
End Sub
|
|
|
|
|
Try using the WithEvents keyword.
Dim WithEvents xl As Excel.Application
Dim WithEvents Wb As Excel.Workbook
Dim WithEvents ws As Excel.Worksheet
You will have to create a reference to the Microsoft Excel Object library. I have 9.0 on my computer.
Below is a cut-n-past directly from the help file. Simply highlight the WorkBook or WorkSheet words in the Dim statements above and press F1 for help. The Microsoft Excel Visual Basic Reference Help window should appear.
CUT-N-PASTE from the Help File:
SheetChange Event
Occurs when cells in any worksheet are changed by the user or by an external link.
Syntax
Private Sub object_SheetChange(ByVal Sh As Object, ByVal Source As Range)
object Application or Workbook. For more information about using events with the Application object, see Using Events with the Application Object.
Sh A Worksheet object that represents the sheet.
Source The changed range.
Remarks
This event doesn't occur on chart sheets.
Not sure this is what your looking for but it may help.
Michael
|
|
|
|
|
We are on the right track. This looks promising
Being a newbie, how do I trigger this event or pass info to this sub once Excel has updated.
Many thanks
Jeremy
|
|
|
|
|
The Excel object should fire the events. Make sure the withevents declaration is at the class or module level. Now click on the left dropdown above the code and select the Excel object variable then right dropdown to select the appropriate events.
I have not personally used this but the events should fire. I just don't an event you are needing. You will have to play around with them so see which one will suite your needs.
Michael
|
|
|
|