Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 如何逐行修改文件_Vba_Ms Access - Fatal编程技术网

Vba 如何逐行修改文件

Vba 如何逐行修改文件,vba,ms-access,Vba,Ms Access,我有一个txt文件,语法如下: 'foo','bar' 'foo','foo bar' 'foo','foo bar bar' 'bar', 'foo' 我想找出不是以开头的每一行,并纠正它们。最后,我想说: 'foo','bar' 'foo','foo bar' 'foo','foo bar bar' 'bar', 'foo' 新行必须删除并添加到前一行的末尾,并带有前导空格。 我的代码逐行遍历一个文件,并检查第一个字符是否已经不等于” 我考虑将每一行添加到一个数组中,并在该数组中进行校正

我有一个txt文件,语法如下:

'foo','bar'
'foo','foo bar'
'foo','foo
bar bar'
'bar', 'foo'
我想找出不是以
开头的每一行
,并纠正它们。最后,我想说:

'foo','bar'
'foo','foo bar'
'foo','foo bar bar'
'bar', 'foo'
新行必须删除并添加到前一行的末尾,并带有前导空格。 我的代码逐行遍历一个文件,并检查第一个字符是否已经不等于

我考虑将每一行添加到一个数组中,并在该数组中进行校正。 我会用

以更新文件

我当前的代码:

Sub Update_File(fileToUpdate As String, fileSys As Variant)

    Set File = fileSys.OpenTextFile(fileToUpdate)
    Do Until File.AtEndOfStream
        currentLine = File.ReadLine

        If Left(currentFile, 1) <> "'" Then
            'Magic here
        End If
    Loop
    File.Close

End Sub

子更新_文件(fileToUpdate为字符串,fileSys为变量)
Set File=fileSys.OpenTextFile(fileToUpdate)
直到File.AtEndOfStream
currentLine=File.ReadLine
如果左(currentFile,1)“”,则
“这里有魔法吗
如果结束
环
文件,关闭
端接头
我正在绞尽脑汁研究什么是最佳实践,什么是精简快速的代码,因为脚本最后应该运行1000多个文件。
我不知道是否可以存储当前行和下一行,如果下一行不是以
'
开头,那么
currentLine=currentLine&&&&nextLine
并以某种方式更新文件,将循环值减少一,然后继续执行。

我将这样做

Sub Update_File(ByVal FilePath As String, ByRef fso As Object)
    Const ForReading = 1

    Dim lines() As String
    Dim r As Long
    Dim Dirty As Boolean

    With fso.OpenTextFile(FilePath, ForReading)
        lines = Split(.ReadAll, vbNewLine)
        .Close
    End With

    For r = 0 To UBound(lines)
        If Left(lines(r), 1) <> "'" Then
            lines(r) = "'" & lines(r)
            Dirty = True
        End If
    Next

    If Dirty Then
        With fso.CreateTextFile(FilePath, True)
            .Write Join(lines, vbNewLine)
            .Close
        End With
    End If

End Sub
子更新_文件(ByVal FilePath作为字符串,ByRef fso作为对象)
常数ForReading=1
将线()变暗为字符串
变暗,变长
Dim Dirty作为布尔值
使用fso.OpenTextFile(文件路径,用于读取)
行=拆分(.ReadAll,vbNewLine)
.结束
以
对于r=0到UBound(线)
如果左(第(r)行,1)“”,则
行(r)=“'”和行(r)
脏=真
如果结束
下一个
如果脏的话
使用fso.CreateTextFile(FilePath,True)
.写入联接(行、vbNewLine)
.结束
以
如果结束
端接头

以下是我的做法

Sub Update_File(ByVal FilePath As String, ByRef fso As Object)
    Const ForReading = 1

    Dim lines() As String
    Dim r As Long
    Dim Dirty As Boolean

    With fso.OpenTextFile(FilePath, ForReading)
        lines = Split(.ReadAll, vbNewLine)
        .Close
    End With

    For r = 0 To UBound(lines)
        If Left(lines(r), 1) <> "'" Then
            lines(r) = "'" & lines(r)
            Dirty = True
        End If
    Next

    If Dirty Then
        With fso.CreateTextFile(FilePath, True)
            .Write Join(lines, vbNewLine)
            .Close
        End With
    End If

End Sub
子更新_文件(ByVal FilePath作为字符串,ByRef fso作为对象)
常数ForReading=1
将线()变暗为字符串
变暗,变长
Dim Dirty作为布尔值
使用fso.OpenTextFile(文件路径,用于读取)
行=拆分(.ReadAll,vbNewLine)
.结束
以
对于r=0到UBound(线)
如果左(第(r)行,1)“”,则
行(r)=“'”和行(r)
脏=真
如果结束
下一个
如果脏的话
使用fso.CreateTextFile(FilePath,True)
.写入联接(行、vbNewLine)
.结束
以
如果结束
端接头
这个怎么办:

currentline = File.ReadLine
NextLine = vbNullString
Do 
    If Not File.AtEndOfStream Then
        NextLine = File.ReadLine

        Do While Left$(NextLine, 1) <> "'"
            If Len(currentline) > 0 Then currentline = Trim(currentline) & " "
            currentline = currentline & Trim(NextLine)
            NextLine = File.ReadLine
        Loop
    End If

    Write #1, currentline
    currentline = NextLine

Loop Until File.AtEndOfStream
currentline=File.ReadLine
NextLine=vbNullString
做
如果不是File.AtEndOfStream,则
NextLine=File.ReadLine
在左$(下一行,1)“时执行”
如果Len(currentline)>0,则currentline=Trim(currentline)&“
currentline=currentline和Trim(下一行)
NextLine=File.ReadLine
环
如果结束
写入#1,当前行
currentline=NextLine
循环到File.AtEndOfStream
这个怎么办:

currentline = File.ReadLine
NextLine = vbNullString
Do 
    If Not File.AtEndOfStream Then
        NextLine = File.ReadLine

        Do While Left$(NextLine, 1) <> "'"
            If Len(currentline) > 0 Then currentline = Trim(currentline) & " "
            currentline = currentline & Trim(NextLine)
            NextLine = File.ReadLine
        Loop
    End If

    Write #1, currentline
    currentline = NextLine

Loop Until File.AtEndOfStream
currentline=File.ReadLine
NextLine=vbNullString
做
如果不是File.AtEndOfStream,则
NextLine=File.ReadLine
在左$(下一行,1)“时执行”
如果Len(currentline)>0,则currentline=Trim(currentline)&“
currentline=currentline和Trim(下一行)
NextLine=File.ReadLine
环
如果结束
写入#1,当前行
currentline=NextLine
循环到File.AtEndOfStream

基于TinMan的精彩代码,试试这个vSeion

Sub Test_UpdateFile()
UpdateFile ThisWorkbook.Path & "\Sample.txt", CreateObject("Scripting.FileSystemObject")
End Sub

Sub UpdateFile(ByVal filePath As String, ByRef fso As Object)
Const forReading = 1

Dim lines()     As String
Dim dirty       As Boolean
Dim r           As Long
Dim x           As Long
Dim k           As Long

With fso.OpenTextFile(filePath, forReading)
    lines = Split(.ReadAll, vbNewLine)
    .Close
End With

For r = 1 To UBound(lines)
    If Left(lines(r), 1) <> "'" Then
        lines(r - 1) = lines(r - 1) & " " & lines(r)
        lines(r) = Empty
        dirty = True
        x = x + 1
    End If
Next r

If dirty Then
    ReDim nLines(0 To UBound(lines) - x + 1)

    For r = 0 To UBound(lines)
        If lines(r) <> Empty Then
            nLines(k) = lines(r)
            k = k + 1
        End If
    Next r


    With fso.CreateTextFile(filePath, True)
        .Write Join(nLines, vbNewLine)
        .Close
    End With
End If
End Sub
子测试_UpdateFile()
更新文件ThisWorkbook.Path&“\Sample.txt”,CreateObject(“Scripting.FileSystemObject”)
端接头
子更新文件(ByVal filePath作为字符串,ByRef fso作为对象)
常数forReading=1
将线()变暗为字符串
Dim dirty作为布尔值
变暗,变长
暗x等长
暗k一样长
使用fso.OpenTextFile(文件路径,用于读取)
行=拆分(.ReadAll,vbNewLine)
.结束
以
对于r=1至UBound(线)
如果左(第(r)行,1)“”,则
行(r-1)=行(r-1)和“&行(r)
行(r)=空
脏=真
x=x+1
如果结束
下一个r
如果脏的话
重拨线路(0到UBound(线路)-x+1)
对于r=0到UBound(线)
如果行(r)为空,则
n线(k)=线(r)
k=k+1
如果结束
下一个r
使用fso.CreateTextFile(filePath,True)
.写入联接(nLines、vbNewLine)
.结束
以
如果结束
端接头

基于TinMan的精彩代码,试试这个vSeion

Sub Test_UpdateFile()
UpdateFile ThisWorkbook.Path & "\Sample.txt", CreateObject("Scripting.FileSystemObject")
End Sub

Sub UpdateFile(ByVal filePath As String, ByRef fso As Object)
Const forReading = 1

Dim lines()     As String
Dim dirty       As Boolean
Dim r           As Long
Dim x           As Long
Dim k           As Long

With fso.OpenTextFile(filePath, forReading)
    lines = Split(.ReadAll, vbNewLine)
    .Close
End With

For r = 1 To UBound(lines)
    If Left(lines(r), 1) <> "'" Then
        lines(r - 1) = lines(r - 1) & " " & lines(r)
        lines(r) = Empty
        dirty = True
        x = x + 1
    End If
Next r

If dirty Then
    ReDim nLines(0 To UBound(lines) - x + 1)

    For r = 0 To UBound(lines)
        If lines(r) <> Empty Then
            nLines(k) = lines(r)
            k = k + 1
        End If
    Next r


    With fso.CreateTextFile(filePath, True)
        .Write Join(nLines, vbNewLine)
        .Close
    End With
End If
End Sub
子测试_UpdateFile()
更新文件ThisWorkbook.Path&“\Sample.txt”,CreateObject(“Scripting.FileSystemObject”)
端接头
子更新文件(ByVal filePath作为字符串,ByRef fso作为对象)
常数forReading=1
将线()变暗为字符串
Dim dirty作为布尔值
变暗,变长
暗x等长
暗k一样长
使用fso.OpenTextFile(文件路径,用于读取)
行=拆分(.ReadAll,vbNewLine)
.结束
以
对于r=1至UBound(线)
如果左(第(r)行,1)“”,则
行(r-1)=行(r-1)和“&行(r)
行(r)=空
脏=真
x=x+1
如果结束
下一个r
如果脏的话
重拨线路(0到UBound(线路)-x+1)
对于r=0到UBound(线)
如果行(r)为空,则
n线(k)=线(r)
k=k+1
如果结束
下一个r
使用fso.CreateTextFile(filePath,True)
.写入联接(nLines、vbNewLine)
.结束
以
如果结束
端接头

谢谢,当我将
行(r)=“'”和行(r)
修改为
行(r-1)=行(r-1)&“&行(r)
时,这是有效的。但现在缺少的是从数组中删除脏线。在其他语言中,我会使用
unset(lines(r))
。谢谢,当我将
lines(r)=“'”和lines(r)
修改为
lines(r-1)=lines(r-1)&“&lines(r)
时,这会起作用。但现在缺少的是我