locked
Please help me with this code RRS feed

  • Question

  • User373919886 posted

    Hi !


    I have below code:


    Imports System.IO

    Public Class ScriptMain

    Public Sub Main()

    Dim folder() As String = Directory.GetFiles("U:\MyZippyFiles\", "*.zip")
    Dim filename As String
    Dim flength As Long 'kilobytes

    If UBound(folder) <> -1 Then 'ensure files in folder

    For Each filename In folder

    flength = New FileInfo(filename).Length

    If flength = 0 Then
    File.Delete(filename)
    End If

    Next

    End If

    Dts.TaskResult = Dts.Results.Success

    End Sub

    End Class


    NOw what I want to do I want to check for every zip files . If the file is being writtten then I want to return variable and set value to 1. (i.e., return myvalue = 0)
    If it is not beign written then I wnat to check the file size. If file size is 0kb then delete them and if not (i.e., file size >0kb then return myvalue = 1). How can i modified above code to achieve this???

    Thanks in advance for your help.






    Monday, May 10, 2010 6:43 AM

Answers

  • User-952121411 posted

    NOw what I want to do I want to check for every zip files
     

    Looks like you are already filtering on *.zip files, so this should already be accomplished.

    If the file is being writtten then I want to return variable and set value to 1.
     

    Let me stop you there.  You are inside Sub Main() (possibly a console or WinForms app?)which Sub's in VB.NET have no return value.  You will need to refactor this code into your own method that has a return value via a Function.

    If the file is being writtten...

    I do not understand this portion.  You want to determine if the file is actively being written to the hard drive or something?  I do not know if this is possible, but I am sure I must be misunderstanding you on this.  Can you please clarify?

    Also, you are not quite leveraging the System.IO namespace to its fullest.  It would be easier and better to iterate through the File Object itself to get all of its properties (Name, Size, etc) than just the filename property only.  Take a look to the following which is not the exact solution to your problem, but it has a good method for showing how to traverse a directory and all of its files to do something (in the case of the article it is to delete the files based on how old the file is.  In your case it will be based on size):

    How To: Use Recursion in .NET To Delete All Files and Directories in .NET:

    http://allen-conway-dotnet.blogspot.com/2010/04/how-to-recursively-delete-all-files-and.html

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 14, 2010 10:24 AM

All replies

  • User-1745705970 posted

    hi,

    Please can you mention why are u returning the value..

    u mean return(myvalue =0) and

                 return(myvalue=1).

    and Please let me know what kind of modification do u want. and actually what is the goal of this code.

    Friday, May 14, 2010 12:24 AM
  • User-952121411 posted

    NOw what I want to do I want to check for every zip files
     

    Looks like you are already filtering on *.zip files, so this should already be accomplished.

    If the file is being writtten then I want to return variable and set value to 1.
     

    Let me stop you there.  You are inside Sub Main() (possibly a console or WinForms app?)which Sub's in VB.NET have no return value.  You will need to refactor this code into your own method that has a return value via a Function.

    If the file is being writtten...

    I do not understand this portion.  You want to determine if the file is actively being written to the hard drive or something?  I do not know if this is possible, but I am sure I must be misunderstanding you on this.  Can you please clarify?

    Also, you are not quite leveraging the System.IO namespace to its fullest.  It would be easier and better to iterate through the File Object itself to get all of its properties (Name, Size, etc) than just the filename property only.  Take a look to the following which is not the exact solution to your problem, but it has a good method for showing how to traverse a directory and all of its files to do something (in the case of the article it is to delete the files based on how old the file is.  In your case it will be based on size):

    How To: Use Recursion in .NET To Delete All Files and Directories in .NET:

    http://allen-conway-dotnet.blogspot.com/2010/04/how-to-recursively-delete-all-files-and.html

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 14, 2010 10:24 AM