Skip to content

Instantly share code, notes, and snippets.

@Joeyy
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Joeyy/8918941 to your computer and use it in GitHub Desktop.
Save Joeyy/8918941 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace UniqueSQFIDs
{
public partial class UniqueSQFIDs : Form
{
public UniqueSQFIDs()
{
InitializeComponent();
}
int startID;
OpenFileDialog fdlg = new OpenFileDialog();
System.IO.StreamReader sR;
System.IO.StreamWriter sW;
Regex reg = new Regex(@"_vehicle_\d*");
private void btnGo_Click(object sender, EventArgs e)
{
try
{
startID = Convert.ToInt32(txtStartID.Text);
fdlg.Title = "C# Corner Open File Dialog";
fdlg.InitialDirectory = @"c:\";
fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
if (fdlg.ShowDialog() == DialogResult.OK)
{
sR = new System.IO.StreamReader(fdlg.FileName);
using (sR)
{
using (sW = File.AppendText(fdlg.FileName.Replace(fdlg.SafeFileName, "SQFUniqueID" + Path.GetExtension(fdlg.FileName).ToString())))
{
string line;
int line2 = 0;
while ((line = sR.ReadLine()) != null)
{
Match regM = reg.Match(line);
if (regM.Success)
{
line2++;
line = reg.Replace(line, "_vehicle_" + startID.ToString());
sW.WriteLine(line);
if (IsOdd(startID))
{
if (!IsOdd(line2))
{
startID++;
}
}
else
{
if (!IsOdd(line2))
{
startID++;
}
}
}
else
{
sW.WriteLine(line);
}
}
}
}
}
}
catch
{
MessageBox.Show("Couldn't convert ID to INT");
}
}
public static bool IsOdd(int value)
{
return value % 2 != 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment