[Solved] Printing : Printing out .pgo files found in C:Machi

  • Using VB 6.0, I would like to output to a .txt file all the .pgo files listed in "C:\Machine Files\". There are other types of files located in the directory, (.exe, .xls, .txt...) but I only want the ones with a .pgo extention. The number of .pgo files will change from day to day. The output file would look like this:


    C:\Machine Files\Machine1.pgo
    C:\Machine Files\Machine2.pgo
    C:\Machine Files\Machine3.pgo


    I really appreciate any help.:cool:

  • Here is one way using FSO


    The first sub creates the text file to write to, the second searches for the files


    Sub WriteToFile(TheText As String)
    Dim FileNum As Integer
    Dim strMsg As String

    FileNum = FreeFile

    'open log file to for appending
    'will create it if it does not exist
    Open "C:\MachineFiles\LogFile.txt" For Append As FileNum
    Print #FileNum, TheText
    Close #FileNum

    Exit Sub
    End Sub
    Sub GetFiles()
    Dim i As Integer
    Dim FileCount As Integer
    'create scripting object
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    'count of all files in sub directory
    FileCount = objFSO.GetFolder("C:\MachineFiles").Files.Count
    For i = 1 To FileCount
    'loop through until i reaches upper limit
    If objFSO.FileExists("C:\MachineFiles\Machine" & i & ".pgo") Then
    WriteToFile ("C:\MachineFiles\Machine" & i & ".pgo")
    End If
    Next i

    End Sub

  • I am new at VB so I did'nt understand exactly what to do. I copied the code and pasted it in a new VB project, pushed F5, nothing happened. I added 2 command buttons, 1 for each sub, and I still could not get it to work. Im going to need a "slower" understanding of what I need to do to get your code to work. I do appreciate your time with this issue. :(

  • If you have the directory C:\MachineFiles on your machine, look in there and a .txt file should have been created called LogFile.txt.


    If not post back and I can work up something to give you some alerts as to what is happening.

  • I created a form with one text box and one command button


    and this is the code I put in


    Option Explicit


    Private Sub Command1_Click()
    Call GetFiles
    End Sub
    Sub WriteToFile(TheText As String)
    Dim FileNum As Integer
    Dim strMsg As String


    FileNum = FreeFile


    'open log file to for appending
    'will create it if it does not exist
    Open "C:\MachineFiles\LogFile.txt" For Append As FileNum
    Print #FileNum, TheText
    Close #FileNum



    End Sub
    Sub GetFiles()
    Dim i As Integer
    Dim pgoCnt As Integer
    Dim FileCount As Integer
    Dim objFSO As Object
    'create scripting object
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    'count of all files in sub directory
    FileCount = objFSO.GetFolder(Text1.Text).Files.Count
    MsgBox FileCount & " Files Were Found"
    For i = 1 To FileCount
    'loop through until i reaches upper limit
    If objFSO.FileExists(Text1.Text & "\Machine" & i & ".pgo") Then
    pgoCnt = pgoCnt + 1
    MsgBox "Writing Machine" & i & " To Log File"
    WriteToFile (Text1.Text & "\Machine" & i & ".pgo_" & Int(Now))
    End If
    Next i
    MsgBox pgoCnt & " Files Were Written To Log"
    End Sub


    If you run the form enter the directory in the text box in my case I entered C:\MachineFiles no quotes.


    It then gave me prompts as to what was happening.


    HTH


    Bruce

  • OK, Great, the code worked perfect. But when I described to u what I needed, I was not total correct. The files located in C:\MachineFiles will be named CP6_T.pgo, CP642_T.pgo, CP643_T.pgo, IP3_T.pgo. There are a possibility of 12 different machine names. I need it to print out all the files with a .pgo extention. I changed this line of code


    If objFSO.FileExists(Text1.Text & "\Machine" & i & ".pgo") Then


    to this


    If objFSO.FileExists(Text1.Text & "\" & Chr(42)) & ".pgo") Then


    I tried to use a wild card * but it did not find anything. If u could fix this last line of code I would appreciate it. If this line of code is changed, will the print statement have to change?


    WriteToFile (Text1.Text & "\Machine" & i & ".pgo_" & Int(Now))


    :cool:

  • Okay try this. I changed it up a little and tested on my machine and it seems to do what your are asking. Post back if you have any more questions about it.


    Option Explicit


    Private Sub Command1_Click()
    GetFiles
    End Sub
    Sub WriteToFile(TheText As String)
    Dim FileNum As Integer
    Dim strMsg As String


    FileNum = FreeFile


    'open log file to for appending
    'will create it if it does not exist
    Open "C:\MachineFiles\LogFile.txt" For Append As FileNum
    Print #FileNum, TheText
    Close #FileNum



    End Sub
    Function GetFiles() As Long
    Dim strFN As String
    Dim lngCount As Long
    Const ATTR = vbReadOnly + vbHidden + vbSystem + vbDirectory
    strFN = Dir(Text1.Text & "\", ATTR)
    Do Until strFN = ""
    If InStr(1, strFN, ".pgo") Then
    WriteToFile (Text1.Text & "\" & strFN & "_" & Int(Now))
    lngCount = lngCount + 1
    End If
    strFN = Dir
    Loop
    GetFiles = lngCount
    MsgBox lngCount & " Files Were Written To Log"
    End Function

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!