Copy all files from one directory to another directory

  • Thread starter Thread starter Phil
  • Start date Start date
Use FileCopy to copy the file to the new location then
Kill to remove it from the old location

If it is on the same drive you can use Name .. As ..

Sub TwoWaysToMove()
' METHOD ONE
FileCopy "h:\l_koch.xls", "h:\test\l_koch.xls"
Kill "h:\l_koch.xls"
' METHOD TWO
Name "h:\test\l_koch.xls" As "h:\l_kock.xls"
End Sub

Of course you'll need to use Dir to read the file names.
 
Media Lint said:
Use FileCopy to copy the file to the new location then
Kill to remove it from the old location

If it is on the same drive you can use Name .. As ..

Sub TwoWaysToMove()
' METHOD ONE
FileCopy "h:\l_koch.xls", "h:\test\l_koch.xls"
Kill "h:\l_koch.xls"
' METHOD TWO
Name "h:\test\l_koch.xls" As "h:\l_kock.xls"
End Sub

Surprisingly, you can use the Name statement to move a file between
drives, too. Try it and see.
 
Dirk
Can I use this for All Files, *.* ?
Because I won't know the numerous individual file names contained in a
folder I'm trying to backup.
Thanks
 
Edward said:
Dirk
Can I use this for All Files, *.* ?
Because I won't know the numerous individual file names contained in a
folder I'm trying to backup.
Thanks

No, but you can use a Dir() loop to move all files:

Dim strFile As String
Dim strSourceFolder As String
Dim strTargetFolder As String

strSourceFolder = "C:\Source Folder\"
strTargetFolder = "D:\Backup Folder\"

strFile = Dir(strSourceFolder & "*.*")
While Len(strFile) > 0
Name strSourceFolder & strFile As strTargetFolder & strFile
strFile = Dir()
Wend
 
Dirk
Using method #2, It actually Cuts & Pastes the files, Not copies. Scary.
Also it doesn't copy folders.
I need to "Copy" all files & folders...
And this seemed so simple when I was assuring someone it would work.
thanks
 
Edward said:
Dirk
Using method #2, It actually Cuts & Pastes the files, Not copies.
Scary. Also it doesn't copy folders.
I need to "Copy" all files & folders...
And this seemed so simple when I was assuring someone it would work.
thanks

The original question was about *moving* a file, not copying it, and
that's what the Name statement was presented to do. It does *not* cut &
paste in any sense except a metaphorical one -- that is, it doesn't put
the file or its name on the clipboard -- but it moves the file without
leaving a copy behind.

The Name statement can indeed move a folder and its contents, at least
on the same drive. I haven't tested it to see if it can move it from
drive to drive, but I have no reason as yet to believe that it can't.
The reason the code I gave you didn't move folders was that the Dir
function wasn't being passed the necessary argument to get it to return
folder names. See the help file entry on the Dir function.

Did you want to *copy* files instead of moving them? You should have
said so, since that wasn't what we were previously talking about. For
that you would want to either use the FileCopy statement or the
facilities available to the FileSystemObject, which requires Windows
Scripting support (though that's usually installed). Unfortunately, the
FileCopy statement won't copy folders directly, so you need more complex
logic to copy all files and folders, so the FileSystemObject may be your
better bet *if* you can rely on Windows Scripting being available.
 
Dirk
Sorry for any misunderstanding of my objective. My original question to you
about copying was under someone else's post, about Moving. I'm trying to
add a Backup utility to an application.

Briefly, to use FileSystemObject and Windows Scripting in Access 97, how
should I get started? Do I need VB 5/6? I can't find much information on
this.
Thanks
Ed
 
Edward said:
Dirk
Sorry for any misunderstanding of my objective. My original question
to you about copying was under someone else's post, about Moving.
I'm trying to add a Backup utility to an application.

Briefly, to use FileSystemObject and Windows Scripting in Access 97,
how should I get started? Do I need VB 5/6? I can't find much
information on this.
Thanks
Ed

I haven't worked with it in Access 97 myself, and not much in Access
2002, though it is at least documented in Access 2002. I believe you'd
need to set a reference to Microsoft Scripting Runtime, and use code
along the lines of

Dim fso As FileSystemObject

Set fso = New FileSystemObject

fso.CopyFolder "C:\SourceFolder", "C:\BackupFolder"

Set fso = Nothing
 
Or use late binding in order to avoid having to set a reference:

Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFolder "C:\SourceFolder", "C:\BackupFolder"
Set fso = Nothing
 
Ken
Will CopyFolder work in Access 97?
Ed
Ken Snell said:
Or use late binding in order to avoid having to set a reference:

Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFolder "C:\SourceFolder", "C:\BackupFolder"
Set fso = Nothing
 
I don't have ACCESS 97 here to try it, but I believe that FileScripting is
available there.
 
Ken Snell said:
I don't have ACCESS 97 here to try it, but I believe that
FileScripting is available there.

It will, provided that Windows Scripting is supported by the operating
system. I tested in Access 97 under Windows 2000. But Ed should be
aware that scripting was not installed by default in some older versions
of Windows (not in Windows 95, I think), and also that some system
adminstrators disable it for security reasons.
 
First of all, If you have a file producing an error when you try to copy or
move it, you need to address that problem. Outside of Access you need to
delete it, repair it, or move all other valid files you want to copy/move to
another folder.

I have the file select and folder select and copy file functions working
Great now.

Mani said:
Hi,
I have a similar situation. I am trying to copy files and folders from a
specific path excluding one file and one folder. When i used the code below,
I copy a list of files excluding one. But if i have a file like default
(without ext) there is a file error. please help!
-Thanks.



TargetFolder = "C:\script\example\targer_my"

ParentFolder = "C:\script\export\my_move"

Set objShell = CreateObject("Shell.Application")

Set objFolder = objShell.Namespace(TargetFolder)
Set objFolder_move = objShell.NameSpace(ParentFolder)
Set colItems = objFolder.Items

For i = 0 to colItems.Count - 1

if colItems.Item(i) <> "default.aspx.vb" Then
Set objFSO = CreateObject("Scripting.FileSystemObject")
name = colItems.Item(i)
objFSO.CopyFile ("C:\script\example\targer_my\" & name & ".*" ),
"C:\script\export\my_move\" , True
'objFSO.CopyFolder ("C:\script\example\targer_my"),
"C:\script\export\my_move" , True
 
Back
Top