来自directoryof(1.txt-9999.txt)的readfile内容,范围(1233.txt -12234.txt) [英] Readfile content from directoryof (1.txt-9999.txt) with range(1233.txt -12234.txt)
问题描述
我有问题我想从目录中读取特定范围的文件
p0.txt到p9999.txt。
i只想读取p1234.txt中的文本文件-p1255.txt通过文本输入来选择范围。
并处理每个文件内容
任何帮助都将受到高度赞赏。
谢谢
我尝试过:
选项 严格 开启
选项 显式 开启
选项推断关闭
Imports System.IO
Public Class Form1
私有 Sub Button1_Click(发件人作为 对象,e As EventArgs)句柄 Button1.Click
使用 OpenFileDialog1
.FileName =
。标题= 打开文本文件
.InitialDirectory = C:\ Users \mks \Desktop\\\
ewonlindup
.Filter = Sp1 | * .sp1
.ShowDialog()
结束 < span class =code-keyword>使用
Dim 路径作为 < span class =co de-keyword> String = System.IO.Path.GetFileName(OpenFileDialog1.FileName)
TextBox1.Text = Path
结束 Sub
私有 Sub Button2_Click(发件人作为 对象,e As EventArgs)句柄 Button2.Click
使用 OpenFileDialog1
.FileName =
。标题= 打开文本文件
.InitialDirectory = C:\ Users \mks \Desktop \ newonlindup
.Filter = Sp1 | * .sp1
.ShowDialog()
结束 使用
Dim 路径 As String = System.IO.Path.GetFileName(OpenFileDialog1.FileName)
TextBox2.Text = path
End Sub
私人 Sub Button3_Click(发件人作为 对象,e As EventArgs)句柄 Button3.Click
Dim z 作为 字符串
Dim a 作为 字符串
z = TextBox1.Text
Console.WriteLine(z.Substring( 1 ,z.Length - 5 ))
a = TextBox2.Text
Console.WriteLine(a.Substring ( 1 ,a。长度 - 5 ))
如果 TextBox1.Text = 或 TextBox2.Text = 然后
MsgBox( 请填写所有文本框 )
ElseIf z.Substring( 1 ,z。长度 - 5 )> a.Substring( 1 ,a.Length - 5 )然后
Console.WriteLine( great)
MsgBox ( FROM值应大于TO)
TextBox2.Focus()
结束 如果
TextBox3.Text = TextBox1.Text
结束 Sub
私人 Sub Button4_Click(发件人) 作为 对象,e 作为 PaintEventArgs)< span class =code-keyword>句柄 Button4.Paint
' Private Sub Form1_Load(sender As Object,e As PaintEventArgs)Handles MyBase.Paint
Dim g As Graphics = e.Graphics
Dim strTextFromFile As String = IO.File.ReadAllText( C:\ Users \mks \Desktop\\\
ewonlindup \ + p1234.SP1)
Dim strResults < span class =code-keyword>作为 字符串 = 字符串 .Empty
对于 每个 c 作为 字符 在 strTextFromFile
如果 IsNumeric(c)然后
strResults + = c
结束 如果
下一步
' g.DrawString (strResults,New Font(Consolas,12),Brushes.Red,New Point(10,10))
MsgBox(strResults)
Dim 路径 As String = System.IO.Path.GetFileName(OpenFileDialog1.FileName)
结束 Sub
结束 类
< blockquote>有两种基本方法可以做到这一点,但都需要一个循环。
1)蛮力和无知方法。
设置一个循环,其整数变量 fileNo
从1233到1255(含)。
在循环内部,使用String.Format生成您的文件名:
Dim 名称 as 字符串 = 字符串 .Format( p {0} .txt,fileNo)
然后,您可以使用File.Exists检查文件是否存在,如果是,阅读它。
2)选择性方法。
首先使用Directory.GetFiles读取所有文件的列表:
Dim files As String()= Directory.GetFiles(D:\ temp \,p * .txt)
然后使用For Each循环处理每个文件。
内部e循环,检查名称是否在范围内
For Each file As String In files
Dim name As String = Path.GetFileNameWithoutExtension(file)
Dim fileNo As Integer
如果Integer.TryParse(name.Substring(1),fileNo)AndAlso fileNo> = 1233 AndAlso fileNo< = 1255那么
'在这里处理文件。
结束如果
下一步
第一个效果最好,如果它是一个短距离,没有间隙。
第二个效果最好,如果它是一个更大的范围,哪个有间隙。
您可以使用Linq创建一系列要读取的文件。检查一下:
Dim iStart 作为 整数 = 1234
Dim iEnd 作为 整数 = 1255
Dim fileRange As List( 字符串)= Enumerable.Range(iStart,iEnd - iStart + 1 < /跨度>)。 _
选择(功能(f) String .Concat( p,f, .txt))。 _
ToList()
fileRange
返回:
p1234.txt
p1235.txt
p1236.txt
p1237.txt
...
p1255。 txt
I have problem i want to read specific range of files from directory
p0.txt to p9999.txt.
i want to read only text files from p1234.txt -p1255.txt through text input to select range.
and process each file content
Any help will be highly appreciated .
Thank you
What I have tried:
Option Strict On
Option Explicit On
Option Infer Off
Imports System.IO
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
With OpenFileDialog1
.FileName = ""
.Title = "Open Text File"
.InitialDirectory = "C:\Users\mks\Desktop\newonlindup"
.Filter = "Sp1|*.sp1"
.ShowDialog()
End With
Dim path As String = System.IO.Path.GetFileName(OpenFileDialog1.FileName)
TextBox1.Text = Path
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
With OpenFileDialog1
.FileName = ""
.Title = "Open Text File"
.InitialDirectory = "C:\Users\mks\Desktop\newonlindup"
.Filter = "Sp1|*.sp1"
.ShowDialog()
End With
Dim path As String = System.IO.Path.GetFileName(OpenFileDialog1.FileName)
TextBox2.Text = path
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim z As String
Dim a As String
z = TextBox1.Text
Console.WriteLine(z.Substring(1, z.Length - 5))
a = TextBox2.Text
Console.WriteLine(a.Substring(1, a.Length - 5))
If TextBox1.Text = "" Or TextBox2.Text = "" Then
MsgBox("Please fill all text boxes")
ElseIf z.Substring(1, z.Length - 5) > a.Substring(1, a.Length - 5) Then
Console.WriteLine("great")
MsgBox("FROM value Should be greater than TO")
TextBox2.Focus()
End If
TextBox3.Text = TextBox1.Text
End Sub
Private Sub Button4_Click(sender As Object, e As PaintEventArgs) Handles Button4.Paint
' Private Sub Form1_Load(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
Dim g As Graphics = e.Graphics
Dim strTextFromFile As String = IO.File.ReadAllText("C:\Users\mks\Desktop\newonlindup\" + "p1234.SP1")
Dim strResults As String = String.Empty
For Each c As Char In strTextFromFile
If IsNumeric(c) Then
strResults += c
End If
Next
'g.DrawString(strResults, New Font("Consolas", 12), Brushes.Red, New Point(10, 10))
MsgBox(strResults)
Dim path As String = System.IO.Path.GetFileName(OpenFileDialog1.FileName)
End Sub
End Class
There are two basic ways to do this, but both require a loop.
1) The "Brute Force and Ignorance" approach.
Set up a loop with an integer variablefileNo
that runs from 1233 to 1255 inclusive.
Inside the loop, use String.Format to generate your file names:
Dim name as String = String.Format("p{0}.txt", fileNo)You can then use File.Exists to check if the file is there, and if so you read it.
2) The "Selective" approach.
Start by reading a list of all files using Directory.GetFiles:
Dim files As String() = Directory.GetFiles("D:\temp\", "p*.txt")
Then use a For Each loop to process each file.
Inside the loop, check if the name is in the range
For Each file As String In files Dim name As String = Path.GetFileNameWithoutExtension(file) Dim fileNo As Integer If Integer.TryParse(name.Substring(1), fileNo) AndAlso fileNo >= 1233 AndAlso fileNo <= 1255 Then ' Process the file here. End If NextThe first works best if it's a short range, with no gaps.
The second works best if it's a larger range, which has gaps.
You can use Linq to create a range of files to read. Check this:
Dim iStart As Integer = 1234 Dim iEnd As Integer = 1255 Dim fileRange As List(Of String) = Enumerable.Range(iStart, iEnd - iStart + 1). _ Select(Function(f) String.Concat("p", f, ".txt")). _ ToList()
fileRange
returns:
p1234.txt p1235.txt p1236.txt p1237.txt ... p1255.txt
这篇关于来自directoryof(1.txt-9999.txt)的readfile内容,范围(1233.txt -12234.txt)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!