Moving Files and creating copies

  • Thread starter Thread starter Nils Titley
  • Start date Start date
N

Nils Titley

FSO.MoveFile

With FSO.MoveFile, can you move the files and if they exist write them with
a new name. If not how can I do that?

Thanks
 
I would think that using SaveAs with the full path would allow you to move
and rename the files in one fell swoop.
 
I would recommend using some addition to the filename. I typically use
something extra like date and time information added to the original
filename to avoid overwriting previous versions of the same file.

If you need some examples, let me know.

Mark
 
Let me provide additional information.

The macro will be used by all sorts of people. I want to make it easy for
them. There is a data folder and an archive folder. The macro process any
file that sits in the data folder. After each file has been processed, a
report is generated and written to the report folder that is dated and timeed
stamped.

I want to move all of the data files to the archive folder. I can do that
now. But the person asking me to help create the macro is concerned that
there might be the same files in the archive folder. I don't think that will
happen but in case, I want to move the files and if the file already exist in
the archive folder, I would like to rename it to some thing like "filename
copy".

Does that help?

Thanks
 
Ron,

Thanks for the suggestion but I could not find in your pages what I need to
do.
 
You could try copying files individually using FSO File.Move, and if it
returns 'file exists' error (58), use a different name. Something like this
(please be careful, this is not tested):

strSourceFolder = "c:\source\"
strDestFolder = "d:\destination\"

Set objFso = CreateObject("Scripting.FileSystemObject")
Set objSourceFolder = objFso.GetFolder(strSourceFolder)

For Each objFile In objSourceFolder.Files

On Error Resume Next

objFile.Move strDestFolder

If Err.Number = 58 Then
i = 1
Do While Err.Number <> 0

Err.Clear

objFile.Move strDestFolder & "Copy Of " & _
objFso.GetBaseName(objFile) & _
" (" & i & ")" & "." & _
objFso.GetExtensionName(objFile.Path)

i = i + 1

Loop
Else
Debug.Print Err.Number, Err.Description
End If

On Error GoTo 0

Next
 
Thanks for the help. I will give it a try. I dont' think it is necessary to
check if they exist but better to check than to get an error and have the
move fail. I don't think it is necessary to check because I don't think
there will be duplicate files.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top