Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Simple backup

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Simple backup

    Code:
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    ' Simple backup procedure with incremental filenames in a subfolder.
    ' Preserve old backup copies.
    ' Use new PBWin 9 functions and statements.
    ' i.e.  "c:/source/routine.bas"  becomes  "c:/source/BkFolder/routine_bas.001"
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    SUB MakeBackup(BYVAL fName AS STRING)        'complete full drive/path/file name
       LOCAL n AS LONG
       LOCAL BkFile, Temp AS STRING
    
       'create subfolder if not exist
       BkFile = PATHSCAN$(PATH, fName) + "BkFolder"
       IF LEN(DIR$(BkFile, 16)) = 0 THEN MKDIR BkFile
    
       'add actual extension name as part of filename
       Temp = BkFile + "\" + PATHSCAN$(NAMEX, fName)
       REPLACE "." WITH "_" IN Temp
    
       'add new extension with a non existing number
       DO
          INCR n
          IF n > 990 THEN MSGBOX "BkFile limit reached ..."  'some action to alert
          BkFile = Temp + "." + FORMAT$(n, "000")
       LOOP UNTIL ISFILE(BkFile) = %FALSE                    'use first free number
    
       FILECOPY fName, BkFile                                'copy file to backup
    END SUB
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

  • #2
    Partially rewritten to add the numerical suffix to name portion of filename instead to change the extension filename.
    Additionally test the ISFILE function after update PB 9.03. Must be tested on PBCC 5.03.

    WIN32API.INC file must be included to solve %SUBDIR equate.
    Code:
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    ' Simple backup procedure with incremental filenames in a subfolder.
    '  - Preserve old backup copies.
    '  - Retaing original date and time
    '  - Maintain original file extension (more practical if you have to edit)
    '  - Original file remains unaltered
    ' Use new PBWin 9 functions. 
    ' Tested with 9.03 on Windows Vista
    ' i.e.  "C:/Source/Routine.bas"  becomes  "C:/Source/BkFolder/Routine_001.bas"
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    Sub MakeBackupN(ByVal fName As String)                   'complete full drive/path/file name
       Local n As Long
       Local BkFile,fTemp As String
    
       'create subfolder if not exist
       BkFile = PathScan$(Path, fName) + "BkFolder"          '<--- change name if wish
       If Len(Dir$(BkFile, %SUBDIR)) = 0 Then MkDir BkFile   'create new if needed
       
       'get the NAME and the EXTN parts combined.
       fTemp = BkFile + "\" + PathScan$(Namex, fName)
       
       'add to NAME portion of the path/file name with a non existing 
       'numerical suffix, retaining current extension
       Do
          Incr n
          If n > 990 Then MsgBox "BkFile limit reached ..."  'some action to alert
          BkFile = fTemp
          Replace "." With "_" + Format$(n, "000") + "." In BkFile
       Loop Until IsFile(BkFile) = %FALSE                    'use first free number
    
       FileCopy fName, BkFile                                'copy file to backup
    End Sub
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    Comment

    Working...
    X