By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,824 Members | 1,255 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,824 IT Pros & Developers. It's quick & easy.

Copy directory listing to text file

P: n/a
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!
Nov 20 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
* "F. Michael Miller" <fm***@netzero.net> scripsit:
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.


Enumerating files in a folder and its subfolders:

<http://www.mvps.org/dotnet/dotnet/samples/filesystem/downloads/RecursiveFileScan.zip>

For writing the file, you can use 'System.IO.StreamWriter'.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #2

P: n/a
In article <40***********************@newsreader.visi.com>, F. Michael Miller wrote:
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
Nov 20 '05 #3

P: n/a
Good example.
regards - OHM
Tom Shelton wrote:
In article <40***********************@newsreader.visi.com>, F.
Michael Miller wrote:
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.


--
Best Regards - OHM

O_H_M{at}BTInternet{dot}com
Nov 20 '05 #4

P: n/a
Cor
Hi F. Michael,

Have a look at the control OpenFileDialog with multiselect

Just an alternative

Cor
Nov 20 '05 #5

P: n/a
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" <to*@mtogden.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
In article <40***********************@newsreader.visi.com>, F. Michael

Miller wrote:
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

Nov 20 '05 #6

P: n/a
Cor
Hi F,

Have a look at this just the answer

http://msdn.microsoft.com/library/de...oriestopic.asp

Cor
Nov 20 '05 #7

P: n/a
In article <40***********************@newsreader.visi.com>, F. Michael Miller wrote:
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
Nov 20 '05 #8

P: n/a
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" <to*@mtogden.com> wrote in message
news:OA**************@TK2MSFTNGP09.phx.gbl...
In article <40***********************@newsreader.visi.com>, F. Michael Miller wrote:
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

Nov 20 '05 #9

P: n/a
In article <40***********************@newsreader.visi.com>, F. Michael Miller wrote:
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.

--
Tom Shelton [MVP]
OS Name: Microsoft Windows XP Professional
OS Version: 5.1.2600 Service Pack 1 Build 2600
System Up Time: 0 Days, 22 Hours, 39 Minutes, 42 Seconds
Nov 20 '05 #10

P: n/a
Found it. I needed a dim command as New Process.

It seemd to work great, thanks!

"F. Michael Miller" <fm***@netzero.net> wrote in message
news:40***********************@newsreader.visi.com ...
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" <to*@mtogden.com> wrote in message
news:OA**************@TK2MSFTNGP09.phx.gbl...
In article <40***********************@newsreader.visi.com>, F. Michael Miller wrote:
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


Nov 20 '05 #11

P: n/a
I'm going to try both methods and comparte the speeds. I'll post the
results in this thread.
"Tom Shelton" <to*@mtogden.com> wrote in message
news:uC**************@TK2MSFTNGP10.phx.gbl...
In article <40***********************@newsreader.visi.com>, F. Michael

Miller wrote:
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.

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

Nov 20 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.