Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 30
Default Backup Copy of Source File

I would like to make a make a backup copy and dump it into one directory
deeper called 'Archive' before manipulating the file in the current
directory. If the filename already exists in the Archive directory, then I
would want to add a '-01' or '-02' etc.. to the next revision level. I know
about the FileCopy command, but have no idea how to determine if the file
already exists inthe archive directory and therefore add the next available
revision number.

Thanks
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Backup Copy of Source File

Bob,

Sub BackUpWithIncrementedName()
Dim i As Integer
Dim myFN As String
i = 0

FindAName:
i = i + 1
myFN = ThisWorkbook.Path & "\Archive\" & _
Replace(ThisWorkbook.Name, ".xls", Format(i, "-00") & ".xls")
If Dir(myFN) = "" Then
GoTo DoTheSave
Else
GoTo FindAName
End If

DoTheSave:

ThisWorkbook.SaveCopyAs myFN
MsgBox "I just saved a copy as " & myFN

End Sub

HTH,
Bernie
MS Excel MVP


"Bob Zimski" wrote in message
...
I would like to make a make a backup copy and dump it into one directory
deeper called 'Archive' before manipulating the file in the current
directory. If the filename already exists in the Archive directory, then I
would want to add a '-01' or '-02' etc.. to the next revision level. I know
about the FileCopy command, but have no idea how to determine if the file
already exists inthe archive directory and therefore add the next available
revision number.

Thanks



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 735
Default Backup Copy of Source File

I use the following code to test the presence of a file, if it exists then
it loops until it finds the next serial number....you maybe able to adapt?

If these exist already
File_Rep.xls
File_Rep(1).xls
File_Rep(2).xls

then the file is named File_Rep(3)


Dim myPath As String, myFile As String, myExt As String, mySerial As String

mySerial = ""
myPath = "C:\Data\"
myFile = "File_Rep"
myExt = ".xls"

' create output using sequence 1 to n if report already exists
If Len(Dir(myPath & myFile & mySerial & myExt)) 0 Then

Do While Len(Dir(myPath & myFile & mySerial & myExt)) 0
mySerial = "(" & Val(Mid(mySerial, 2)) + 1 & ")"
Loop

End If

ThisWorkBook.SaveAs Filename:=myPath & myFile & mySerial & myExt


--

Regards,
Nigel




"Bob Zimski" wrote in message
...
I would like to make a make a backup copy and dump it into one directory
deeper called 'Archive' before manipulating the file in the current
directory. If the filename already exists in the Archive directory, then I
would want to add a '-01' or '-02' etc.. to the next revision level. I
know
about the FileCopy command, but have no idea how to determine if the file
already exists inthe archive directory and therefore add the next
available
revision number.

Thanks


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Backup Copy of Source File

I should have noticed that this will fail and go into an infinite loop if you have 99 copies. Add
in this to prevent that:

FindAName:
i = i + 1
If i 99 Then
MsgBox "You have waaaaay too many backup files...."
Exit Sub
End If
.....


HTH,
Bernie
MS Excel MVP


"Bernie Deitrick" <deitbe @ consumer dot org wrote in message
...
Bob,

Sub BackUpWithIncrementedName()
Dim i As Integer
Dim myFN As String
i = 0

FindAName:
i = i + 1
myFN = ThisWorkbook.Path & "\Archive\" & _
Replace(ThisWorkbook.Name, ".xls", Format(i, "-00") & ".xls")
If Dir(myFN) = "" Then
GoTo DoTheSave
Else
GoTo FindAName
End If

DoTheSave:

ThisWorkbook.SaveCopyAs myFN
MsgBox "I just saved a copy as " & myFN

End Sub

HTH,
Bernie
MS Excel MVP


"Bob Zimski" wrote in message
...
I would like to make a make a backup copy and dump it into one directory
deeper called 'Archive' before manipulating the file in the current
directory. If the filename already exists in the Archive directory, then I
would want to add a '-01' or '-02' etc.. to the next revision level. I know
about the FileCopy command, but have no idea how to determine if the file
already exists inthe archive directory and therefore add the next available
revision number.

Thanks





  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 389
Default Backup Copy of Source File

Here's a couple of reusable functions which are a variation of Bernie's
excellent approach.

Sub tester()
MsgBox GetNextIncrementedFullFilename("D:\Test", ".xls")
End Sub

Function GetNextIncrementedFullFilename(RootFilename As String, Optional
ByVal FileExt As String = ".xls") As String
Dim szFullFilename As String, i As Long
szFullFilename = RootFilename & FileExt
Do
If FileExists(szFullFilename) Then
' Found file, so increment it by one
i = i + 1
szFullFilename = RootFilename & "_" & Format(CStr(i), "00") &
FileExt
Else
' No file with this name.
Exit Do
End If
Loop
GetNextIncrementedFullFilename = szFullFilename
End Function

Public Function FileExists(ByVal strFullFileName As String) As Boolean
On Error GoTo ErrHandler
If Dir(strFullFileName) < "" And Len(strFullFileName) 0 Then
FileExists = True
Else
FileExists = False
End If
Exit Function
ErrHandler:
FileExists = False
End Function


--
Tim Zych
http://www.higherdata.com
Workbook Compare - Excel data comparison
Free & Pro versions


"Bob Zimski" wrote in message
...
I would like to make a make a backup copy and dump it into one directory
deeper called 'Archive' before manipulating the file in the current
directory. If the filename already exists in the Archive directory, then I
would want to add a '-01' or '-02' etc.. to the next revision level. I
know
about the FileCopy command, but have no idea how to determine if the file
already exists inthe archive directory and therefore add the next
available
revision number.

Thanks





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 389
Default Backup Copy of Source File

I think that part is OK:

In the immediate window:
?format(9999,"-00")
-9999

--
Tim Zych
http://www.higherdata.com
Workbook Compare - Excel data comparison
Free & Pro versions


"Bernie Deitrick" <deitbe @ consumer dot org wrote in message
...
I should have noticed that this will fail and go into an infinite loop if
you have 99 copies. Add in this to prevent that:

FindAName:
i = i + 1
If i 99 Then
MsgBox "You have waaaaay too many backup files...."
Exit Sub
End If
....


HTH,
Bernie
MS Excel MVP


"Bernie Deitrick" <deitbe @ consumer dot org wrote in message
...
Bob,

Sub BackUpWithIncrementedName()
Dim i As Integer
Dim myFN As String
i = 0

FindAName:
i = i + 1
myFN = ThisWorkbook.Path & "\Archive\" & _
Replace(ThisWorkbook.Name, ".xls", Format(i, "-00") & ".xls")
If Dir(myFN) = "" Then
GoTo DoTheSave
Else
GoTo FindAName
End If

DoTheSave:

ThisWorkbook.SaveCopyAs myFN
MsgBox "I just saved a copy as " & myFN

End Sub

HTH,
Bernie
MS Excel MVP


"Bob Zimski" wrote in message
...
I would like to make a make a backup copy and dump it into one directory
deeper called 'Archive' before manipulating the file in the current
directory. If the filename already exists in the Archive directory, then
I
would want to add a '-01' or '-02' etc.. to the next revision level. I
know
about the FileCopy command, but have no idea how to determine if the
file
already exists inthe archive directory and therefore add the next
available
revision number.

Thanks







  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Backup Copy of Source File

Tim,

Very true, but I was thinking of sorting based on names... 10 vs 100, 101,
etc...

I guess I didn't explain myself (at all).

Bernie


"Tim Zych" <feedback at higherdata dt com wrote in message
...
I think that part is OK:

In the immediate window:
?format(9999,"-00")
-9999

--
Tim Zych
http://www.higherdata.com
Workbook Compare - Excel data comparison
Free & Pro versions


"Bernie Deitrick" <deitbe @ consumer dot org wrote in message
...
I should have noticed that this will fail and go into an infinite loop if
you have 99 copies. Add in this to prevent that:

FindAName:
i = i + 1
If i 99 Then
MsgBox "You have waaaaay too many backup files...."
Exit Sub
End If
....


HTH,
Bernie
MS Excel MVP


"Bernie Deitrick" <deitbe @ consumer dot org wrote in message
...
Bob,

Sub BackUpWithIncrementedName()
Dim i As Integer
Dim myFN As String
i = 0

FindAName:
i = i + 1
myFN = ThisWorkbook.Path & "\Archive\" & _
Replace(ThisWorkbook.Name, ".xls", Format(i, "-00") & ".xls")
If Dir(myFN) = "" Then
GoTo DoTheSave
Else
GoTo FindAName
End If

DoTheSave:

ThisWorkbook.SaveCopyAs myFN
MsgBox "I just saved a copy as " & myFN

End Sub

HTH,
Bernie
MS Excel MVP


"Bob Zimski" wrote in message
...
I would like to make a make a backup copy and dump it into one directory
deeper called 'Archive' before manipulating the file in the current
directory. If the filename already exists in the Archive directory,
then I
would want to add a '-01' or '-02' etc.. to the next revision level. I
know
about the FileCopy command, but have no idea how to determine if the
file
already exists inthe archive directory and therefore add the next
available
revision number.

Thanks







Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Don't want backup copy in XLSTART Andyjim Excel Programming 9 December 31st 07 02:34 PM
Everytime i close an excel file, it creates a new backup file p Excel Discussion (Misc queries) 3 November 22nd 07 08:13 AM
copy data from source file to w/book tkraju via OfficeKB.com Excel Programming 7 March 14th 07 10:40 AM
In Excel, I want to create a backup copy whenever I save a file. Bearpecs Excel Discussion (Misc queries) 1 June 7th 06 01:52 PM
How do I shut off making a backup copy of a file when I save in E. kboley2004 Excel Discussion (Misc queries) 2 December 21st 04 10:41 PM


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

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"