Saving files Using VB

I am creating a procedure to back up a mdb file to a number of floppy discs. How do I make the file span a number of discs on the save and then be able to copy them back and rejoin the file to open again on the PC

Comments

  • You can use the Put method to write files byte by byte. The syntax is:

    Put #filenumber, bytenumber, byte

    where filenumber is the number of the file you've opened with Open, bytenumber is the number of the byte in the file (beginning with 1), and byte is the byte you want to write. It's not a fast method but I guess it's enough for a backup programm.


  • easier to say it with code than try to explain. it's a bit rough but should show you most of the principles involved. You might think about writing an additional file to each diskette with information about the original file size. That way you would know how many diskettes to expect when restoring.

    i've used buffering to cut down on Gets and Puts but it may make no difference with built-in Windows file buffering.

    All the indents seem to disappear on posting so code is a bit unreadable.

    Public Sub BackupToDiskette()
    Const BufferSize As Integer = 4096
    Const BlocksPerDiskette As Integer = 256

    'Filenames
    Dim szOriginalFileName As String
    Dim szBackupPath As String
    Dim szBackupFileName As String

    'Information about file size:
    Dim lOriginalFileSize As Long
    Dim lBlocks As Long
    Dim lRemainingBytes As Long
    Dim lBlockNumber As Long
    Dim lByteNumber As Long
    Dim iDiskNo As Integer

    'File handles:
    Dim iOriginalFileNo As Integer
    Dim iBackupFileNo As Integer

    'File buffers:
    Dim bBuffer(4095) As Byte
    Dim bByte As Byte

    'Initialise filenames:
    szOriginalFileName = "d: est.mpg"
    szBackupPath = "e:"

    'Read file size and calculate number of
    'blocks and remaining bytes:
    lOriginalFileSize = FileLen(szOriginalFileName)
    lBlocks = lOriginalFileSize / BufferSize
    lRemainingBytes = lOriginalFileSize Mod BufferSize

    'Open the file:
    iOriginalFileNo = FreeFile(0)
    Open szOriginalFileName For Binary Lock Read Write As iOriginalFileNo

    'No diskettes written to yet:
    iDiskNo = 0

    'For each full block of data,
    'plus one for the remaining bytes:
    For lBlockNumber = 0 To lBlocks
    If (lBlockNumber Mod BlocksPerDiskette = 0) Then
    'It's time for a new disk:
    If (iDiskNo <> 0) Then
    'We have to close the old output file:
    Close #iBackupFileNo
    End If

    MsgBox "New Disk Please"

    iDiskNo = iDiskNo + 1

    'Create a new backup file on the new diskette:
    szBackupFileName = szBackupPath & "backup." & Format(iDiskNo, "000")
    iBackupFileNo = FreeFile(0)
    Open szBackupFileName For Binary Lock Read Write As iBackupFileNo

    End If

    If (lBlockNumber = lBlocks) Then
    'Get and Put the remaining bytes:
    For lByteNumber = 1 To lRemainingBytes
    Get #iOriginalFileNo, , bByte
    Put #iBackupFileNo, , bByte
    Next lByteNumber
    Else
    'Get and put a whole block:
    Get #iOriginalFileNo, , bBuffer
    Put #iBackupFileNo, , bBuffer
    End If

    Next lBlockNumber

    Close iBackupFileNo
    Close iOriginalFileNo

    End Sub

    Public Sub RestoreFromDiskette()
    Const BufferSize As Integer = 4096
    Const BlocksPerDiskette As Integer = 256

    'Filenames
    Dim szRestoreFileName As String
    Dim szBackupPath As String
    Dim szBackupFileName As String

    'Information about file size:
    Dim lBackupFileSize As Long
    Dim lBlocks As Long
    Dim lRemainingBytes As Long
    Dim lBlockNumber As Long
    Dim lByteNumber As Long

    'File handles:
    Dim iRestoreFileNo As Integer
    Dim iBackupFileNo As Integer

    Dim iDiskNo As Integer

    'File buffers:
    Dim bBuffer(4095) As Byte
    Dim bByte As Byte

    'Initialise filenames:
    szRestoreFileName = "d:copy.mpg"
    szBackupPath = "e:"

    'Create the file we are restoring:
    iRestoreFileNo = FreeFile(0)
    Open szRestoreFileName For Binary Lock Read Write As iRestoreFileNo

    'For each disk in the set...
    For iDiskNo = 1 To 999
    MsgBox "Put disk #" & Format(iDiskNo) & " in the drive."

    szBackupFileName = szBackupPath & "backup." & Format(iDiskNo, "000")
    If (Dir(szBackupFileName) = "") Then
    Exit For
    End If

    'Work out file size:
    lBackupFileSize = FileLen(szBackupFileName)
    lBlocks = lBackupFileSize / BufferSize
    lRemainingBytes = lBackupFileSize Mod BufferSize

    'Open the file:
    iBackupFileNo = FreeFile(0)
    Open szBackupFileName For Binary Lock Read Write As iBackupFileNo

    'Append it to the restore file:
    'For each full block of data,
    'plus one for the remaining bytes:
    For lBlockNumber = 0 To lBlocks
    If (lBlockNumber = lBlocks) Then
    'Get and Put the remaining bytes:
    For lByteNumber = 1 To lRemainingBytes
    Get #iBackupFileNo, , bByte
    Put #iRestoreFileNo, , bByte
    Next lByteNumber
    Else
    'Get and put a whole block:
    Get #iBackupFileNo, , bBuffer
    Put #iRestoreFileNo, , bBuffer
    End If

    Next lBlockNumber

    'Close the backup file:
    Close iBackupFileNo

    Next iDiskNo

    'Close the restore file:
    Close iRestoreFileNo
    End Sub


    : I am creating a procedure to back up a mdb file to a number of floppy discs. How do I make the file span a number of discs on the save and then be able to copy them back and rejoin the file to open again on the PC
    :


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories