title: Input string was not in a correct format -- foreach loop
body: <pre><code>Public Sub Main()
  
      Try
          ' Last load date
          dLastLoad = CType(Dts.Variables("User::PAR_LastLoadDate").Value, DateTime)
  
          ' Files to load
          dtFilesToLoad = CType(Dts.Variables("User::PAR_FilesToLoad").Value, DataTable)
  
          ' Parse file date out of file name
          strFileName = Dts.Variables("User::PAR_FileName").Value.ToString
  
          Dim temp As String
          temp = strFileName.Substring(strFileName.LastIndexOf("\") + 1, (strFileName.Length - (strFileName.LastIndexOf("\") + 1)))
  
          If temp.Length = Dts.Variables("$Package::PAR_SF_FileName").Value.ToString.Length Then
  
              strFileDate = strFileName.Substring(strFileName.Length - 12, 8)
  
              dFile = New DateTime( _
                  Int32.Parse(strFileDate.Substring(0, 4)), _
                  Int32.Parse(strFileDate.Substring(4, 2)), _
                  Int32.Parse(strFileDate.Substring(6, 2)) _
              )
  
              dtMaxDate = DateTime.Parse(Dts.Variables("$Package::PAR_SF_MaxDate").Value.ToString, CultureInfo.InvariantCulture)
  
             ' Add row to data table when file date is more recent than last load date
              If dFile &gt; dLastLoad And dFile &lt;= dtMaxDate Then
                  dtFilesToLoad.Rows.Add(New Object() {dFile, strFileName})
              End If
  
              Dts.Variables("User::PAR_FilesToLoad").Value = dtFilesToLoad
  
          End If
  
          Dts.TaskResult = ScriptResults.Success
  
      Catch ex As Exception
  
          Dts.Events.FireError(Nothing, String.Empty, ex.Message.ToString, String.Empty, Nothing)
          Dts.TaskResult = ScriptResults.Failure
  
      End Try
  End Sub
  </code></pre>
  
  <p>I'm looping over files with extension file_20140101_0100.fil (YYYYMMDD_hhmm).
  The other variables <code>lastLoaddate</code> are also datetime type.</p>
  
  <p>I'm getting the error </p>
  
  <blockquote>
    <p>When converting a string to datetime, parse the string to take the date before putting each variable into the datetime object.</p>
  </blockquote>