开发者

C# stringbuilder + conversion

What I have going on is this:

1) Reading a directory of files

2) Writing out to a text file the filenames + "|"

3) Where i'm stuck.....

I have a bunch of files named... and need to be converted corispondingly: Apple0154~3.Txt convertedTO -> Apple0156.txt

Apple0136~31.txt convertedTO -> Apple0166.txt

The prefix is always apple so it kinda goes like:

Apple (always the same prefix). The numbers match is # + ~ subnumber -1 always in in .txt

I'm sure this is confusing i'm using this code but i cant figured out how to get this resulting textfile:

Apple0154~3.Txt|Apple0156.txt

Apple0136~31.txt|Apple0166.txt

{
            string resultingfile = ***This is what i dont know***
            string moved开发者_开发知识库redfolder = (overlordfolder + "\\redactions\\");
            DirectoryInfo movedredinfo = new DirectoryInfo(movedredfolder);
            using (StreamWriter output = new StreamWriter(Path.Combine(movedredfolder, "Master.txt")))
            {
                foreach (FileInfo fi in movedredfolder)
                {
                    output.WriteLine(Path.GetFileName(fi)+"|"+resultingfile);
                }
            }
        }


Ok, I see what you are trying to do.

Try using Regular expressions to grab the 2 numbers out of the original file name. Something like:

Regex r = new Regex(@"Apple(\d+)~(\d+)\.txt");
Match mat = r.Match(filename);

if( !mat.Success )
{
    // Something bad happened...
    return;
}

int one = int.Parse(mat.Groups[1].Value);
int two = int.Parse(mat.Groups[2].Value);
int num = one + (two-1);

string newFilename = "Apple"+num.ToString("0000")+".txt";


Inside the foreach loop:

   string fileName = Path.GetFileName(fi);
   string[] parts = fileName.Split('~', '.');
   int basenum = int.Parse(parts[0].Substring(6));
   int offset = int.Parse(parts[1]);
   string resultingfile = string.Format("Apple{0:0000}.txt", basenum+offset-1);


euh something like:

 string path = Path.GetFileName(fi);
 int indexOfTilde = path.IndexOf('~');
 int indexOfPoint = path.LastIndexOf('.');
 int length = indexOfPoint -indexOfTilde; 
 string tmp = path.SubString(indexOfTilde+1, length);
 int numberToIncrease = Convert.ToInt32(tmp) - 1;
 int baseNumber = Convert.ToInt32(path.SubString(5, indexOfTilde-4);
 string newPath = "Apple" + (baseNumber + numberToIncrease ) + ".txt";

and you can use the FileInfo.MoveTo for file movement :) good luck!

edit: damn... too slow typing of me...


Ok, this should work for one file:

String filename = "Apple0154~3.Txt";
Regex re = new Regex(@"Apple(?<num>\d+)\~(?<add>\d+)");

Int32 num = Int32.Parse(re.Match(filename).Groups["num"].Value);
Int32 add = Int32.Parse(re.Match(filename).Groups["add"].Value);

Int32 rez = num + (add - 1);
MessageBox.Show("Apple" + rez + ".txt");


using (var output = new StreamWriter(Path.Combine(movedredfolder, "Master.txt")))
{
    foreach (var filePath in Directory.GetFiles(directoryPath))
    {
        var fileName = Path.GetFileNameWithoutExtension(filePath);
        var fileExtension = Path.GetExtension(filePath);
        var index = fileName.IndexOf('~'); 
        var firstNumber = Int32.Parse(fileName.Substring(5, index - 1)); 
        var secondNumber = Int32.Parse(fileName.Substring(index + 1)) - 1;
        output.Write("Apple0" + 
            (firstNumber + secondNumber).ToString() +
            fileExtension + "|"
        );
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜