Consulting

Results 1 to 7 of 7

Thread: Solved: Renaming / Reorganizing files / directories

  1. #1
    Site Admin
    The Princess
    VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location

    Solved: Renaming / Reorganizing files / directories

    I would love Excel to rearrange the files in a directory.

    Sample FROM list of files

    2202-2.pdf
    2234.pdf
    2235.pdf
    2237.pdf
    2238.pdf
    2239-1.pdf
    2242.pdf
    1333-1.pdf

    Sample TO list of files/folders:

    2234 (this is a folder)
    ....2234.pdf (this is the file, now within the folder)
    2235
    ....2235.pdf
    2237
    ....2237.pdf
    2238
    ....2238.pdf
    2242
    ....2242.pdf
    1333-1.pdf (nothing has been done with these)
    2202-2.pdf
    2239-1.pdf


    [VBA]Private Sub Workbook_Open()
    'Do the folder making
    On Error GoTo
    'A message box that says "This operation has aborted due to file naming error(s)."
    'and no folders should be made at all
    End Sub
    [/vba]

    Acceptable filename formats:

    nnnn.pdf
    nnnn-n.pdf


    The path, which I'd like to be able to change IN THE CODE, is:
    P:\Stock Room

    If we need the name of the server, let's call it MyServer


    Thanks sooooo much!!!

    Help!
    ~Anne Troy

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    [VBA]Sub MovePDFs()
    Dim fName As String, myFolder As String, fileName As String
    Dim p1 As Integer, p2 As Integer
    myFolder = "P:\Stock Room\" 'Trailing backslash needed

    If Dir(myFolder, vbDirectory) = "" Then Exit Sub
    fileName = myFolder & Dir(myFolder & "*.pdf")
    Do While fileName <> myFolder
    If InStr(fileName, "-") = 0 Then
    p1 = InStrRev(fileName, "\") + 1
    p2 = InStrRev(fileName, ".") - p1
    fName = Mid(fileName, p1, p2)
    If FolderExists(myFolder & fName) = False Then
    MkDir myFolder & fName
    End If
    Name fileName As (myFolder & fName & "\" & fName & ".pdf")
    End If
    fileName = myFolder & Dir
    Loop
    End Sub

    Function FolderExists(sFolder As Variant) As Boolean
    Dim fso As Object
    Dim tf As Boolean
    Set fso = CreateObject("Scripting.FileSystemObject")
    tf = fso.FolderExists(sFolder)
    Set fso = Nothing
    FolderExists = tf
    End Function[/VBA]

  3. #3
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    You, sir, are the BOMB. That's incredible, perfect. You're a gentleman and a scholar, and your generosity is exceeded ONLY by your extremely good looks!
    ~Anne Troy

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    You're in a good mood Anne.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  5. #5
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    LOL
    ~Anne Troy

  6. #6
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    I am still using this macro at least weekly. I love it. Thank you!!
    ~Anne Troy

  7. #7
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Glad it worked out for you Anne.

    You can remove the Function part if you like.
    [VBA]Sub MovePDFs()
    Dim fName As String, myFolder As String, fileName As String
    Dim p1 As Integer, p2 As Integer
    myFolder = "P:\Stock Room\" 'Trailing backslash needed

    If Dir(myFolder, vbDirectory) = "" Then Exit Sub
    fileName = myFolder & Dir(myFolder & "*.pdf")
    Do While fileName <> myFolder
    If InStr(fileName, "-") = 0 Then
    p1 = InStrRev(fileName, "\") + 1
    p2 = InStrRev(fileName, ".") - p1
    fName = Mid(fileName, p1, p2)
    If Dir(myFolder & fName, vbDirectory) = "" Then
    MkDir myFolder & fName
    End If
    Name fileName As (myFolder & fName & "\" & fName & ".pdf")
    End If
    fileName = myFolder & Dir
    Loop
    End Sub[/VBA]

Posting Permissions

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