Consulting

Results 1 to 3 of 3

Thread: Moving file to folder with first couple matching characters

  1. #1
    VBAX Newbie
    Joined
    May 2023
    Posts
    1
    Location

    Moving file to folder with first couple matching characters

    Hello everyone,

    I'm wanting to use VBA to save some time in moving some files around. Basically, I have a list of word documents that are datasheets that are formatted as an an equipment piece name following a unique number, ie.
    13-215.1 - Pump, Syringe 123456.docx. Id like to move these documents into their respective folders with the "UNIQUEIDENTIFIER - EQUIPMENT NAME" folder ie. 13-215.1 - Pump, Syringe folder. So I have already created all the folder names and the issue is the folder names are not always exactly matching the folder names, but the unique identifiers are, so id like to move the documents to the folders based on their number code.

    So:

    ME-102 - Cabinet, Biosafety, 6 ft 234567.docx ids moved to ME-102 - Cabinet, Biosafety, 6 ft folder
    18-430.1 - Imaging Device, X-Ray Unit, Rad, Digital -TRANSFER.docx is moved to 18-430.1 - Imaging Device, X-Ray Unit, Rad, Digital folder

    **notice that the documents names include numbers/notes that are not included in the folder name**

    So my thought is that maybe we can move document to folder with when the first 8 characters of the folder name and the document name are matching? Anyone worked on something like this before?

    Thank you in advance for any help - this would save me a looooot of time

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,049
    Location
    Welcome to VBAX Jess98. Perhaps a more detailed explanation is required here. You indicated;
    1. a "List of word documents", where currently is the list situated?
    2. "already created all the folder names", is there a parent folder name?
    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

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Do any of the files with the last piece (234567.docx) have spaces?

    i.e.

    ME-102 - Cabinet, Biosafety, 6 ft 2345 67.docx --> \ME-102 - Cabinet, Biosafety, 6 ft

    Otherwise, you can just Split() the file name and build the new path

    Option Explicit
    
    'ME-102 - Cabinet, Biosafety, 6 ft 234567.docx ids moved to ME-102 - Cabinet, Biosafety, 6 ft folder
    '18-430.1 - Imaging Device, X-Ray Unit, Rad, Digital -TRANSFER.docx is moved to 18-430.1 - Imaging Device, X-Ray Unit, Rad, Digital folder
    
    
    Sub MoveFiles()
        Dim sFile1 As String, sFile2 As String, sPath1 As String, sPath2 As String
        Dim v As Variant
        Dim i As Long
        
        
        'inputs
        sPath1 = "d:\Test"
        sPath2 = "d:\Test1\"
        
        sFile1 = "ME-102 - Cabinet, Biosafety, 6 ft 234567.docx"
        
        'get rid of last part
        v = Split(sFile1, " ")
        
        For i = LBound(v) To UBound(v) - 1
            sFile2 = sFile2 & v(i) & " "
        Next i
    
    
        sFile2 = Left(sFile2, Len(sFile2) - 1)
           
        Name (sPath1 & "\" & sFile1) As sPath2 & sFile2 & "\" & sFile1
    End Sub


    No error checking and no folder creation

    The work to loop the file list, etc. is left as an assignment for the reader





    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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