Results 1 to 10 of 10
  1. #1
    Join Date
    Mar 2004
    Posts
    10

    Unanswered: Create a directory based on the file name

    I have some files are named file101, file 102, file 103, etc, I want to automatically create a directory based on the last 3 numbers, such as c:\101\, c:\102, c:\103 ....dynamically.

    What's are the fuctions to the functions to read the filename from harddrive ?Please help.

  2. #2
    Join Date
    Jan 2004
    Location
    Toronto, Canada
    Posts
    259
    Read up on FSO (FileSystemObjects)

    They allow you to create, navigate folders, do other stuff...

  3. #3
    Join Date
    Mar 2004
    Posts
    10
    Yes I did create a directory by using FileSystemObjects. But I need to identify the file name (last 3 numbers in the file name) first. I am not sure how to do that......thx!

  4. #4
    Join Date
    Jan 2004
    Location
    Toronto, Canada
    Posts
    259
    if they are all constant, as in the last three characters are the only ones you want to extract..then

    pseudocode:

    for each file in the folder
    Dim temp as string
    temp = Mid(file.Name, Len(file.name) - 2, 3) ' this will give the last three characters.

    make a new folder with temp as the name

  5. #5
    Join Date
    Jan 2004
    Location
    Toronto, Canada
    Posts
    259
    if you want teh VB code for that, just let me know

  6. #6
    Join Date
    Mar 2004
    Posts
    10
    Can you give me the vb code?
    Thanks very much!

  7. #7
    Join Date
    Jan 2004
    Location
    Toronto, Canada
    Posts
    259
    1. In your references, make sure to have "Microsoft Scripting Runtime" checked off.
    2. Here's the code for a function that goes through all the files in the folder mentioned in foldername and makes a folder out of them.

    Code:
    Private Function CreateFolders(folderName As String)
    Dim fso As New FileSystemObject
    Dim rootfolder As Folder
    Dim curobject As File
    Set rootfolder = fso.GetFolder(folderName)
    Dim segment As String 'stores the last three chars.
    
    For Each curobject In rootfolder.Files
        Debug.Print curobject.Name
        segment = Mid(curobject.Name, 5, 3)
        Debug.Print segment
        If fso.FolderExists(folderName & "\" & segment) = False Then
            fso.CreateFolder (folderName & "\" & segment)
        End If
    Next
    End Function

  8. #8
    Join Date
    Jan 2004
    Location
    Toronto, Canada
    Posts
    259
    note however, this goes through ALLLL the files in that folder...not just a selection of them. and it will make a folder based on the 5th to 7th characters of that filename. for all those files

  9. #9
    Join Date
    Mar 2004
    Posts
    10

    Thumbs up

    I did some modifications, it works just fine!
    Thank you again!

  10. #10
    Join Date
    Jan 2004
    Location
    Toronto, Canada
    Posts
    259
    no problem at all!

Posting Permissions

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