Consulting

Results 1 to 11 of 11

Thread: Solved: MoveFile method of Scripting.FileSystemObject

  1. #1
    Administrator
    2nd VP-Knowledge Base
    VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location

    Solved: MoveFile method of Scripting.FileSystemObject

    Hey Guys,

    I'm trying to move multiple files from one directory to another. I've found in the help files that the .MoveFile method can move multiple files (as opposed the the .Move method). The thing is that they don't explain how to move multiple files (in one shot)...like do I send it an array? Can it be multidimensional? Is there a specific syntax? This is how to move one file:

    [vba]Set fs = CreateObject("Scripting.FileSystemObject")
    fs.MoveFile Source:="C:\SourceFolder\Test.xls", Destination:="C:\DestinationFolder\"[/vba]
    So what about Test1.xls, Test2.xls, Test3.xls?????

    And it also says: "This method allows moving files between volumes only if supported by the operating system." What does that mean, anyway?

    Or do I just have to make a loop?? (I don't want to do that, though).

    Thanks in advance




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Look at the Name Statement in VBA help.

  3. #3
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Not sure it answers my question...this is what I get from the help files:

    Quote Originally Posted by VBA Help
    Name Statement
    Renames a disk file, directory, or folder.
    Syntax
    Name oldpathname As newpathname
    The Name statement syntax has these parts:
    PartDescriptionoldpathnameRequired. String expression that specifies the existing file name and location ? may include directory or folder, and drive. newpathnameRequired. String expression that specifies the new file name and location ? may include directory or folder, and drive. The file name specified by newpathname can't already exist.


    Remarks
    The Name statement renames a file and moves it to a different directory or folder, if necessary. Name can move a file across drives, but it can only rename an existing directory or folder when both newpathname and oldpathname are located on the same drive. Name cannot create a new file, directory, or folder.
    Using Name on an open file produces an error. You must close an open file before renaming it. Name arguments cannot include multiple-character (*) and single-character (?) wildcards.
    I guess to put it in another way what I mean to do is to perform something very similiar to when you press Ctrl+Click on seperated files, then press Right-Click --> Cut....then go to the folder you want to move them to and press Right-Click --> Paste

    Or like how you would format cells that are not in the same Area ....like:

    [vba]
    Range("A1:A3, D312, R3:U40").Interior.ColorIndex = xlNone
    [/vba]

    So is there something similar to this for moving multiple files? Like with one line of code?




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Renaming the files is effectively moving them .

  5. #5
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Even still....what's the syntax to do it without looping through different file names in the same directory (which all of them will be moved to one folder)???

    Here's my code right now...which works, it's just a big loop (if there are a lot of files to be moved):

    [VBA]Private Sub cmdMove_Click()
    Dim fs
    Dim i As Long
    Dim Src As String, Dest As String
    Src = txtSource.Text
    Dest = txtDestination.Text
    If Right(Src, 1) <> "\" Then Src = Src & "\"
    If Right(Dest, 1) <> "\" Then Dest = Dest & "\"
    Set fs = CreateObject("Scripting.FileSystemObject")
    For i = 0 To lstFiles.ListCount - 1
    fs.MoveFile Source:=Src & lstFiles.List(i), Destination:=Dest & lstFiles.List(i)
    Next i
    Unload Me
    End Sub[/VBA]
    The source folder is in txtSource.Text and the destination folder is in txtDestination.Text.....all the files are listed in a listbox (example: "Test1.xls"). I just don't want to have to loop through it, though.

    BTW, I'm not moving all the files from one folder to another. It's multiple...just not all.

    Or is there something that I'm not understanding here???




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Rename the folder.

  7. #7
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Bob, I'm sorry for all this confusion.....but I still feel that my primary question hasn't been answered.....I know you know what you're talking about, so am I asking the wrong question?

    Look, if I have 3 excel worksheets named Test1.xls, Test2.xls, and Test3.xls then how would you move these files from:

    C:\SourceFolder\

    To

    C:\DestinationFolder\

    Without looping?

    This is something I came up with:
    [vba]Sub MoveFiles()
    Dim Src As String, Dest As String
    Dim i As Long

    Src = "C:\SourceFolder\Test"
    Dest = "C:\DestinationFolder\Test"

    For i = 1 To 3
    Name Src & i & ".xls" As Dest & i & ".xls"
    Next i
    End Sub[/vba] This is kinda crude, but just an example.

    BTW, thanks for showing me the Name Statement. Nice little trick.




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  8. #8
    VBAX Expert Shazam's Avatar
    Joined
    Sep 2005
    Posts
    530
    Location
    Here is a KB entry that I used in the past.


    http://www.vbaexpress.com/kb/getarticle.php?kb_id=404



    Hope it helps.
    SHAZAM!

  9. #9
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Quote Originally Posted by Shazam
    Here is a KB entry that I used in the past.


    http://www.vbaexpress.com/kb/getarticle.php?kb_id=404



    Hope it helps.
    and changing this to line
    [VBA]
    myFile = Dir(oldName & "\Test*." & FileType)
    [/VBA]will handle your "Test" scenario
    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'

  10. #10
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Thanks guys

    For the Test.xls example, then this could be done in one shot. My real file names, though, are more unique than that, and I would hesitate to use an asterisk for my case. I don't want to move files that shouldn't be moved.

    So I guess the answer (for my case) is no...I'll stick with the loop then


    Thanks again guys, appreciate it




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  11. #11
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Another alternative is run a VB Script. Like this one:

    http://www.microsoft.com/technet/scr...5/hey0317.mspx

Posting Permissions

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