Click here to Skip to main content
15,743,541 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Experts,
Problem: after the batch uploading files I need to recognizing and sorting files by names (by mask or flag). For example, file name "Tripolskoe_23_2341_MD_123-XXX.PDF". My utility should recognize the file name and determine this file per flag "MD". What condition for search should be put? What code I can use in my case?

What I have tried:

private void DropSpot_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
			// We only want to accept files, so we only set our DragDropEffects 
			// if that's what's being dragged in
			if (e.Data.GetDataPresent(DataFormats.FileDrop, false)==true)
				e.Effect = DragDropEffects.All;				
		ArrayList Files = new ArrayList();
		private void DropSpot_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
			// Get a list of all objects in the Drop Data, that are files
			string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
			// Iterate through the dropped files
			for (int i=0;i<files.Length;i++)
				// Add the to our ArrayList
				// Create our new List View item
				ListViewItem item = new ListViewItem();
				// Get a file info object
				// we use this for getting file size, etc.
				System.IO.FileInfo fInfo = new System.IO.FileInfo(files[i]);								
				item.Text = System.IO.Path.GetFileName(fInfo.Name);
				FileListView.Tag = Files[Files.Count-1];
			// Refresh the file list - for good measure
			// If we added files, clear the instruction label
Updated 7-Sep-18 20:41pm

1 solution

It's going to depend on how your file names are organised. If they are always in the same format:
Then the simplest way is just to use Split:
string[] parts = filename.Split('_');
if (parts.Length == 5)
   string bitYouWant = parts[3];
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900