Solved

Please Help WriteFile API

Posted on 2001-06-02
5
258 Views
Last Modified: 2006-11-17
Hi I can someone give me some examples and reading and writeing files useing the Win API at the moment I have just been useing the normal vb way Open file for binary and so On.

Anyway Since I made a new program that can removes files form your system like a file wipeing tool I have run in to problums with the speed when dealing with big files anyway I hope someone can help. I have also included some of the code I am useing the so you will see what I mean when I say it goes solw. well I hope someone can help anyway I will be very helpfull.

Private Function WipeFiles(lzPath As String)
Dim Icount As Long
Dim TChar1 As String
Dim TChar2 As String
Dim T As Long

TFile = FreeFile
    On Error Resume Next
    x = Dir(lzPath, vbHidden)
    Do While x <> ""
        Open lzPath & x For Binary As #TFile
            FLen = LOF(TFile)
            For Icount = 0 To FLen
                    Randomize
                    TChar1 = Chr(Val(Int(160 * Rnd) + 1))
                    TChar2 = Chr(Val(Int(60 * Rnd) + 2))
                    T = Seek(TFile)
                    Put #TFile, TFile, TChar1
                    Put #TFile, T, TChar2
            Next
        Close #TFile
    Kill lzPath & x
    x = Dir
    Loop
    TChar1 = "": TChar2 = "": FLen = 0: Icount = 0: T = 0
   
End Function

Also please let me know if there is some thing wrong with the code I useing. or why it might be going slow
0
Comment
Question by:dreamvb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 6

Accepted Solution

by:
sharmon earned 30 total points
ID: 6149573
It's not your kill that is slowing it down it's the fact that you are looping through and writing to the file but not just any character you are also grabbing a random char to wipe it with.  Use bigger blocks and a predefined char to write with as in this example and it should be faster.


Sub WipeFileClean (sFileName As String)
    Dim Block1 As String, Block2 As String, Blocks As Long
    Dim hFileHandle As Integer, iLoop As Long, Offset As Long
    'Create two buffers with a specified 'wi
    '     pe-out' characters
    Const BLOCKSIZE = 4096
    Block1 = String(BLOCKSIZE, "X")
    Block2 = String(BLOCKSIZE, " ")
    'Overwrite the file contents with the wi
    '     pe-out characters
    hFileHandle = FreeFile
    Open sFileName For Binary As hFileHandle
    Blocks = (LOF(hFileHandle) \ BLOCKSIZE) + 1


    For iLoop = 1 To Blocks
        Offset = Seek(hFileHandle)
        Put hFileHandle, , Block1
        Put hFileHandle, Offset, Block2
    Next iLoop
    Close hFileHandle
    'Now you can delete the file, which cont
    '     ains no sensitive data
    Kill sFileName
End Sub
0
 
LVL 3

Expert Comment

by:Hornet241
ID: 6150096
I'm not sure if doing this is the same a scrambling the data in a file but I don't know of a way to recover from this

      Open lzPath & x For Output #TFile
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6150263
>Use bigger blocks and a predefined char to write with as in this example and it should be faster.

As long as you use bigger blocks it doesn't matter if you use a predefined character or not.  Randomizing the data in the block is not the slow part.
0
 
LVL 4

Author Comment

by:dreamvb
ID: 6150755
Thanks this now seems to work a lot better now

Thanks again and also thanks the rest of you.
0
 
LVL 6

Expert Comment

by:sharmon
ID: 6150888
PaulHews,

I haven't timed it at all, but shouldn't throwing two more function calls in the middle of the loop slow it down more?  I can understand it might not be much, but wouldn't it be something a bit slower?  Thanks for the info if you respond.

Shannon
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Saving history changes to sub form 4 56
passing a value with stream reader AFTER a ";" 3 92
Win 10 Automation Error with .doCmd in Access 2 92
VB6 Enviroment 3 37
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question