Consulting

Results 1 to 2 of 2

Thread: Move files and ignore file extension?

  1. #1
    VBAX Newbie
    Joined
    Jul 2011
    Posts
    1
    Location

    Move files and ignore file extension?

    Hello

    I have a list of files that I wish to move from one folder to another. Now the problem is the list doesn't contain the file extension and there could be multiple different extensions.

    I have this code that will read the first cell and add the ".txt" extension then move the file. But ideally I want the code to search the folder ,find the first six digits match the cell then add the extension of the file it found be it ".tif" ,".xls" etc etc

    [VBA]Sub move_files_Adept()
    Dim c As Object, x, i As Long, n As Long
    Set c = CreateObject("Scripting.FileSystemObject")
    oldpath = "C:\testfilesFROM\": newpath = "C:\testfilesTO\"
    x = Range([a2], [a2].End(xlDown))
    For i = 1 To UBound(x)
    c.movefile oldpath & x(i, 1) & ".txt", newpath & x(i, 1) & ".txt"
    n = n + 1
    Next: MsgBox n & "files has been successfully moved", vbInformation: End Sub
    [/VBA]

    Any ideas?

    thanks

    Geoff

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]
    Sub move_files_Adept()
    Dim FSO As Object, rng As Range
    Dim newpath As String
    Dim i As Long, n As Long
    Dim file As Object

    Set FSO = CreateObject("Scripting.FileSystemObject")
    newpath = "C:\testfilesTO\"
    For Each file In FSO.getfolder("C:\testfilesFROM\").Files

    Set rng = Range(Range("A2"), Range("A2").End(xlDown))
    If Not IsError(Application.Match(Left$(file.Name, 6), rng, 0)) Then

    Name file.Path As newpath & file.Name
    n = n + 1
    End If
    Next file

    MsgBox n & "files has been successfully moved", vbInformation:
    End Sub[/vba]
    ____________________________________________
    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

Posting Permissions

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