Link to home
Start Free TrialLog in
Avatar of kimmie8000
kimmie8000Flag for United States of America

asked on

C# (how to mach file names with different extensions....??)

Each file is processed and get a .wav file in this program.  I need to copy only the filenames that match to a .wav file in a folder.  I know this is easy, but I am converting from VB.NET to C# and the language is not the same.  I need this done fast apparently.  I need a few gallons of coffee at this point.

Thanks in advance,

string sourcePath = ConfigurationManager.AppSettings["DictaphoneChild"].ToString() + "\\" + fileName;
                    string targetPath = ConfigurationManager.AppSettings["Dictaphone"].ToString() + "\\" + fileName;

                    string[] files = System.IO.Directory.GetFiles(sourcePath);

                    // Copy the files and overwrite destination files if they already exist.
                    foreach (string s in files)
                    {
                        // Use static Path methods to extract only the file name from the path.
                        fileName = System.IO.Path.GetFileName(s);
                        // code for only wav files that match file name XML.

                        string destFile = System.IO.Path.Combine(targetPath, fileName);
                        System.IO.File.Copy(s, destFile, true);
                    }

Open in new window

Avatar of rprimora
rprimora

You have something.wav
and you want just "something"?!

make a method that takes filename as argument and returns string without extension.

string newFile = filename.Substring(0, filename.LastIndexOf("."));
Sounds to me like you are only looking for .wav files.  If that is correct the following will work.

string[] files = System.IO.Directory.GetFiles(sourcePath, "*.wav");
try this function
bool CompareJustFileName(string strFile1, string strFile2)
{
	int index1 = strFile1.LastIndexOf("\\");
	if(index1 == -1)
	{
		index1 = 0;
	}

	int index2 = strFile2.LastIndexOf("\\");
	if(index2 == -1)
	{
		index2 = 0;
	}

	int comp = strFile1.Substring(index1, strFile1.LastIndexOf(".")).CompareTo(strFile2.Substring(index2, strFile2.LastIndexOf(".")));

	return (comp==0);
}

Open in new window

Avatar of kimmie8000

ASKER

The files in the directory look like this:
//foldername....
Text.xml
Text1.xml
Text2.xml
Text1.wav
Text2.wav

They want to move Text1.xml, Text2.xml and Text1.wav,Text2.wav to a different directory, leaving text.xml in the original folder.  I am sort of sleepy(work, work, work), and I have spend too much time trying to get this to work.  I know that answer is simple it should be after I get the file name I just don't know th comman in C# to hunt down the Text1.xml, Text1.wav and copy it to another directory.  The copy part I know how to do it is the matching if there is a .wav file.      
How about something like this

string sourceDir = ConfigurationManager.AppSettings["DictaphoneChild"].ToString();
            string targetDir = ConfigurationManager.AppSettings["Dictaphone"].ToString();

            string[] wavFiles = Directory.GetFiles(sourceDir, "*.wav");

            foreach (string wavFile in wavFiles)
            {
                string wavFileName = Path.GetFileName(wavFile);
                string xmlFileName = Path.GetFileNameWithoutExtension(wavFile) + ".xml";

                string targetWavFileName = Path.Combine(targetDir, wavFileName);
                string targetXmlFileName = Path.Combine(targetDir, xmlFileName);

                File.Copy(wavFile, targetWavFileName);
                File.Copy(xmlFileName, targetXmlFileName);
            }
Avatar of Mike Tomlinson
It seems to me that you should search for all ".xml" files in "sourceDir" and then only move those files having a matching ".wav" file:
            string sourcePath = ConfigurationManager.AppSettings["DictaphoneChild"].ToString() + "\\" + fileName;
            string targetPath = ConfigurationManager.AppSettings["Dictaphone"].ToString() + "\\" + fileName;

            string wavFile;
            string[] xmlFiles = System.IO.Directory.GetFiles(sourcePath, "*.xml");
            foreach (string xmlFile in xmlFiles)
            {
                wavFile = System.IO.Path.Combine(sourcePath, System.IO.Path.GetFileNameWithoutExtension(xmlFile) + ".wav");
                if (System.IO.File.Exists(wavFile))
                {
                    System.IO.File.Copy(xmlFile, System.IO.Path.Combine(targetPath, System.IO.Path.GetFileName(xmlFile)), true);
                    System.IO.File.Copy(xmlFile, System.IO.Path.Combine(targetPath, System.IO.Path.GetFileName(wavFile)), true);
                }
            }

Open in new window

This is for MarshalMooreman, because you are on the right track!  

I get an error stating it cannot find the .xml file when tried your solution.  It fails on the last line.  

File.Copy(xmlFileName, targetXmlFileName);

I assure you the file is there.  Other than that, it is working smoothly.
ASKER CERTIFIED SOLUTION
Avatar of MarshallMoorman
MarshallMoorman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
After being up all night writing code, this person was a real help.  I am still trying to convert my VB.NET skills to C#.  I think I can get some sleep after this intervention.  Words cannot decribe how much I appreciate the help!