Erwin Alcantara
Erwin Alcantara

Reputation: 45

What is the code for renaming non-space filename

I have a filename of document erwin_01problem.doc, What i want here is the if a filename does not contain a space between 01problem (erwin_01problem.doc). I find the index of problem and replace it with " ". The output will be erwin_01 problem.doc Here is the code that i try but still I wasn't able to put a space between 01 and problem.

if (!string.IsNullOrEmpty(job.ProblemPath))
            {
                job.HasProblemFile = true;
                var problemDocFname = Path.GetFileName(job.ProblemPath);
                if (!Regex.IsMatch(problemDocFname, @"\sproblem\.doc$"))
                {
                    ProgM.JobStatus = "Checking space between filename and problem...";
                    Thread.Sleep(1000);
                    problemDocFname = problemDocFname.Insert(problemDocFname.IndexOf("problem.doc", StringComparison.Ordinal), " ");
                    //problemDocFname = problemDocFname.Replace("problem", " problem");
                }
                problemDocFname = Path.Combine(job.FilePath, problemDocFname);
                var docProblemCount = 0;
                ProgM.JobStatus = "Correcting the Format of Problem Doc...";
                Thread.Sleep(1000);
                MicrosoftWord.CorrectProblemDocFormatting(problemDocFname, ref docProblemCount);
            }
            jobs.Add(job);

Upvotes: 0

Views: 33

Answers (1)

CCamilo
CCamilo

Reputation: 887

I belive you don't really need regular expressions. You can just do something like:

string key = "problem.doc";
if (problemDocFname.EndsWith(key) && problemDocFname.Length > key.Length)
{
      problemDocFname.Replace(key, " problem.doc");
}

Upvotes: 1

Related Questions