PDA

View Full Version : Move files between directories



britsfp
12-21-2006, 04:48 AM
Hello,
I need to move a .csv file to another directory once it has been read by Excel's VBA code. Now the problem is : How do I move a file?.
The examples in this website has a variable called "File", What is that "File"

Regards
Frik Brits
:banghead:

Bob Phillips
12-21-2006, 05:14 AM
OldName = "C:\MYDIR\OLDFILE"
NewName = "C:\YOURDIR\NEWFILE"
Name OldName As NewName

CBrine
12-21-2006, 09:29 AM
xld,
That's a neat bit of code. How did you figure out this usage for it? The excel help doesn't mention anything other then workbook objects. I would have used an FSO filecopy then a delete to complete the same task, which is obviously much more complex. My learning for the day.

gnod
12-21-2006, 09:48 AM
Copy, Move and Delete files and folder
http://www.rondebruin.nl/folder.htm

gnod
12-21-2006, 09:59 AM
The excel help doesn't mention anything other then workbook objects.
it does and it gives an example on how to move the file the same as xld provide.


Name Statement Example

This example uses the Name statement to rename a file. For purposes of this example, assume that the directories or folders that are specified already exist. On the Macintosh, ?HD:? is the default drive name and portions of the pathname are separated by colons instead of backslashes.

Dim OldName, NewName
OldName = "OLDFILE": NewName = "NEWFILE" ' Define file names.
Name OldName As NewName ' Rename file.

OldName = "C:\MYDIR\OLDFILE": NewName = "C:\YOURDIR\NEWFILE"
Name OldName As NewName ' Move and rename file.

CBrine
12-21-2006, 12:00 PM
gnod,
It's the old context sensitive help pulling up the wrong help screen. I ended up looking at the Name Property, not the name statement. Found the right help screen for it now. Still didn't even realize it existed.

Cal

tstom
12-21-2006, 10:44 PM
Just FYI. The FSO does have a MoveFile method. You would not have to copy it and then delete the original...

johnske
12-22-2006, 03:03 AM
It's even easier to use 'Name' ... 'As' - works on both files and folders and no need to delete original. e.g.

Sub MoveFileOrFolder()
'Illusions is a folder
Name "C:\Windows\Desktop\Illusions" As "C:\Illusions"
End Sub

Zack Barresse
12-22-2006, 12:30 PM
The Name method has been around for a while and is nice sometimes. I generally use the FSO method, but that is usually because I am already using it for something else and the associated duties are alike, thus keeping everything uniform.

I do remember having some problems with Name not working well for me, but I cannot remember the circumstances, sorry.

Ivan F Moala
12-23-2006, 10:54 PM
It's even easier to use 'Name' ... 'As' - works on both files and folders and no need to delete original. e.g.

Sub MoveFileOrFolder()
'Illusions is a folder
Name "C:\Windows\Desktop\Illusions" As "C:\Illusions"
End Sub


Yes, but that then just becomes a COPY, Move is a fact a Copy and Delete.

johnske
12-23-2006, 11:51 PM
Hi Ivan,

Well, when I execute that bit of code the folder 'Illusions' disappears from the desktop and appears in the 'C' folder. So if that's the copy, where has the original gone to?

I think it's fair to say it's been deleted, but perhaps this works differently in different versions (for me: Office 2K, Win98)

Regards,
John :)

Ivan F Moala
12-24-2006, 07:07 PM
Hi Ivan,

Well, when I execute that bit of code the folder 'Illusions' disappears from the desktop and appears in the 'C' folder. So if that's the copy, where has the original gone to?

I think it's fair to say it's been deleted, but perhaps this works differently in different versions (for me: Office 2K, Win98)

Regards,
John :)

John, sorry :) , I think I was thinking of the DOS REN , RENAME command

johnske
12-25-2006, 01:35 AM
Thought it might've been something like that :) Anyhoo, have a merry christmas Ivan (or what's left of it)