Results 1 to 15 of 15

Thread: [RESOLVED] [2008] Problems with For Statement

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Location
    Nowhere
    Posts
    427

    Resolved [RESOLVED] [2008] Problems with For Statement

    Can someone help me fix this please? Im trying to loop through 2 for statements so that the first one can get the directories and the second for statement gets all the files in each directory (also the child directories of the one its currently in) and then write the information.

    vb.net Code:
    1. Dim foundfolders As String() = IO.Directory.GetDirectories(workdir)
    2.             For Each d_irectory As String In foundfolders
    3.                 Dim filefound As String() = IO.Directory.GetFiles(d_irectory)
    4.                 For Each newarray As String In filefound
    5.                     Dim File_stream As New IO.FileStream(d_irectory & "\" & Path.GetFileName(newarray), FileMode.Open, FileAccess.Read, FileShare.Read, 8192I)
    6.                     Raw_Crc_Integer = Crc_Class.GetCrc32(File_stream)
    7.                     Writer.WriteLine("  <PATCHNODE file=" & ControlChars.Quote & Path.GetFileName(newarray) & ControlChars.Quote & ">" & vbCrLf & _
    8.                                      "      <CRC>" & Raw_Crc_Integer.ToString & "</CRC>" & vbCrLf & _
    9.                                      "      <SERVERLINK>" & (IO.Path.GetDirectoryName(newarray).Replace(Path.GetDirectoryName((newarray)), String.Empty)) & "\" & IO.Path.GetFileName(newarray) & "</SERVERLINK>" & vbCrLf & "  </PATCHNODE>")
    10.                     looped += 1
    11.                     Console.WriteLine("Writing Directories: (" & looped.ToString & "/" & amountoffoundfiles.ToString & ")")
    12.                 Next
    13.             Next

    I pretty much want to write a code that will search through every child directory in the main directory and write the information mainly the file location like this ("*This is the main directory*/*childdirectory*/filename.exe") or ("*This is the main directory*/*childdirectory*/nextchilddirectory/filename.exe").
    Last edited by youngbucks; Dec 25th, 2008 at 06:09 AM.
    "Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz


    Code Snippets/Usefull Links:
    WinRAR DLL|Vista Style Form|Krypton Component Library|Windows Form Aero|Parsing XML files|Calculate File's CRC 32|Download a list of files.

  2. #2
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: [2008] Problems with For Statement

    Theres a few parameters to the GetFiles method and the third one is a searchoption to search subdirectorys.
    Code:
    For Each File As String In IO.Directory.GetFiles(workdir, "*.*", IO.SearchOption.AllDirectories)
                'DO SOMETHING WITH FILE
    Next
    Casey.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Location
    Nowhere
    Posts
    427

    Re: [2008] Problems with For Statement

    Thanks alot that was really helpfull, lol i cant believe it was that easy and i was making it complicated =/ . I need help with one more thing though. How would i get the current directory its in and write the filename that its currently checking? Path.getfilename gets me the filename but i need to get the current directory and not the full directory so it would be "/Directoryimcurrentlyin/*childdirectoryifavailable*/filename.exe"
    "Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz


    Code Snippets/Usefull Links:
    WinRAR DLL|Vista Style Form|Krypton Component Library|Windows Form Aero|Parsing XML files|Calculate File's CRC 32|Download a list of files.

  4. #4
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: [2008] Problems with For Statement

    Are you saying you need everything after the root directory ?

    workdir\sub1\sub2\sub3\file or workdir\sub1\file

    becomes

    sub1\sub2\sub3\file or sub1\file

    Casey.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Location
    Nowhere
    Posts
    427

    Re: [2008] Problems with For Statement

    Yes thats what i meant, sorry if i wasnt clear
    "Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz


    Code Snippets/Usefull Links:
    WinRAR DLL|Vista Style Form|Krypton Component Library|Windows Form Aero|Parsing XML files|Calculate File's CRC 32|Download a list of files.

  6. #6
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: [2008] Problems with For Statement

    Cant you just use substring and IndexOf. Find the first backslash with IndexOf and use substring to grab everything one position after that.

    Casey.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Location
    Nowhere
    Posts
    427

    Re: [2008] Problems with For Statement

    Ive never used Indexof. only substring and very basic, the only thing i know at the moment is .replace but that cant help me in this situation.
    "Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz


    Code Snippets/Usefull Links:
    WinRAR DLL|Vista Style Form|Krypton Component Library|Windows Form Aero|Parsing XML files|Calculate File's CRC 32|Download a list of files.

  8. #8
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: [2008] Problems with For Statement

    IndexOf returns the first position of a character/s in a string.

    Code:
    Dim str As String = "Merry Xmas"
    
            MessageBox.Show(str.IndexOf("X").ToString)
    That returns 6 because its zero based.

    If you select any method and press F1, you will get all the help on that method.

    Casey.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Location
    Nowhere
    Posts
    427

    Re: [2008] Problems with For Statement

    oh ok thanks, one last question lol, if i want to skip a file from being written how would i do that?
    "Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz


    Code Snippets/Usefull Links:
    WinRAR DLL|Vista Style Form|Krypton Component Library|Windows Form Aero|Parsing XML files|Calculate File's CRC 32|Download a list of files.

  10. #10
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: [2008] Problems with For Statement

    If IO.Path.GetFileName(File) <> "some filename you dont want to check" Then
    'do something with the others
    End if


    Casey.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Location
    Nowhere
    Posts
    427

    Re: [2008] Problems with For Statement

    Thanks I used the replace method which did work after all lol.
    "Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz


    Code Snippets/Usefull Links:
    WinRAR DLL|Vista Style Form|Krypton Component Library|Windows Form Aero|Parsing XML files|Calculate File's CRC 32|Download a list of files.

  12. #12
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: [RESOLVED] [2008] Problems with For Statement

    Can i ask why you used Replace ?, can we see your code now ?

    Casey.

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Location
    Nowhere
    Posts
    427

    Re: [RESOLVED] [2008] Problems with For Statement

    Sure
    vb Code:
    1. Option Strict On
    2. Option Explicit On
    3. Option Infer Off
    4. Imports ConsoleApplication1.Crc_Class
    5. Imports System.IO
    6.  
    7. Module Module1
    8.     Private password As String = "a18GB8eI0a"
    9.     Dim PatchFile As FileStream = New FileStream("C:\Users\Home\Desktop\Patch.xml", FileMode.Create, FileAccess.Write)
    10.     Dim Writer As New StreamWriter(PatchFile)
    11.     Dim Crc_Class As New CRC32
    12.     Dim Raw_Crc_Integer As Integer
    13.     Dim looped As Integer = 0
    14.     Dim folderlooped As Integer = 0
    15.     Sub Main()
    16.         Console.Title = ""
    17.         Try
    18. pw:
    19.             Console.WriteLine("Please enter password: ")
    20.             If Not Console.ReadLine = password Then
    21.                 Console.WriteLine("Incorrect password.")
    22.                 GoTo pw
    23.             Else
    24.                           Console.Clear()
    25.             End If
    26. input:
    27.             Console.WriteLine("Input directory...")
    28.             Dim workdir As String = Console.ReadLine
    29.             If Not System.IO.Directory.Exists(workdir) Then
    30.                 Console.WriteLine("Incorrect input: Directory doesnt exist.")
    31.                 GoTo input
    32.             End If
    33.             Writer.Write("<?xml version=" & ControlChars.Quote & "1.0" & ControlChars.Quote & "?>" & vbCrLf & _
    34.                         "<XML>" & vbCrLf & "<PATCHINFO>" & vbCrLf)
    35.             '========================================================================
    36.             Dim foundfolders As String() = IO.Directory.GetDirectories(workdir)
    37.             Dim amountoffoundfolders As Integer = IO.Directory.GetDirectories(workdir).Count
    38.             For Each File As String In IO.Directory.GetFiles(workdir, "*", IO.SearchOption.AllDirectories)
    39.                 Dim fileinfo As New IO.FileInfo(File)
    40.                 Dim File_stream As New IO.FileStream(File, FileMode.Open, FileAccess.Read, FileShare.Read, 8192I)
    41.                 Raw_Crc_Integer = Crc_Class.GetCrc32(File_stream)
    42.                 Writer.WriteLine("  <PATCHNODE file=" & ControlChars.Quote & Path.GetFileName(File) & ControlChars.Quote & ">" & vbCrLf & _
    43.                                  "      <CRC>" & Raw_Crc_Integer.ToString & "</CRC>" & vbCrLf & _
    44.                                  "      <SERVERLINK>" & (IO.Path.GetDirectoryName(File).Replace(workdir, String.Empty)) & "\" & IO.Path.GetFileName(File) & "</SERVERLINK>" & vbCrLf & "    </PATCHNODE>")
    45.  
    46.                 folderlooped += 1
    47.                 Console.WriteLine("Writing File: (" & folderlooped.ToString & ")")
    48.             Next
    49.             '========================================================================
    50.             Writer.WriteLine("</PATCHINFO>" & vbCrLf & "</XML>")
    51.             Writer.Close()
    52.         Catch ex As Exception
    53.             Console.WriteLine(ex.Message & vbCrLf & "Press any button to continue...")
    54.             Console.ReadLine()
    55.             GoTo input
    56.         End Try
    57.     End Sub
    58.  
    59. End Module

    This is what i used.
    Code:
    (IO.Path.GetDirectoryName(File).Replace(workdir, String.Empty)) & "\" & IO.Path.GetFileName(File)
    "Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz


    Code Snippets/Usefull Links:
    WinRAR DLL|Vista Style Form|Krypton Component Library|Windows Form Aero|Parsing XML files|Calculate File's CRC 32|Download a list of files.

  14. #14
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: [RESOLVED] [2008] Problems with For Statement

    Right, i see what you have done. This is what i meant

    File.Substring(File.IndexOf("\") + 1)


    Casey.

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Location
    Nowhere
    Posts
    427

    Re: [RESOLVED] [2008] Problems with For Statement

    Ill keep that in mind next time, The reason i used .replace was because the root directory never changes so i just remove the root directory from the string and replace it with string.empty . I tend to figure this stuff out when im not tired or hungry lol. Thanks for your help it was greatly appreciated
    "Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz


    Code Snippets/Usefull Links:
    WinRAR DLL|Vista Style Form|Krypton Component Library|Windows Form Aero|Parsing XML files|Calculate File's CRC 32|Download a list of files.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width