While Loop with a StreamReader process included for fun

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?V2lsbGlhbSBGb3N0ZXI=?=

    #1

    While Loop with a StreamReader process included for fun

    Good evening all,

    I am trying to write a process that uses a while loop to cycle multiple
    files from an array throught the StreamReader Process.

    The whole thing works using:

    Dim Import_File_Rea der As System.IO.Strea mReader

    While ...
    Import_File_Rea der = New StreamReader(Im port_File_and_P ath)
    .... Extract all the data I want then start again ....
    End While

    However, when I get to the second run through it always return an error
    saying that there was an illegal character included in the
    Import_File_and _Path variable; on checking there are no illegal characters.
    All I can think is that without closing off the Import_File_Rea der variable
    before running through the While loop for a second time it will not work,
    however, when I add in Import_File_Rea der.Close towards the bottom of the
    While Loop it still doesn't work [The same error is returned].

    I am a bit lost know, if anyone has any ideas on how to get this to work it
    would be greatly appreciated if you could respond to this query as I am left
    with the option of opening every file seperately through different variables
    if this doesn't work; and with over 700 files to be opened I am not liking
    that option.

    Yours sincerely,

    William Foster
  • =?Utf-8?B?TWFuaXNoIEJhZm5h?=

    #2
    RE: While Loop with a StreamReader process included for fun

    Hi,
    Try below code which will help you atleast know exact problem:
    // show error if user specified invalid file
    if ( fileName == "" || fileName == null )
    MessageBox.Show ( "Invalid File Name", "Error",
    MessageBoxButto ns.OK, MessageBoxIcon. Error );
    else
    {
    // create FileStream to obtain read access to file
    input = new FileStream( fileName, FileMode.Open,
    FileAccess.Read );

    // set file from where data is read
    fileReader = new StreamReader( input );
    }
    As We pass constant FileMode.Open as the second argument to the FileStream
    constructor to indicate that the FileStream should open the file if it exists
    or should throw a FileNotFoundExc eption if the file does not exist



    --
    Hope this helps.
    Thanks and Regards.
    Manish Bafna.
    MCP and MCTS.



    "William Foster" wrote:
    Good evening all,
    >
    I am trying to write a process that uses a while loop to cycle multiple
    files from an array throught the StreamReader Process.
    >
    The whole thing works using:
    >
    Dim Import_File_Rea der As System.IO.Strea mReader
    >
    While ...
    Import_File_Rea der = New StreamReader(Im port_File_and_P ath)
    .... Extract all the data I want then start again ....
    End While
    >
    However, when I get to the second run through it always return an error
    saying that there was an illegal character included in the
    Import_File_and _Path variable; on checking there are no illegal characters.
    All I can think is that without closing off the Import_File_Rea der variable
    before running through the While loop for a second time it will not work,
    however, when I add in Import_File_Rea der.Close towards the bottom of the
    While Loop it still doesn't work [The same error is returned].
    >
    I am a bit lost know, if anyone has any ideas on how to get this to work it
    would be greatly appreciated if you could respond to this query as I am left
    with the option of opening every file seperately through different variables
    if this doesn't work; and with over 700 files to be opened I am not liking
    that option.
    >
    Yours sincerely,
    >
    William Foster

    Comment

    • =?Utf-8?B?V2lsbGlhbSBGb3N0ZXI=?=

      #3
      RE: While Loop with a StreamReader process included for fun

      Manish,

      Thanks for the help, however, I have already confirmed the file name are
      correct and don't contain any errors. I have used the internal function to
      automatically load an array with files within a directory that meet a certain
      criteria.

      I think the error being returned has nothing to do with the actual error.

      If you have any further thoughts let me know.

      Yours sincerely,

      William Foster


      "Manish Bafna" wrote:
      Hi,
      Try below code which will help you atleast know exact problem:
      // show error if user specified invalid file
      if ( fileName == "" || fileName == null )
      MessageBox.Show ( "Invalid File Name", "Error",
      MessageBoxButto ns.OK, MessageBoxIcon. Error );
      else
      {
      // create FileStream to obtain read access to file
      input = new FileStream( fileName, FileMode.Open,
      FileAccess.Read );
      >
      // set file from where data is read
      fileReader = new StreamReader( input );
      }
      As We pass constant FileMode.Open as the second argument to the FileStream
      constructor to indicate that the FileStream should open the file if it exists
      or should throw a FileNotFoundExc eption if the file does not exist
      >
      >
      >
      --
      Hope this helps.
      Thanks and Regards.
      Manish Bafna.
      MCP and MCTS.
      >
      >
      >
      "William Foster" wrote:
      >
      Good evening all,

      I am trying to write a process that uses a while loop to cycle multiple
      files from an array throught the StreamReader Process.

      The whole thing works using:

      Dim Import_File_Rea der As System.IO.Strea mReader

      While ...
      Import_File_Rea der = New StreamReader(Im port_File_and_P ath)
      .... Extract all the data I want then start again ....
      End While

      However, when I get to the second run through it always return an error
      saying that there was an illegal character included in the
      Import_File_and _Path variable; on checking there are no illegal characters.
      All I can think is that without closing off the Import_File_Rea der variable
      before running through the While loop for a second time it will not work,
      however, when I add in Import_File_Rea der.Close towards the bottom of the
      While Loop it still doesn't work [The same error is returned].

      I am a bit lost know, if anyone has any ideas on how to get this to work it
      would be greatly appreciated if you could respond to this query as I am left
      with the option of opening every file seperately through different variables
      if this doesn't work; and with over 700 files to be opened I am not liking
      that option.

      Yours sincerely,

      William Foster

      Comment

      • =?Utf-8?B?TW9oYW1hZCBFbGFyYWJp?=

        #4
        RE: While Loop with a StreamReader process included for fun

        You are currently dimming a single streamreader, not initializing it, and
        then initializing it over and over in your loop.

        There might be a better way of doing this and it actually confirms that the
        object is destroyed. That is by using the "Using" clause. The "Using" clause
        makes sure the object is completely destroyed outside the Using range.

        Example:

        While . . .
        'Insert code to populate sFilePath
        Using Import_File_Rea der as New System.IO.Strea mReader(sFilePa th)
        ... Extract data from file. etc
        'No need to destroy the file reader it is automatically destroyed by
        the engine.
        End Using
        End While

        Let me know if that helps. Good luck.

        --
        Mohamad Elarabi
        Lead Developer. MCTS, MCPD.


        "William Foster" wrote:
        Good evening all,
        >
        I am trying to write a process that uses a while loop to cycle multiple
        files from an array throught the StreamReader Process.
        >
        The whole thing works using:
        >
        Dim Import_File_Rea der As System.IO.Strea mReader
        >
        While ...
        Import_File_Rea der = New StreamReader(Im port_File_and_P ath)
        .... Extract all the data I want then start again ....
        End While
        >
        However, when I get to the second run through it always return an error
        saying that there was an illegal character included in the
        Import_File_and _Path variable; on checking there are no illegal characters.
        All I can think is that without closing off the Import_File_Rea der variable
        before running through the While loop for a second time it will not work,
        however, when I add in Import_File_Rea der.Close towards the bottom of the
        While Loop it still doesn't work [The same error is returned].
        >
        I am a bit lost know, if anyone has any ideas on how to get this to work it
        would be greatly appreciated if you could respond to this query as I am left
        with the option of opening every file seperately through different variables
        if this doesn't work; and with over 700 files to be opened I am not liking
        that option.
        >
        Yours sincerely,
        >
        William Foster

        Comment

        • =?Utf-8?B?V2lsbGlhbSBGb3N0ZXI=?=

          #5
          RE: While Loop with a StreamReader process included for fun

          Mohamad,

          Thanks for your assistance, on the good side your recommendation has
          certainly improved my coding. On the bad side it did not resolve my error;
          on further investigation [painful investigation] I found that I had misplaced
          a vbCr within a seperate process which was coming into causing the error.

          Thanks for your help!

          William Foster

          "Mohamad Elarabi" wrote:
          You are currently dimming a single streamreader, not initializing it, and
          then initializing it over and over in your loop.
          >
          There might be a better way of doing this and it actually confirms that the
          object is destroyed. That is by using the "Using" clause. The "Using" clause
          makes sure the object is completely destroyed outside the Using range.
          >
          Example:
          >
          While . . .
          'Insert code to populate sFilePath
          Using Import_File_Rea der as New System.IO.Strea mReader(sFilePa th)
          ... Extract data from file. etc
          'No need to destroy the file reader it is automatically destroyed by
          the engine.
          End Using
          End While
          >
          Let me know if that helps. Good luck.
          >
          --
          Mohamad Elarabi
          Lead Developer. MCTS, MCPD.
          >
          >
          "William Foster" wrote:
          >
          Good evening all,

          I am trying to write a process that uses a while loop to cycle multiple
          files from an array throught the StreamReader Process.

          The whole thing works using:

          Dim Import_File_Rea der As System.IO.Strea mReader

          While ...
          Import_File_Rea der = New StreamReader(Im port_File_and_P ath)
          .... Extract all the data I want then start again ....
          End While

          However, when I get to the second run through it always return an error
          saying that there was an illegal character included in the
          Import_File_and _Path variable; on checking there are no illegal characters.
          All I can think is that without closing off the Import_File_Rea der variable
          before running through the While loop for a second time it will not work,
          however, when I add in Import_File_Rea der.Close towards the bottom of the
          While Loop it still doesn't work [The same error is returned].

          I am a bit lost know, if anyone has any ideas on how to get this to work it
          would be greatly appreciated if you could respond to this query as I am left
          with the option of opening every file seperately through different variables
          if this doesn't work; and with over 700 files to be opened I am not liking
          that option.

          Yours sincerely,

          William Foster

          Comment

          • =?Utf-8?B?TW9oYW1hZCBFbGFyYWJp?=

            #6
            RE: While Loop with a StreamReader process included for fun

            William, happy to help and happier you found the problem. :) Debugging
            something like this might feel like shooting in the dark :) I was mainly
            trying to address your concern that the object isn't clearing. But I'm glad
            that the error message was more relevant to the problem than just the object
            not clearing.
            Thanks,

            --
            Mohamad Elarabi
            Lead Developer. MCTS, MCPD.


            "William Foster" wrote:
            Mohamad,
            >
            Thanks for your assistance, on the good side your recommendation has
            certainly improved my coding. On the bad side it did not resolve my error;
            on further investigation [painful investigation] I found that I had misplaced
            a vbCr within a seperate process which was coming into causing the error.
            >
            Thanks for your help!
            >
            William Foster
            >
            "Mohamad Elarabi" wrote:
            >
            You are currently dimming a single streamreader, not initializing it, and
            then initializing it over and over in your loop.

            There might be a better way of doing this and it actually confirms that the
            object is destroyed. That is by using the "Using" clause. The "Using" clause
            makes sure the object is completely destroyed outside the Using range.

            Example:

            While . . .
            'Insert code to populate sFilePath
            Using Import_File_Rea der as New System.IO.Strea mReader(sFilePa th)
            ... Extract data from file. etc
            'No need to destroy the file reader it is automatically destroyed by
            the engine.
            End Using
            End While

            Let me know if that helps. Good luck.

            --
            Mohamad Elarabi
            Lead Developer. MCTS, MCPD.


            "William Foster" wrote:
            Good evening all,
            >
            I am trying to write a process that uses a while loop to cycle multiple
            files from an array throught the StreamReader Process.
            >
            The whole thing works using:
            >
            Dim Import_File_Rea der As System.IO.Strea mReader
            >
            While ...
            Import_File_Rea der = New StreamReader(Im port_File_and_P ath)
            .... Extract all the data I want then start again ....
            End While
            >
            However, when I get to the second run through it always return an error
            saying that there was an illegal character included in the
            Import_File_and _Path variable; on checking there are no illegal characters.
            All I can think is that without closing off the Import_File_Rea der variable
            before running through the While loop for a second time it will not work,
            however, when I add in Import_File_Rea der.Close towards the bottom of the
            While Loop it still doesn't work [The same error is returned].
            >
            I am a bit lost know, if anyone has any ideas on how to get this to work it
            would be greatly appreciated if you could respond to this query as I am left
            with the option of opening every file seperately through different variables
            if this doesn't work; and with over 700 files to be opened I am not liking
            that option.
            >
            Yours sincerely,
            >
            William Foster

            Comment

            Working...