Consulting

Results 1 to 5 of 5

Thread: Solved: Create folder structure

  1. #1
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location

    Solved: Create folder structure

    The following code is used to create a copy of an existing folder structure, delete the files within it, and make a modified copy of the original files to the new structure.Is there a better way to create the folder structure, without copying the files? (there can be a few hundred files involved)
    [vba]
    Option Explicit

    Dim i As Long

    Sub CopyFolder()
    Dim fs, f, f1, s, sf
    Dim Pth As String, Small As String
    Pth = "P:\1000 PRI"
    Small = Left(Pth, 7) & "s" & Right(Pth, Len(Pth) - 7)
    Set fs = CreateObject("Scripting.FileSystemObject")
    On Error Resume Next
    MkDir Small
    On Error GoTo 0
    fs.CopyFolder Pth, Small
    KillFiles Pth, Small
    End Sub

    Sub KillFiles(Pth, folderspec)
    Dim fs, f, f1, s, sf
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(folderspec)
    Set sf = f.SubFolders
    On Error Resume Next
    For Each f1 In sf
    Kill folderspec & "\" & f1.Name & "\*.*"
    ResizeFiles Pth & "\" & f1.Name
    KillFiles Pth & "\" & f1.Name, folderspec & "\" & f1.Name
    Next
    End Sub

    Sub ResizeFiles(Pth As String)
    Dim ShellComm As String
    Dim Dest As String
    Dim fil
    If Len(Dir(Pth & "\*.jpg")) > 0 Then
    Dest = Left(Pth, 7) & "s" & Right(Pth, Len(Pth) - 7)
    ShellComm = "C:\Program Files\IrfanView\i_view32.exe " & Pth & "\*.jpg _
    /resize=(800,602) /aspectratio /resample /convert=" & Dest & "\*.jpg"
    Shell (ShellComm)
    End If
    End Sub
    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Malcolm,

    Couldn't you use the FSO MoveFolder method to effectively rename the folders?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Bob,
    It's not a case of moving the folders. I should end up with two folder stuctures differing only in that the top level name is modified from 1234 Test to 1234s Test.
    I created some code to extract folder names into a collection, and then create the structure, but it was very messy looking at every file and as you can't create Top/Middle/Bottom/Detail in one MkDir command.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I thought you were renaming it.

    So how about fs.CopyFolder?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I think I got it. Make a folder instead of killing the contents!
    [VBA]
    Option Explicit

    Dim i As Long

    Sub CopyFolder()
    Dim fs, f, f1, s, sf
    Dim Pth As String, Small As String
    Pth = ActiveCell.Value
    Small = Left(Pth, 7) & "s" & Right(Pth, Len(Pth) - 7)
    Set fs = CreateObject("Scripting.FileSystemObject")
    On Error Resume Next
    MkDir Small
    On Error GoTo 0
    MakeFolders Pth, Small
    End Sub

    Sub MakeFolders(Pth, folderspec)
    Dim fs, f, f1, s, sf
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(Pth)
    Set sf = f.SubFolders
    On Error Resume Next
    For Each f1 In sf
    MkDir folderspec & "\" & f1.Name
    ResizeFiles Pth & "\" & f1.Name
    MakeFolders Pth & "\" & f1.Name, folderspec & "\" & f1.Name
    Next
    End Sub

    Sub ResizeFiles(Pth As String)
    Dim ShellComm As String
    Dim Dest As String
    Dim fil
    If Len(Dir(Pth & "\*.jpg")) > 0 Then
    Dest = Left(Pth, 7) & "s" & Right(Pth, Len(Pth) - 7)
    ShellComm = "C:\Program Files\IrfanView\i_view32.exe " & Pth & "\*.jpg /resize=(800,602) /aspectratio /resample /convert=" & Dest & "\*.jpg"
    Shell (ShellComm)
    End If
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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