Copy directory listing to text file

  • Thread starter F. Michael Miller
  • Start date
F

F. Michael Miller

I'd like to copy the listing of a directory (& sub directories) to a text
file in vb.net.

I'm looking for teh equivilant of the DOS command dir [SourceDirectory] /s >
TargetFile.txt.


Thanks!
 
T

Tom Shelton

I'd like to copy the listing of a directory (& sub directories) to a text
file in vb.net.

I'm looking for teh equivilant of the DOS command dir [SourceDirectory] /s >
TargetFile.txt.


Thanks!

How 'bout this:

Option Explicit On
Option Strict On

Imports System.Diagnostics
Imports System.IO

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents sourceDirectory As System.Windows.Forms.TextBox
Friend WithEvents outputFile As System.Windows.Forms.TextBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents go As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.sourceDirectory = New System.Windows.Forms.TextBox
Me.outputFile = New System.Windows.Forms.TextBox
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.go = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'sourceDirectory
'
Me.sourceDirectory.Location = New System.Drawing.Point(108, 4)
Me.sourceDirectory.Name = "sourceDirectory"
Me.sourceDirectory.Size = New System.Drawing.Size(180, 20)
Me.sourceDirectory.TabIndex = 0
Me.sourceDirectory.Text = ""
'
'outputFile
'
Me.outputFile.Location = New System.Drawing.Point(108, 32)
Me.outputFile.Name = "outputFile"
Me.outputFile.Size = New System.Drawing.Size(180, 20)
Me.outputFile.TabIndex = 1
Me.outputFile.Text = ""
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(4, 4)
Me.Label1.Name = "Label1"
Me.Label1.TabIndex = 2
Me.Label1.Text = "Source Directory"
Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(4, 32)
Me.Label2.Name = "Label2"
Me.Label2.TabIndex = 3
Me.Label2.Text = "Output File Name"
Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
'
'go
'
Me.go.Location = New System.Drawing.Point(109, 64)
Me.go.Name = "go"
Me.go.TabIndex = 4
Me.go.Text = "&Go"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 93)
Me.Controls.Add(Me.go)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.outputFile)
Me.Controls.Add(Me.sourceDirectory)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub go_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles go.Click
Dim command As New Process

go.Enabled = False

With command.StartInfo
.FileName =
System.Environment.GetEnvironmentVariable("COMSPEC")
.Arguments = "/c dir /s " & Me.sourceDirectory.Text
.CreateNoWindow = True
.UseShellExecute = False
.RedirectStandardOutput = True
End With

command.Start()

Dim output As StreamWriter = File.CreateText(Me.outputFile.Text)
Dim line As String = command.StandardOutput.ReadLine()
Do Until line Is Nothing
output.WriteLine(line)
line = command.StandardOutput.ReadLine()
Loop

output.Close()
command.Dispose()
go.Enabled = True
End Sub

End Class

Obviously, there is no error trapping, etc. in this - so you would want
to add that in a real world scenario.
 
C

Cor

Hi F. Michael,

Have a look at the control OpenFileDialog with multiselect

Just an alternative

Cor
 
O

One Handed Man

Good example.


regards - OHM


Tom said:
F. said:
I'd like to copy the listing of a directory (& sub directories) to a
text file in vb.net.

I'm looking for teh equivilant of the DOS command dir
[SourceDirectory] /s > TargetFile.txt.


Thanks!

How 'bout this:

Option Explicit On
Option Strict On

Imports System.Diagnostics
Imports System.IO

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents sourceDirectory As System.Windows.Forms.TextBox
Friend WithEvents outputFile As System.Windows.Forms.TextBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents go As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.sourceDirectory = New System.Windows.Forms.TextBox
Me.outputFile = New System.Windows.Forms.TextBox
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.go = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'sourceDirectory
'
Me.sourceDirectory.Location = New System.Drawing.Point(108, 4)
Me.sourceDirectory.Name = "sourceDirectory"
Me.sourceDirectory.Size = New System.Drawing.Size(180, 20)
Me.sourceDirectory.TabIndex = 0
Me.sourceDirectory.Text = ""
'
'outputFile
'
Me.outputFile.Location = New System.Drawing.Point(108, 32)
Me.outputFile.Name = "outputFile"
Me.outputFile.Size = New System.Drawing.Size(180, 20)
Me.outputFile.TabIndex = 1
Me.outputFile.Text = ""
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(4, 4)
Me.Label1.Name = "Label1"
Me.Label1.TabIndex = 2
Me.Label1.Text = "Source Directory"
Me.Label1.TextAlign =
System.Drawing.ContentAlignment.MiddleLeft '
'Label2
'
Me.Label2.Location = New System.Drawing.Point(4, 32)
Me.Label2.Name = "Label2"
Me.Label2.TabIndex = 3
Me.Label2.Text = "Output File Name"
Me.Label2.TextAlign =
System.Drawing.ContentAlignment.MiddleLeft '
'go
'
Me.go.Location = New System.Drawing.Point(109, 64)
Me.go.Name = "go"
Me.go.TabIndex = 4
Me.go.Text = "&Go"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 93)
Me.Controls.Add(Me.go)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.outputFile)
Me.Controls.Add(Me.sourceDirectory)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub go_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles go.Click
Dim command As New Process

go.Enabled = False

With command.StartInfo
.FileName =
System.Environment.GetEnvironmentVariable("COMSPEC")
.Arguments = "/c dir /s " & Me.sourceDirectory.Text
.CreateNoWindow = True
.UseShellExecute = False
.RedirectStandardOutput = True
End With

command.Start()

Dim output As StreamWriter =
File.CreateText(Me.outputFile.Text) Dim line As String =
command.StandardOutput.ReadLine() Do Until line Is Nothing
output.WriteLine(line)
line = command.StandardOutput.ReadLine()
Loop

output.Close()
command.Dispose()
go.Enabled = True
End Sub

End Class

Obviously, there is no error trapping, etc. in this - so you would
want
to add that in a real world scenario.
 
F

F. Michael Miller

I shall gives these a shot, but none of them are exactly what I'm looking
for.

I have an app that checks for the existance of a large list of files. The
original technique was to use the DIR function to determine if the file
existed. This is too slow. It's much faster to dupm the directory to a
text file and search that instead.

My gut feeling is that the methods presented so far will be slower then
opening a DOS session and using dir [SourcePath] /s > TargetFile.txt. I was
trying to do this with the SHELL function but without success.

Tom Shelton said:
I'd like to copy the listing of a directory (& sub directories) to a text
file in vb.net.

I'm looking for teh equivilant of the DOS command dir [SourceDirectory] /s >
TargetFile.txt.


Thanks!

How 'bout this:

Option Explicit On
Option Strict On

Imports System.Diagnostics
Imports System.IO

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents sourceDirectory As System.Windows.Forms.TextBox
Friend WithEvents outputFile As System.Windows.Forms.TextBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents go As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.sourceDirectory = New System.Windows.Forms.TextBox
Me.outputFile = New System.Windows.Forms.TextBox
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.go = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'sourceDirectory
'
Me.sourceDirectory.Location = New System.Drawing.Point(108, 4)
Me.sourceDirectory.Name = "sourceDirectory"
Me.sourceDirectory.Size = New System.Drawing.Size(180, 20)
Me.sourceDirectory.TabIndex = 0
Me.sourceDirectory.Text = ""
'
'outputFile
'
Me.outputFile.Location = New System.Drawing.Point(108, 32)
Me.outputFile.Name = "outputFile"
Me.outputFile.Size = New System.Drawing.Size(180, 20)
Me.outputFile.TabIndex = 1
Me.outputFile.Text = ""
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(4, 4)
Me.Label1.Name = "Label1"
Me.Label1.TabIndex = 2
Me.Label1.Text = "Source Directory"
Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(4, 32)
Me.Label2.Name = "Label2"
Me.Label2.TabIndex = 3
Me.Label2.Text = "Output File Name"
Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
'
'go
'
Me.go.Location = New System.Drawing.Point(109, 64)
Me.go.Name = "go"
Me.go.TabIndex = 4
Me.go.Text = "&Go"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 93)
Me.Controls.Add(Me.go)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.outputFile)
Me.Controls.Add(Me.sourceDirectory)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub go_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles go.Click
Dim command As New Process

go.Enabled = False

With command.StartInfo
.FileName =
System.Environment.GetEnvironmentVariable("COMSPEC")
.Arguments = "/c dir /s " & Me.sourceDirectory.Text
.CreateNoWindow = True
.UseShellExecute = False
.RedirectStandardOutput = True
End With

command.Start()

Dim output As StreamWriter = File.CreateText(Me.outputFile.Text)
Dim line As String = command.StandardOutput.ReadLine()
Do Until line Is Nothing
output.WriteLine(line)
line = command.StandardOutput.ReadLine()
Loop

output.Close()
command.Dispose()
go.Enabled = True
End Sub

End Class

Obviously, there is no error trapping, etc. in this - so you would want
to add that in a real world scenario.

--
Tom Shelton [MVP]
OS Name: Microsoft Windows XP Professional
OS Version: 5.1.2600 Service Pack 1 Build 2600
System Up Time: 0 Days, 19 Hours, 40 Minutes, 6 Seconds
 
T

Tom Shelton

I shall gives these a shot, but none of them are exactly what I'm looking
for.

I have an app that checks for the existance of a large list of files. The
original technique was to use the DIR function to determine if the file
existed. This is too slow. It's much faster to dupm the directory to a
text file and search that instead.

Wow... Maybe something like:

const dirName as string = "c:\whatever"

dim searchFile as streamreader = file.opentext("filelist.txt")
dim searchlist() as string = searchFile.ReadToEnd().Split(Environment.NewLine)

for i as integer = 0 to searchlist.getupperbound(0)
if file.exists(path.combine(dirName, searchlist(i)) Then
' file exists
else
file.doesn't exist
end if
next i

should be fairly easy to modify this into a recursive routine that would
scan an entire directory tree if needs be...
My gut feeling is that the methods presented so far will be slower then
opening a DOS session and using dir [SourcePath] /s > TargetFile.txt. I was
trying to do this with the SHELL function but without success.

My method would have been somewhat slower - but I don't think by much,
since you are simply running the same command... Of course, you could
just put the redirect on the command line and then call it like this...


with command.StartInfo
.FileName = System.Environment.GetEnvironmentVariable("COMSPEC")
.Arguments = "/c dir /s " & Me.sourceDirectory.Text _
& " > " & outputFile.Text
.CreateNoWindow = True
.UseShellExecute = False
End With

command.Start()
command.WaitForExit()
command.Dispose()

Read in your output file... But personally, I think your going to want
to take a closer look at the System.IO namespace, especially the
Directory, File, and Path classes.
 
F

F. Michael Miller

I must be missing an Import statement. It doesn't like the
with Command.StartInfo line, or any of the other command. lines.



Tom Shelton said:
I shall gives these a shot, but none of them are exactly what I'm looking
for.

I have an app that checks for the existance of a large list of files. The
original technique was to use the DIR function to determine if the file
existed. This is too slow. It's much faster to dupm the directory to a
text file and search that instead.

Wow... Maybe something like:

const dirName as string = "c:\whatever"

dim searchFile as streamreader = file.opentext("filelist.txt")
dim searchlist() as string = searchFile.ReadToEnd().Split(Environment.NewLine)

for i as integer = 0 to searchlist.getupperbound(0)
if file.exists(path.combine(dirName, searchlist(i)) Then
' file exists
else
file.doesn't exist
end if
next i

should be fairly easy to modify this into a recursive routine that would
scan an entire directory tree if needs be...
My gut feeling is that the methods presented so far will be slower then
opening a DOS session and using dir [SourcePath] /s > TargetFile.txt. I was
trying to do this with the SHELL function but without success.

My method would have been somewhat slower - but I don't think by much,
since you are simply running the same command... Of course, you could
just put the redirect on the command line and then call it like this...


with command.StartInfo
.FileName = System.Environment.GetEnvironmentVariable("COMSPEC")
.Arguments = "/c dir /s " & Me.sourceDirectory.Text _
& " > " & outputFile.Text
.CreateNoWindow = True
.UseShellExecute = False
End With

command.Start()
command.WaitForExit()
command.Dispose()

Read in your output file... But personally, I think your going to want
to take a closer look at the System.IO namespace, especially the
Directory, File, and Path classes.

--
Tom Shelton [MVP]
OS Name: Microsoft Windows XP Professional
OS Version: 5.1.2600 Service Pack 1 Build 2600
System Up Time: 0 Days, 21 Hours, 9 Minutes, 45 Seconds
 
T

Tom Shelton

I must be missing an Import statement. It doesn't like the
with Command.StartInfo line, or any of the other command. lines.

Look back at my original example - this was just a slight modification
of that, so I didn't post the entire thing. This was just part where I
was filling in the StartInfo information.

I seriously think you may want to look at Cor's link, and read up on
what's available in the System.IO namespace.
 
F

F. Michael Miller

Found it. I needed a dim command as New Process.

It seemd to work great, thanks!

F. Michael Miller said:
I must be missing an Import statement. It doesn't like the
with Command.StartInfo line, or any of the other command. lines.



Tom Shelton said:
I shall gives these a shot, but none of them are exactly what I'm looking
for.

I have an app that checks for the existance of a large list of files. The
original technique was to use the DIR function to determine if the file
existed. This is too slow. It's much faster to dupm the directory to a
text file and search that instead.

Wow... Maybe something like:

const dirName as string = "c:\whatever"

dim searchFile as streamreader = file.opentext("filelist.txt")
dim searchlist() as string = searchFile.ReadToEnd().Split(Environment.NewLine)

for i as integer = 0 to searchlist.getupperbound(0)
if file.exists(path.combine(dirName, searchlist(i)) Then
' file exists
else
file.doesn't exist
end if
next i

should be fairly easy to modify this into a recursive routine that would
scan an entire directory tree if needs be...
My gut feeling is that the methods presented so far will be slower then
opening a DOS session and using dir [SourcePath] /s > TargetFile.txt.
I
was

My method would have been somewhat slower - but I don't think by much,
since you are simply running the same command... Of course, you could
just put the redirect on the command line and then call it like this...


with command.StartInfo
.FileName = System.Environment.GetEnvironmentVariable("COMSPEC")
.Arguments = "/c dir /s " & Me.sourceDirectory.Text _
& " > " & outputFile.Text
.CreateNoWindow = True
.UseShellExecute = False
End With

command.Start()
command.WaitForExit()
command.Dispose()

Read in your output file... But personally, I think your going to want
to take a closer look at the System.IO namespace, especially the
Directory, File, and Path classes.

--
Tom Shelton [MVP]
OS Name: Microsoft Windows XP Professional
OS Version: 5.1.2600 Service Pack 1 Build 2600
System Up Time: 0 Days, 21 Hours, 9 Minutes, 45 Seconds
 
F

F. Michael Miller

I'm going to try both methods and comparte the speeds. I'll post the
results in this thread.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top