Results 1 to 10 of 10

Thread: filesystemobject help!

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Norwich, UK
    Posts
    405

    Unhappy filesystemobject help!

    hello,

    i'm just starting to write a program that will look through a folder containing other folders (all on one level)
    inside these folders are lots of jpgs and pngs all the filenames either end in '1' or '2'.

    what i want to do is have the program run through all the files and all the files with 1 i then want to copy into either a table or text file (i have not decide yet which but text file seems easier)
    and it will insert files ending 2 into another text file/table.

    i also want as much file info put into the table as well ie file size, modified date, file type etc

    i know that it is probably possible to do this with the filesystemobject. the thing is that most of the tutorials i've seen (and belive me i've seen loads) all concentrate on the web side of using the fso.

    if anyone can point me in the right direction as to how to go forward i would be really grateful, as this is the first 'proper' project i've been set by my boss so i want to get it sorted

    cheers

  2. #2
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    Try this and let me know if it works for you:
    VB Code:
    1. Private Sub Command1_Click()
    2.     CheckFolder "c:\temp", "list1.txt", "list2.txt"
    3. End Sub
    4.  
    5. Public Sub CheckFolder(strFolder As String, strFile1 As String, strFile2 As String)
    6. '===================================================================================
    7. ' NOTE: to simplify processing strFile1(2) must have extensions
    8. '===================================================================================
    9. Dim strFile As String
    10. Dim intPos As Integer
    11.  
    12. On Error GoTo ErrClear
    13.  
    14.     If Not Right(strFolder, 1) = "\" Then strFolder = strFolder & "\"
    15.     Open strFolder & strFile1 For Output As #1
    16.     Open strFolder & strFile2 For Output As #2
    17.         strFile = Dir(strFolder, vbNormal)
    18.         Do While strFile <> ""
    19.             If strFile <> "." And strFile <> ".." Then
    20.                 If (GetAttr(strFolder & strFile) And vbNormal) = vbNormal Then
    21.                     intPos = InStrRev(strFile, ".")
    22.                     If Not (strFile = strFile1 Or strFile = strFile2) Then
    23.                         If Mid(strFile, intPos - 1, 1) = "1" Then
    24.                             Print #1, strFile
    25.                         Else
    26.                             Print #2, strFile
    27.                         End If
    28.                     End If
    29.                 End If
    30.             End If
    31.             strFile = Dir
    32.         Loop
    33.     Close #1
    34.     Close #2
    35.     Exit Sub
    36.    
    37. ErrClear:
    38. '-----------
    39.     Err.Clear
    40.     Resume Next
    41.  
    42. End Sub
    Roy

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Norwich, UK
    Posts
    405
    cheers for the reply.

    i've tried using your example and it works to a certain extent.
    but not how i want it to

    i set up a folder called test and put 7 jpgs in it 3 had file names ending in 1 and 4 in two

    when i click onthe command button nothing is pushed into the listboxes.
    but two text files are created in the test folder list1.txt contains all the jpg file names and list2.txt contains nothing.

    what i want is to have a directory structure like so

    c:\testfolder\ testfolder1
    \testfolder2
    \testfolder3
    \testfolder4

    the program will go through each folder within the testfolder parent folder.
    split the jpgs into one group that ends in1 and one that ends in two and puts them into separate files along with as much individual file info as i can get.

    so i guess that the easiest way would be t use the filesystemobject in some way. quite how i dont know.

    thanks for the help anyway

  4. #4
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    In your original post here's what you said:
    ... and it will insert files ending 2 into another text file/table ...

    So, my example will exactly what you've asked for. Isn't it? But if you want to populate a Listbox instead of spooling to a new file then just modify that procedure. Let me know if you have any problem with doing that.
    Roy

  5. #5
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    It may look some thing like this:
    VB Code:
    1. Private Sub Command1_Click()
    2.     CheckFolder "c:\temp", List1, List2
    3. End Sub
    4.  
    5. Public Sub CheckFolder(strFolder As String, lst1 As ListBox, lst2 As ListBox)
    6. '===================================================================================
    7. Dim strFile As String
    8. Dim intPos As Integer
    9.  
    10. On Error GoTo ErrClear
    11.  
    12.     If Not Right(strFolder, 1) = "\" Then strFolder = strFolder & "\"
    13.         strFile = Dir(strFolder, vbNormal)
    14.         Do While strFile <> ""
    15.             If strFile <> "." And strFile <> ".." Then
    16.                 If (GetAttr(strFolder & strFile) And vbNormal) = vbNormal Then
    17.                     intPos = InStrRev(strFile, ".")
    18.                     If Mid(strFile, intPos - 1, 1) = "1" Then
    19.                         lst1.AddItem strFile
    20.                     Else
    21.                         lst2.AddItem strFile
    22.                     End If
    23.                 End If
    24.             End If
    25.             strFile = Dir
    26.         Loop
    27.     Exit Sub
    28.    
    29. ErrClear:
    30. '-----------
    31.     Err.Clear
    32.     Resume Next
    33.  
    34. End Sub
    Roy

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Norwich, UK
    Posts
    405
    cheers for the reply

    but

    the example you posted just put all the image filenames into the same txt file, with no other info.
    and will only search on the level of c:\testing

    wheras i would want it to go through c:\testing\testing1, c:\testing\testing2, c:\testing\testing3, etc

    i hope this makes sense.

    thanks for your help so far

  7. #7
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    OK, use this procedure instead, but KIM: you will need to tune it up.
    [bncode]
    Public Sub CheckFolder(strFolder As String, lst1 As ListBox, lst2 As ListBox)
    '===================================================================================
    Dim strFile As String
    Dim intPos As Integer
    Dim strCurFile As String

    On Error GoTo ErrClear

    If Not Right(strFolder, 1) = "\" Then strFolder = strFolder & "\"
    strFile = Dir(strFolder, vbNormal)
    Do While strFile <> ""
    If strFile <> "." And strFile <> ".." Then
    If (GetAttr(strFolder & strFile) And vbNormal) = vbNormal Then
    intPos = InStrRev(strFile, ".")
    If Mid(strFile, intPos - 1, 1) = "1" Then
    strCurFile = strFolder & strFile
    intPos = InStrRev(strCurFile, ".")
    lst1.AddItem strFile & vbTab & FileLen(strCurFile) & " bytes" _
    & vbTab & FileDateTime(strCurFile) _
    & vbTab & Mid(strCurFile, intPos + 1)
    ElseIf Mid(strFile, intPos - 1, 1) = "2" Then
    strCurFile = strFolder & strFile
    intPos = InStrRev(strCurFile, ".")
    lst2.AddItem strFile & vbTab & FileLen(strCurFile) & " bytes" _
    & vbTab & FileDateTime(strCurFile) _
    & vbTab & Mid(strCurFile, intPos + 1)
    End If
    End If
    End If
    strFile = Dir
    Loop
    Exit Sub

    ErrClear:
    '-----------
    Debug.Print Err.Description
    Err.Clear
    Resume Next

    End Sub
    [/Highlight]
    Roy

  8. #8
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    Just re-posting it:
    VB Code:
    1. Public Sub CheckFolder(strFolder As String, lst1 As ListBox, lst2 As ListBox)
    2. '===================================================================================
    3. Dim strFile As String
    4. Dim intPos As Integer
    5. Dim strCurFile As String
    6.  
    7. On Error GoTo ErrClear
    8.  
    9.     If Not Right(strFolder, 1) = "\" Then strFolder = strFolder & "\"
    10.         strFile = Dir(strFolder, vbNormal)
    11.         Do While strFile <> ""
    12.             If strFile <> "." And strFile <> ".." Then
    13.                 If (GetAttr(strFolder & strFile) And vbNormal) = vbNormal Then
    14.                     intPos = InStrRev(strFile, ".")
    15.                     If Mid(strFile, intPos - 1, 1) = "1" Then
    16.                         strCurFile = strFolder & strFile
    17.                         intPos = InStrRev(strCurFile, ".")
    18.                         lst1.AddItem strFile & vbTab & FileLen(strCurFile) & " bytes" _
    19.                                              & vbTab & FileDateTime(strCurFile) _
    20.                                              & vbTab & Mid(strCurFile, intPos + 1)
    21.                     ElseIf Mid(strFile, intPos - 1, 1) = "2" Then
    22.                         strCurFile = strFolder & strFile
    23.                         intPos = InStrRev(strCurFile, ".")
    24.                         lst2.AddItem strFile & vbTab & FileLen(strCurFile) & " bytes" _
    25.                                              & vbTab & FileDateTime(strCurFile) _
    26.                                              & vbTab & Mid(strCurFile, intPos + 1)
    27.                     End If
    28.                 End If
    29.             End If
    30.             strFile = Dir
    31.         Loop
    32.     Exit Sub
    33.    
    34. ErrClear:
    35. '-----------
    36.     Debug.Print Err.Description
    37.     Err.Clear
    38.     Resume Next
    39.  
    40. End Sub
    Roy

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Norwich, UK
    Posts
    405
    thanks again

    but

    it still puts all file (ending in 1 and 2) in the same place where as i want them in separate files

    it also does not search the sub directories testing1, testing2, etc
    it only searchs the folder that i set checkfolder not all the files in all the directories

    thanks again

  10. #10
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    sagey,
    you must understand one thing: no one here will give a final solution but some example to answer your question as close as possible. You will need to modify sample code to get it to work the way you want. And then if you still have some issues give us a buzz ... But you will need to show us your version of whatever that isn't "cooperating" with your logic.

    ;-)
    Roy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width