Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, May 4, 2012 7:23 PM
I have files that have to get moved to a different location after a given process has run to completion. The problem is the file names change daily with only the fisrt 8 characters in the name not changing, so I'll always have something like "FirstPart xxxxx.txt". The " xxxxx.txt" part changes based on the date the file was created. The only examples I've come acroos so far are those that require the entire path and file name, both source and destination to get passed to a string varialble. How can I move these files using wildcards?
Thanks in advance.
Visual Studio 2008
Stan Benner
All replies (3)
Monday, May 7, 2012 8:19 AM âś…Answered
This worked:
string txtFile = null; string txtFileMoveLoc = null; string[] fileEntries = Directory.GetFiles("D:\\Location_A", "FileName*"); foreach (string fileName in fileEntries) { if (fileName.Contains("FileName")) { txtFile = fileName; txtFileMoveLoc = txtFile.Replace("Location_A", "Location_B"); if (File.Exists(txtFile)) { File.Move(txtFile, txtFileMoveLoc); } } }
Stan Benner
Friday, May 4, 2012 7:28 PM
You can use System.IO.Directory.GetFiles(@"C:\, "FirstPart*.txt") to get an array of strings representing the file names that match in the folder C:\
You can be more specific by using question mark in the pattern, like FirstPart ?????.txt would match 5 changing characters, whereas * will match anything.
Friday, May 4, 2012 7:55 PM
I'm actually using System.IO.Directory.GetFiles(@"C:\, "FirstPart*.txt") already to locate the file I need, then passing the file name to a string variable. However, the entire path and filename gets passed.
Stan Benner