vbscript VBA - 有用的功能(FileManip)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vbscript VBA - 有用的功能(FileManip)相关的知识,希望对你有一定的参考价值。


' Using FileLen("path") function
If (FileLen(TempDir) = 0) Then
        MsgBox ("COULD NOT FIND LINE IN FILE")
        Find_Line_nocmd = "No Line Found"
    Else
        'MsgBox ("Found Line")
        With CreateObject("Scripting.FileSystemObject")
            strOutput = .OpenTextFile(TempDir).ReadAll()
            .DeleteFile TempDir
        End With
        Find_Line_nocmd = strOutput
    End If
  Outline = Replace(Outline, vbCrLf, "")
    
    'Save record to file
    strFile_Path = OutputFile
    Open strFile_Path For Append As #1
    Print #1, Outline
    Close #1
Public Function RetrieveFile(fdirpath As String)

    ' Function : Find_Line_nocmd
    ' Work     : Check a specific directory for a filename with pattern
    ' Called as: Find_Line_nocmd(string, /path/to/file)
    ' Called at: Main Form Find Line Button
    ' Input    : string to be checked, file_in (path)
    ' Notes    : This is the one actually used in main form.



    Dim FullSearchString As String
    Dim File As String
    Dim fpattern As String
    
    fpattern = "AA*PPC"
    
    FullSearchString = fdirpath & fpattern
    File = Dir(FullSearchString)
    MsgBox File
    RetrieveFile = File
End Function



' With two parameters
Public Function RetrieveFile(fdirpath As String, fpattern As String)

    ' Function : Find_Line_nocmd
    ' Work     : Check a specific directory for a filename with pattern
    ' Called as: Find_Line_nocmd(string, /path/to/file)
    ' Called at: Main Form Find Line Button
    ' Input    : string to be checked, file_in (path)
    ' Notes    : This is the one actually used in main form.



    Dim FullSearchString As String
    Dim File As String
    Dim fpattern As String
    
    'fpattern = "AA*PPC"
    
    FullSearchString = fdirpath & fpattern
    File = Dir(FullSearchString)
    MsgBox File
    RetrieveFile = File
End Function
' With cmd
Public Function Find_Line(spattern As String, File_in As String) As String
    ' This version blips cmd
    
    ' Function : Find_Line
    ' Work     : Check a specific file for a line and print it
    ' Called as: Find_Line(string, /path/to/file)
    ' Called at: Main Form Find Line Button
    ' Input    : string to be checked, file_in (path)
    
    
    Dim WSH As WshShell
    Dim oExec As Object
    Dim oOutput As Object
    
    Set WSH = New WshShell
    Set oExec = WSH.Exec("cmd /C findstr " & spattern & Space(1) & File_in)
    Set oOutput = oExec.StdOut
    
    Dim s As String
    Dim sLine As String
    While Not oOutput.AtEndOfStream
        sLine = oOutput.ReadLine
        If sLine <> "" Then s = s & sLine & vbCrLf
    Wend
    Find_Line = s
    
    ' This does not blip cmd
    'Set oExec = WSH.Run("cmd /C findstr " & spattern & Space(1) & File_in & " > C:\Users\p.doulgeridis\Desktop\out.txt", 0, True
    
    
    
    
    
    
    'Dim resp As String
    'resp = redirect(StdOut)
    'S = resp
    'S = StdOut.ReadLine
    
    'Do Until StdOut.AtEndOfStream
    '    S = S & vbCrLf & StdOut.ReadLine
    'Loop
    'MsgBox (S)
    'Find_Line = S
End Function


' Without cmd
Public Function Find_Line_nocmd(spattern As String, File_in As String) As String
    
    
    ' Function : Find_Line_nocmd
    ' Work     : Check a specific file for a line and print it, no cmd window
    ' Called as: Find_Line_nocmd(string, /path/to/file)
    ' Called at: Main Form Find Line Button
    ' Input    : string to be checked, file_in (path)
    ' Notes    : This is the one actually used in main form.
    
    ' Construct command line
    Dim cmdCommand As String
    Dim strOutput
    
    'cmdCommand = "cmd.exe /C findstr " & spattern & Space(1) & File_in & " > C:\out.txt"
    'MsgBox (cmdCommand)
    
    With CreateObject("WScript.Shell")
        '.Run "cmd /C findstr " & spattern & Space(1) & File_in & " > C:\Users\p.doulgeridis\Desktop\out.txt", 0, True
        .Run "cmd /C findstr " & spattern & Space(1) & File_in & " > " & TempDir, 0, True
    End With
    
    With CreateObject("Scripting.FileSystemObject")
        strOutput = .OpenTextFile("C:\Users\p.doulgeridis\Desktop\out.txt").ReadAll()
        .DeleteFile "C:\Users\p.doulgeridis\Desktop\out.txt"
    End With
    
    Find_Line_nocmd = strOutput
End Function
Public Function DirExists(ByVal path_ As String) As Boolean
    'DirExists = (Len(Dir(path_)) > 0)
    ' Function : DirExists
    ' Work     : Check if specific directory exists
    ' Called as: FileExists(path/to/file)
    ' Called at: Main Form Checks
    ' Input    : Directory path
    
    If Dir(path_, vbDirectory) <> "" Then
        DirExists = True
        Exit Function
    Else
        DirExists = False
        Exit Function
    End If
End Function
Public Function FileExists(ByVal path_ As String) As Boolean

    ' Function : FileExists
    ' Work     : Check if specific file exists
    ' Called as: FileExists(path/to/file)
    ' Called at: Main Form Checks
    ' Input    : Filename
    
    FileExists = (Len(Dir(path_)) > 0)
    
    
End Function

以上是关于vbscript VBA - 有用的功能(FileManip)的主要内容,如果未能解决你的问题,请参考以下文章

vbscript REGEX功能VBA

vbscript Outlook功能代码段(VBA)

vbscript VBA最大和最小功能

vbscript MS Access VBA查询功能将CDec()应用于数字字段(有助于避免舍入错误)

vbscript VBScript或VBA数组函数

使用 VBA 控制 VBScript