PDA

View Full Version : Solved: MoveFile method of Scripting.FileSystemObject



malik641
09-28-2006, 10:16 AM
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:

Set fs = CreateObject("Scripting.FileSystemObject")
fs.MoveFile Source:="C:\SourceFolder\Test.xls", Destination:="C:\DestinationFolder\"
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 :)

Bob Phillips
09-28-2006, 10:33 AM
Look at the Name Statement in VBA help.

malik641
09-28-2006, 11:14 AM
Not sure it answers my question...this is what I get from the help files:


Name Statement
Renames a disk file, directory, or folder.
Syntax
Name oldpathname As newpathname
The Name statement syntax has these parts:
PartDescriptionoldpathnameRequired. String expression (http://javascript<b></b>:hhobj_4.Click()) 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 (http://javascript<b></b>:hhobj_5.Click()) 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:


Range("A1:A3, D3:D12, R3:U40").Interior.ColorIndex = xlNone


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

Bob Phillips
09-28-2006, 11:28 AM
Renaming the files is effectively moving them .

malik641
09-28-2006, 11:47 AM
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):

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
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???

Bob Phillips
09-28-2006, 02:06 PM
Rename the folder.

malik641
09-28-2006, 03:10 PM
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:
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 This is kinda crude, but just an example.

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

Shazam
09-28-2006, 04:32 PM
Here is a KB entry that I used in the past.


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



Hope it helps.

mdmackillop
09-28-2006, 04:44 PM
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

myFile = Dir(oldName & "\Test*." & FileType)
will handle your "Test" scenario

malik641
09-29-2006, 09:32 AM
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 :thumb

austenr
09-29-2006, 10:46 AM
Another alternative is run a VB Script. Like this one:

http://www.microsoft.com/technet/scriptcenter/resources/qanda/mar05/hey0317.mspx