Advertisement
Ikillnukes

frmMain Button3 Click

Aug 3rd, 2013
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 21.75 KB | None | 0 0
  1.     Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
  2.         Try
  3.             TextBox3.AppendText("Comenzando a leer y filtrar resultados del archivo...")
  4.             Dim Texto As String = File.ReadAllText(TextBox1.Text)
  5.             Dim Match As Match = Regex.Match(Texto, "(?<prueba>(?<=[\(]).*(?=[\)]))")
  6.             Dim Match2 As Match = Regex.Match(Texto, "(?<prueba2>.(.*(?=[\(])))")
  7.             'Do While Match.Success
  8.             '    MsgBox(Match.Groups(1).ToString)
  9.             '    Match = Match.NextMatch()
  10.             'Loop
  11.  
  12.             Dim strStreamWriter As StreamWriter = Nothing
  13.             Dim strStreamW As Stream = Nothing
  14.             Dim PathArchivo As String = ".\Variables.sql"
  15.             Dim PathArchivo2 As String = ".\INSERTS.sql"
  16.             Dim PathArchivo3 As String = ".\preVariables.sql"
  17.             Dim PathArchivo4 As String = ".\Variables-new.sql"
  18.  
  19.             Dim PathCarpeta As String = Replace(TextBox1.Text, PathArchivo.Substring(PathArchivo.LastIndexOf("/") + 1), "")
  20.  
  21.             'If Not Directory.Exists(PathCarpeta) Then
  22.             '    Directory.CreateDirectory(PathCarpeta)
  23.             'End If
  24.  
  25.             TextBox3.AppendText(Environment.NewLine & "Separando archivo...")
  26.  
  27.             Do While Match.Success
  28.  
  29.                 Try
  30.  
  31.                     If File.Exists(PathArchivo) Then
  32.                         strStreamW = File.Open(PathArchivo, FileMode.Append)
  33.                     Else
  34.                         strStreamW = File.Create(PathArchivo)
  35.                     End If
  36.  
  37.                     strStreamWriter = New StreamWriter(strStreamW, System.Text.Encoding.Default)
  38.  
  39.                     strStreamWriter.Write(Match.Groups("prueba").ToString & Environment.NewLine)
  40.                     Match = Match.NextMatch()
  41.  
  42.                     strStreamWriter.Close()
  43.  
  44.                     strStreamW = Nothing
  45.                     strStreamWriter = Nothing
  46.  
  47.                     If File.Exists(PathArchivo2) Then
  48.                         strStreamW = File.Open(PathArchivo2, FileMode.Append)
  49.                     Else
  50.                         strStreamW = File.Create(PathArchivo2)
  51.                     End If
  52.  
  53.                     strStreamWriter = New StreamWriter(strStreamW, System.Text.Encoding.Default)
  54.  
  55.                     strStreamWriter.Write(Match2.Groups("prueba2").ToString & Environment.NewLine)
  56.                     Match2 = Match2.NextMatch()
  57.  
  58.                     strStreamWriter.Close()
  59.  
  60.                     strStreamW = Nothing
  61.                     strStreamWriter = Nothing
  62.  
  63.                 Catch ex As Exception
  64.                     TextBox3.AppendText(Environment.NewLine & "Error al Guardar la información en el archivo. ")
  65.                     strStreamWriter.Close()
  66.                 End Try
  67.             Loop
  68.  
  69.             Dim lines As String() = File.ReadAllLines(TextBox1.Text)
  70.  
  71.             Dim MaxCommas As Integer = CountCharacter(lines(0), ",") + 1
  72.  
  73.             Dim TimesSplitted As Integer = 0
  74.  
  75.             TextBox3.AppendText(Environment.NewLine & "Leyendo todas las líneas del archivo...")
  76.  
  77.             Dim Line As String = File.ReadAllText(PathArchivo)
  78.  
  79.             'Dim Result As String() = Line.Split(", ")
  80.  
  81.             TextBox3.AppendText(Environment.NewLine & "Corrigiendo errores del Strings...")
  82.  
  83.             Dim RegReplace As String = Regex.Replace(Line, "\n", ",")
  84.  
  85.             For Each Result As String In Regex.Split(RegReplace, ",")
  86.  
  87.                 Select Case True
  88.  
  89.                     Case Result.StartsWith("'") AndAlso Result.EndsWith("'")
  90.                         Dim posResultado = Result
  91.                         Dim preResultado = String.Format("'{0}'", Result.Substring(1, Result.Length - 2).Replace("'", "\'"))
  92.                         Try
  93.  
  94.                             If File.Exists(PathArchivo3) Then
  95.                                 strStreamW = File.Open(PathArchivo3, FileMode.Append)
  96.                             Else
  97.                                 strStreamW = File.Create(PathArchivo3)
  98.                             End If
  99.  
  100.                             strStreamWriter = New StreamWriter(strStreamW, System.Text.Encoding.Default)
  101.  
  102.                             Dim Splittado As String = Nothing
  103.  
  104.  
  105.                             TimesSplitted += 1
  106.  
  107.                             If TimesSplitted = MaxCommas Then
  108.                                 Splittado = Environment.NewLine
  109.                                 TimesSplitted = 0
  110.                             ElseIf TimesSplitted < MaxCommas Then
  111.                                 Splittado = ","
  112.                             End If
  113.  
  114.                             strStreamWriter.Write(preResultado & Splittado)
  115.  
  116.                             strStreamWriter.Close()
  117.  
  118.                             strStreamW = Nothing
  119.                             strStreamWriter = Nothing
  120.  
  121.                         Catch ex As Exception
  122.                             TextBox3.AppendText(Environment.NewLine & "Error al Guardar la información en el archivo. " & ex.ToString)
  123.                             strStreamWriter.Close()
  124.                         End Try
  125.                         If Not preResultado = posResultado Then
  126.                             TextBox3.AppendText(Environment.NewLine & "Msg: #1 (String Cambiada)")
  127.                         End If
  128.  
  129.                     Case Result.StartsWith("'") AndAlso Not Result.EndsWith("'")
  130.                         Dim posResultado = Result
  131.                         Dim preResultado As String = String.Format("'{0}", Result.Substring(1, Result.Length - 1).Replace("'", "\'"))
  132.                         Try
  133.  
  134.                             If File.Exists(PathArchivo3) Then
  135.                                 strStreamW = File.Open(PathArchivo3, FileMode.Append)
  136.                             Else
  137.                                 strStreamW = File.Create(PathArchivo3)
  138.                             End If
  139.  
  140.                             strStreamWriter = New StreamWriter(strStreamW, System.Text.Encoding.Default)
  141.  
  142.                             Dim Splittado As String = Nothing
  143.  
  144.  
  145.                             TimesSplitted += 1
  146.  
  147.                             If TimesSplitted = MaxCommas Then
  148.                                 Splittado = Environment.NewLine
  149.                                 TimesSplitted = 0
  150.                             ElseIf TimesSplitted < MaxCommas Then
  151.                                 Splittado = ","
  152.                             End If
  153.  
  154.                             strStreamWriter.Write(preResultado & "'" & Splittado)
  155.  
  156.                             strStreamWriter.Close()
  157.  
  158.                             strStreamW = Nothing
  159.                             strStreamWriter = Nothing
  160.  
  161.                         Catch ex As Exception
  162.                             TextBox3.AppendText(Environment.NewLine & "Error al Guardar la información en el archivo. " & ex.ToString)
  163.                             strStreamWriter.Close()
  164.                         End Try
  165.                         TextBox3.AppendText(Environment.NewLine & "Msg: #2 (String arreglada; Error #1: [Faltaba la última comilla])")
  166.                         If Not preResultado = posResultado Then
  167.                             TextBox3.AppendText(Environment.NewLine & "Msg: #1 (String Cambiada)")
  168.                         End If
  169.  
  170.                     Case Not Result.StartsWith("'") AndAlso Result.EndsWith("'")
  171.                         Dim posResultado = Result
  172.                         Dim preResultado As String = Result.Replace("'", "\'")
  173.                         Try
  174.  
  175.                             If File.Exists(PathArchivo3) Then
  176.                                 strStreamW = File.Open(PathArchivo3, FileMode.Append)
  177.                             Else
  178.                                 strStreamW = File.Create(PathArchivo3)
  179.                             End If
  180.  
  181.                             strStreamWriter = New StreamWriter(strStreamW, System.Text.Encoding.Default)
  182.  
  183.                             Dim Splittado As String = Nothing
  184.  
  185.  
  186.                             TimesSplitted += 1
  187.  
  188.                             If TimesSplitted = MaxCommas Then
  189.                                 Splittado = Environment.NewLine
  190.                                 TimesSplitted = 0
  191.                             ElseIf TimesSplitted < MaxCommas Then
  192.                                 Splittado = ","
  193.                             End If
  194.  
  195.                             strStreamWriter.Write("'" & preResultado & Splittado)
  196.  
  197.                             strStreamWriter.Close()
  198.  
  199.                             strStreamW = Nothing
  200.                             strStreamWriter = Nothing
  201.  
  202.                         Catch ex As Exception
  203.                             TextBox3.AppendText(Environment.NewLine & "Error al Guardar la información en el archivo. " & ex.ToString)
  204.                             strStreamWriter.Close()
  205.                         End Try
  206.                         TextBox3.AppendText(Environment.NewLine & "Msg: #2 (String arreglada; Error: #2: [Faltaba la primera comilla])")
  207.                         If Not preResultado = posResultado Then
  208.                             TextBox3.AppendText(Environment.NewLine & "Msg: #1 (String Cambiada)")
  209.                         End If
  210.  
  211.                     Case Not Result.StartsWith("'") AndAlso Not Result.EndsWith("'") AndAlso Result.Contains("'")
  212.                         Dim posResultado = Result
  213.                         Dim preResultado As String = String.Format("{0}", Result.Replace("'", "\'"))
  214.                         Try
  215.  
  216.                             If File.Exists(PathArchivo3) Then
  217.                                 strStreamW = File.Open(PathArchivo3, FileMode.Append)
  218.                             Else
  219.                                 strStreamW = File.Create(PathArchivo3)
  220.                             End If
  221.  
  222.                             strStreamWriter = New StreamWriter(strStreamW, System.Text.Encoding.Default)
  223.  
  224.                             Dim Splittado As String = Nothing
  225.  
  226.  
  227.                             TimesSplitted += 1
  228.  
  229.                             If TimesSplitted = MaxCommas Then
  230.                                 Splittado = Environment.NewLine
  231.                                 TimesSplitted = 0
  232.                             ElseIf TimesSplitted < MaxCommas Then
  233.                                 Splittado = ","
  234.                             End If
  235.  
  236.                             strStreamWriter.Write("'" & preResultado & "'" & Splittado)
  237.  
  238.                             strStreamWriter.Close()
  239.  
  240.                             strStreamW = Nothing
  241.                             strStreamWriter = Nothing
  242.  
  243.                         Catch ex As Exception
  244.                             TextBox3.AppendText(Environment.NewLine & "Error al Guardar la información en el archivo. " & ex.ToString)
  245.                             strStreamWriter.Close()
  246.                         End Try
  247.                         TextBox3.AppendText(Environment.NewLine & "Msg: #2 (String arreglada; Error: #3: [Faltaban las comillas de extremos])")
  248.                         If Not preResultado = posResultado Then
  249.                             TextBox3.AppendText(Environment.NewLine & "Msg: #1 (String Cambiada)")
  250.                         End If
  251.  
  252.                         '-----------------------
  253.  
  254.                     Case Result.StartsWith("""") AndAlso Result.EndsWith("""")
  255.                         Dim posResultado = Result
  256.                         Dim preResultado = String.Format("""{0}""", Result.Substring(1, Result.Length - 2).Replace("""", "\"""))
  257.                         Try
  258.  
  259.                             If File.Exists(PathArchivo3) Then
  260.                                 strStreamW = File.Open(PathArchivo3, FileMode.Append)
  261.                             Else
  262.                                 strStreamW = File.Create(PathArchivo3)
  263.                             End If
  264.  
  265.                             strStreamWriter = New StreamWriter(strStreamW, System.Text.Encoding.Default)
  266.  
  267.                             Dim Splittado As String = Nothing
  268.  
  269.  
  270.                             TimesSplitted += 1
  271.  
  272.                             If TimesSplitted = MaxCommas Then
  273.                                 Splittado = Environment.NewLine
  274.                                 TimesSplitted = 0
  275.                             ElseIf TimesSplitted < MaxCommas Then
  276.                                 Splittado = ","
  277.                             End If
  278.  
  279.                             strStreamWriter.Write(preResultado & Splittado)
  280.  
  281.                             strStreamWriter.Close()
  282.  
  283.                             strStreamW = Nothing
  284.                             strStreamWriter = Nothing
  285.  
  286.                         Catch ex As Exception
  287.                             TextBox3.AppendText(Environment.NewLine & "Error al Guardar la información en el archivo. " & ex.ToString)
  288.                             strStreamWriter.Close()
  289.                         End Try
  290.                         If Not preResultado = posResultado Then
  291.                             TextBox3.AppendText(Environment.NewLine & "Msg: #1 (String Cambiada)")
  292.                         End If
  293.  
  294.                     Case Result.StartsWith("""") AndAlso Not Result.EndsWith("""")
  295.                         Dim posResultado = Result
  296.                         Dim preResultado As String = String.Format("""{0}", Result.Substring(1, Result.Length - 1).Replace("""", "\"""))
  297.                         Try
  298.  
  299.                             If File.Exists(PathArchivo3) Then
  300.                                 strStreamW = File.Open(PathArchivo3, FileMode.Append)
  301.                             Else
  302.                                 strStreamW = File.Create(PathArchivo3)
  303.                             End If
  304.  
  305.                             strStreamWriter = New StreamWriter(strStreamW, System.Text.Encoding.Default)
  306.  
  307.                             Dim Splittado As String = Nothing
  308.  
  309.  
  310.                             TimesSplitted += 1
  311.  
  312.                             If TimesSplitted = MaxCommas Then
  313.                                 Splittado = Environment.NewLine
  314.                                 TimesSplitted = 0
  315.                             ElseIf TimesSplitted < MaxCommas Then
  316.                                 Splittado = ","
  317.                             End If
  318.  
  319.                             strStreamWriter.Write(preResultado & """" & Splittado)
  320.  
  321.                             strStreamWriter.Close()
  322.  
  323.                             strStreamW = Nothing
  324.                             strStreamWriter = Nothing
  325.  
  326.                         Catch ex As Exception
  327.                             TextBox3.AppendText(Environment.NewLine & "Error al Guardar la información en el archivo. " & ex.ToString)
  328.                             strStreamWriter.Close()
  329.                         End Try
  330.                         TextBox3.AppendText(Environment.NewLine & "Msg: #2 (String arreglada; Error #1: [Faltaba la última comilla])")
  331.                         If Not preResultado = posResultado Then
  332.                             TextBox3.AppendText(Environment.NewLine & "Msg: #1 (String Cambiada)")
  333.                         End If
  334.  
  335.                     Case Not Result.StartsWith("""") AndAlso Result.EndsWith("""")
  336.                         Dim posResultado = Result
  337.                         Dim preResultado As String = Result.Replace("""", "\""")
  338.                         Try
  339.  
  340.                             If File.Exists(PathArchivo3) Then
  341.                                 strStreamW = File.Open(PathArchivo3, FileMode.Append)
  342.                             Else
  343.                                 strStreamW = File.Create(PathArchivo3)
  344.                             End If
  345.  
  346.                             strStreamWriter = New StreamWriter(strStreamW, System.Text.Encoding.Default)
  347.  
  348.                             Dim Splittado As String = Nothing
  349.  
  350.  
  351.                             TimesSplitted += 1
  352.  
  353.                             If TimesSplitted = MaxCommas Then
  354.                                 Splittado = Environment.NewLine
  355.                                 TimesSplitted = 0
  356.                             ElseIf TimesSplitted < MaxCommas Then
  357.                                 Splittado = ","
  358.                             End If
  359.  
  360.                             strStreamWriter.Write("""" & preResultado & Splittado)
  361.  
  362.                             strStreamWriter.Close()
  363.  
  364.                             strStreamW = Nothing
  365.                             strStreamWriter = Nothing
  366.  
  367.                         Catch ex As Exception
  368.                             TextBox3.AppendText(Environment.NewLine & "Error al Guardar la información en el archivo. " & ex.ToString)
  369.                             strStreamWriter.Close()
  370.                         End Try
  371.                         TextBox3.AppendText(Environment.NewLine & "Msg: #2 (String arreglada; Error: #2: [Faltaba la primera comilla])")
  372.                         If Not preResultado = posResultado Then
  373.                             TextBox3.AppendText(Environment.NewLine & "Msg: #1 (String Cambiada)")
  374.                         End If
  375.  
  376.                     Case Not Result.StartsWith("""") AndAlso Not Result.EndsWith("""") AndAlso Result.Contains("""")
  377.                         Dim posResultado = Result
  378.                         Dim preResultado As String = String.Format("{0}", Result.Replace("""", "\"""))
  379.                         Try
  380.  
  381.                             If File.Exists(PathArchivo3) Then
  382.                                 strStreamW = File.Open(PathArchivo3, FileMode.Append)
  383.                             Else
  384.                                 strStreamW = File.Create(PathArchivo3)
  385.                             End If
  386.  
  387.                             strStreamWriter = New StreamWriter(strStreamW, System.Text.Encoding.Default)
  388.  
  389.                             Dim Splittado As String = Nothing
  390.  
  391.  
  392.                             TimesSplitted += 1
  393.  
  394.                             If TimesSplitted = MaxCommas Then
  395.                                 Splittado = Environment.NewLine
  396.                                 TimesSplitted = 0
  397.                             ElseIf TimesSplitted < MaxCommas Then
  398.                                 Splittado = ","
  399.                             End If
  400.  
  401.                             strStreamWriter.Write("""" & preResultado & """" & Splittado)
  402.  
  403.                             strStreamWriter.Close()
  404.  
  405.                             strStreamW = Nothing
  406.                             strStreamWriter = Nothing
  407.  
  408.                         Catch ex As Exception
  409.                             TextBox3.AppendText(Environment.NewLine & "Error al Guardar la información en el archivo. " & ex.ToString)
  410.                             strStreamWriter.Close()
  411.                         End Try
  412.                         TextBox3.AppendText(Environment.NewLine & "Msg: #2 (String arreglada; Error: #3: [Faltaban las comillas de extremos])")
  413.                         If Not preResultado = posResultado Then
  414.                             TextBox3.AppendText(Environment.NewLine & "Msg: #1 (String Cambiada)")
  415.                         End If
  416.  
  417.                     Case Else
  418.                         Try
  419.  
  420.                             If File.Exists(PathArchivo3) Then
  421.                                 strStreamW = File.Open(PathArchivo3, FileMode.Append)
  422.                             Else
  423.                                 strStreamW = File.Create(PathArchivo3)
  424.                             End If
  425.  
  426.                             strStreamWriter = New StreamWriter(strStreamW, System.Text.Encoding.Default)
  427.  
  428.                             Dim Splittado As String = Nothing
  429.  
  430.  
  431.                             TimesSplitted += 1
  432.  
  433.                             If TimesSplitted = MaxCommas Then
  434.                                 Splittado = Environment.NewLine
  435.                                 TimesSplitted = 0
  436.                             ElseIf TimesSplitted < MaxCommas Then
  437.                                 Splittado = ","
  438.                             End If
  439.  
  440.                             strStreamWriter.Write(Result & Splittado)
  441.  
  442.                             strStreamWriter.Close()
  443.  
  444.                             strStreamW = Nothing
  445.                             strStreamWriter = Nothing
  446.  
  447.                         Catch ex As Exception
  448.                             TextBox3.AppendText(Environment.NewLine & "Error al Guardar la información en el archivo. " & ex.ToString)
  449.                             strStreamWriter.Close()
  450.                         End Try
  451.  
  452.                 End Select
  453.             Next Result
  454.  
  455.             Dim ContentFile As String = File.ReadAllText(PathArchivo3)
  456.             Dim prefinalText As String = Regex.Replace(ContentFile, ".[']$\n[']", "'")
  457.             'prefinalText = Regex.Replace(ContentFile, "\\""$\n""", """")
  458.             Dim finalText As String = Regex.Replace(prefinalText, "\n", "")
  459.  
  460.             MsgBox(ContentFile)
  461.             MsgBox(prefinalText)
  462.             MsgBox(finalText)
  463.  
  464.             Try
  465.  
  466.  
  467.                 If File.Exists(PathArchivo4) Then
  468.                     strStreamW = File.Open(PathArchivo4, FileMode.Append)
  469.                 Else
  470.                     strStreamW = File.Create(PathArchivo4)
  471.                 End If
  472.  
  473.                 strStreamWriter = New StreamWriter(strStreamW, System.Text.Encoding.Default)
  474.  
  475.                 strStreamWriter.Write(finalText)
  476.  
  477.                 strStreamWriter.Close()
  478.  
  479.                 strStreamW = Nothing
  480.                 strStreamWriter = Nothing
  481.  
  482.             Catch ex As Exception
  483.                 TextBox3.AppendText("Error al Guardar la información en el archivo. " & ex.ToString)
  484.                 strStreamWriter.Close()
  485.             End Try
  486.  
  487.             If MsgBox("¡Proceso terminado! ¿Desea abrir el Log?", MsgBoxStyle.YesNo, "Información") = MsgBoxResult.Ok Then
  488.                 '... Texto para el Log
  489.             End If
  490.  
  491.         Catch ex As Exception When TextBox1.Text = String.Empty
  492.             MsgBox("¡Tienes que seleccionar un archivo!", MsgBoxStyle.Critical, "Error")
  493.         End Try
  494.     End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement