I have a list of files that need to be copied to a new directory. All information is in the file name. Each source file name consists of [installation number] [new file name]. For example:
235623bob.txt
Here, the installation number is 235623, and the new file name is bob.txt. The installation number is from 1 to 11 digits, and the new file name never starts with a digit. However, it may begin with any other legal character and may contain numbers after the first character. For example:
3245_6786bil54.txt
- 3245 as the installation number and _6786bil54.txt as the new file name. I tried to do the following:
private void BtnGo_Click(object sender, EventArgs e) { string inst_no = ""; // installation number string dest_filename = ""; // destination filename string dest_directory = ""; string[] source_files = Directory.GetFiles(TxtSource.Text); // copy them to their new destination foreach (string file in source_files) { // source filename contains the instno and dest_filename. Match match = Regex.Match(file, @"(\d+)(\w+)"); inst_no = match.Groups[0].Value; dest_filename = match.Groups[1].Value; dest_directory = TxtDestination.Text + "\\" + inst_no; if (!Directory.Exists(dest_directory)) Directory.CreateDirectory(dest_directory); File.Copy(file, dest_directory + "\\" + dest_filename); } }
Here is the problem:
inst_no = match.Groups[0].Value; dest_filename = match.Groups[1].Value;
1253hans.txt should become
inst_no=1253 dest_filename=hans
But this
inst_no=1253hans filename=1253
What did I misunderstand in group matches?
source share