Conditionally delete file

Posted on 2007-10-06
Last Modified: 2010-04-23

Good evening experts& I have a small application that builds 35 output files onload with headers. Before the application ends I want to basically delete the files that only contain the header and no other data.

Here is as close as I have been able to get.

Thank you very much for your time and expertise,

FYI: Im using 2003


    Public Sub noData(ByVal inFile, ByVal outFile1, ByVal outFile2, ByVal outFile3, ByVal outFile4, ByVal outFile5, ByVal outFile6, ByVal outFile7, ByVal outFile8, _
    ByVal outFile9, ByVal outFile10, ByVal outFile11, ByVal outFile12, ByVal outFile13, ByVal outFile14, ByVal outFile15, ByVal outFile16, _
    ByVal outFile17, ByVal outFile18, ByVal outFile19, ByVal outFile20, ByVal outFile21, ByVal outFile22, ByVal outFile23, _
    ByVal outFile24, ByVal outFile25, ByVal outFile26, ByVal outFile27, ByVal outFile28, ByVal outFile29, ByVal outFile30, ByVal outFile31, ByVal outFile32, _
    ByVal outFile33, ByVal outFile34, ByVal outFile35 As String)
        Dim FileToDelete As String

        If ((outFile1.Length = 1) And (outFile2.Length = 1) And (outFile3.Length = 1) And (outFile4.Length = 1) _
        And (outFile5.Length = 1) And (outFile6.Length = 1) And (outFile7.Length = 1) And (outFile8.Length = 1) _
        And (outFile9.Length = 1) And (outFile10.Length = 1) And (outFile11.Length = 1) And (outFile12.Length = 1) _
        And (outFile13.Length = 1) And (outFile14.Length = 1) And (outFile15.Length = 1) And (outFile16.Length = 1) _
        And (outFile17.Length = 1) And (outFile18.Length = 1) And (outFile19.Length = 1) And (outFile20.Length = 1) _
        And (outFile21.Length = 1) And (outFile22.Length = 1) And (outFile23.Length = 1) And (outFile24.Length = 1) _
        And (outFile25.Length = 1) And (outFile26.Length = 1) And (outFile27.Length = 1) And (outFile28.Length = 1) _
        And (outFile29.Length = 1) And (outFile30.Length = 1) And (outFile31.Length = 1) And (outFile32.Length = 1) _
        And (outFile33.Length = 1) And (outFile34.Length = 1) And (outFile35.Length = 1)) Then

        End If
    End Sub
Question by:gr8life
    LVL 7

    Accepted Solution

    I'm not totally sure I understand what you're doing but i think maybe i have it.  Basically you want to do away with 35 variables and put them into an array.  It will save yourself a lot of time.  Here's the code you should need in a form to add 35 files (i just put them in teh root of c: and called them #.txt where # is anything from 1 to 35) and add a header to them upon load, then delete any where the file has not been added to upon form close:

        Public arrFile As New ArrayList
        Public strHeader As String = "header"

        Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim i As Integer
            Dim strFile As String
            For i = 1 To 35
                strFile = "c:\" & i & ".txt"
                writetofile(strFile, strHeader)
        End Sub

        Public Sub writetofile(ByVal strfile As String, ByVal strText As String)
            Dim sr As New System.IO.StreamWriter(strfile, True)

            sr = Nothing
        End Sub

        Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
            For Each strFile As String In arrFile
                If FileLen(strFile) = Len(strHeader) Then
                End If
        End Sub

    good luck

    LVL 11

    Assisted Solution

    What is the header value that each file contains? Is it the same?
    Do the names of your files have something similar
    Like file1.txt,file2.txt,file3.txt ....

    Your routine can be a lot better.
    Why write a head to a file and you do not just open it when you want to add some value to it?
    here are to usefull functions
        Public Sub writeToFile(ByVal thisFilePath As String, ByVal writeStr As String)
                Dim F2 As Integer = FreeFile()
                If System.IO.File.Exists(thisFilePath) Then
                    FileOpen(F2, thisFilePath, OpenMode.Append, OpenAccess.Default)
                    FileOpen(F2, thisFilePath, OpenMode.Output, OpenAccess.Default)
                    Print(F2, "My_Header")
                    Print(F2, vbCrLf)
                End If
                Print(F2, writeStr)
                F2 = Nothing
            Catch ex As Exception
            End Try
        End Sub
        Private Function readFileContents(ByVal thisFilePath As String) As String
            Dim retStr As String = ""
            If System.IO.File.Exists(thisFilePath) Then
                Dim F1 As Integer = FreeFile(), rLine As String = ""
                FileOpen(F1, thisFilePath, OpenMode.Input, OpenAccess.Default)
                While Not EOF(F1)
                    rLine = LineInput(F1)
                    If Microsoft.VisualBasic.Strings.Right(rLine, 2) <> vbCrLf Then
                        rLine &= vbCrLf
                    End If
                    retStr &= rLine
                End While
                F1 = Nothing
            End If
            Return retStr
        End Function

    Tell if you need any more explanation to what each does :)


    Author Comment

    Thank you, I ended up using parts of both your suggestions.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now