ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Moving Files and creating copies (https://www.excelbanter.com/excel-programming/408532-moving-files-creating-copies.html)

Nils Titley

Moving Files and creating copies
 
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

JLGWhiz

Moving Files and creating copies
 
I would think that using SaveAs with the full path would allow you to move
and rename the files in one fell swoop.

"Nils Titley" wrote:

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


Mark Ivey[_2_]

Moving Files and creating copies
 
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

"Nils Titley" wrote in message
...
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



Nils Titley

Moving Files and creating copies
 
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


"Nils Titley" wrote:

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


Ron de Bruin

Moving Files and creating copies
 
See also
http://www.rondebruin.nl/folder.htm

--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Nils Titley" wrote in message ...
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


"Nils Titley" wrote:

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


Nils Titley

Moving Files and creating copies
 
Ron,

Thanks for the suggestion but I could not find in your pages what I need to
do.



"Ron de Bruin" wrote:

See also
http://www.rondebruin.nl/folder.htm

--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Nils Titley" wrote in message ...
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


"Nils Titley" wrote:

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



urkec

Moving Files and creating copies
 
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


--
urkec


"Nils Titley" wrote:

Ron,

Thanks for the suggestion but I could not find in your pages what I need to
do.



"Ron de Bruin" wrote:

See also
http://www.rondebruin.nl/folder.htm

--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Nils Titley" wrote in message ...
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


"Nils Titley" wrote:

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



Ron de Bruin

Moving Files and creating copies
 
Hi Nils

Checking if each file exist in a loop is the only way.


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Nils Titley" wrote in message ...
Ron,

Thanks for the suggestion but I could not find in your pages what I need to
do.



"Ron de Bruin" wrote:

See also
http://www.rondebruin.nl/folder.htm

--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Nils Titley" wrote in message ...
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


"Nils Titley" wrote:

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



Nils Titley

Moving Files and creating copies
 
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.



"Nils Titley" wrote:

Ron,

Thanks for the suggestion but I could not find in your pages what I need to
do.



"Ron de Bruin" wrote:

See also
http://www.rondebruin.nl/folder.htm

--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Nils Titley" wrote in message ...
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


"Nils Titley" wrote:

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



Nils Titley

Moving Files and creating copies
 
Urkec,

Your code worked perfectly. Thanks for helping me.



"urkec" wrote:

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


--
urkec


"Nils Titley" wrote:

Ron,

Thanks for the suggestion but I could not find in your pages what I need to
do.



"Ron de Bruin" wrote:

See also
http://www.rondebruin.nl/folder.htm

--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Nils Titley" wrote in message ...
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


"Nils Titley" wrote:

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



All times are GMT +1. The time now is 11:59 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com