?
Solved

Conditionally delete file

Posted on 2007-10-06
3
Medium Priority
?
184 Views
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,
Gr8life

FYI: Im using 2003 vb.net.

Code:

    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
            System.IO.File.Delete(FileToDelete)
        Else

        End If
    End Sub
0
Comment
Question by:gr8life
3 Comments
 
LVL 7

Accepted Solution

by:
prosh0t earned 1000 total points
ID: 20029090
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"
            arrFile.Add(strFile)
            writetofile(strFile, strHeader)
        Next
    End Sub

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

        sr.Write(strText)
        sr.Close()
        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
                System.IO.File.Delete(strFile)
            End If
        Next
    End Sub

good luck

0
 
LVL 11

Assisted Solution

by:AkisC
AkisC earned 1000 total points
ID: 20029904
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.
-also-
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)
        Try
            Dim F2 As Integer = FreeFile()
            If System.IO.File.Exists(thisFilePath) Then
                FileOpen(F2, thisFilePath, OpenMode.Append, OpenAccess.Default)
            Else
                FileOpen(F2, thisFilePath, OpenMode.Output, OpenAccess.Default)
                Print(F2, "My_Header")
                Print(F2, vbCrLf)
            End If
            Print(F2, writeStr)
            FileClose(F2)
            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
            FileClose(F1)
            F1 = Nothing
        End If
        Return retStr
    End Function

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

0
 

Author Comment

by:gr8life
ID: 20098349
Thank you, I ended up using parts of both your suggestions.
Gr8life
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

621 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