locked
Problem with File::Copy and full filename RRS feed

  • Question

  • Hey,

    i used the search-function > no matches

    google > no matches

    so let me ask u for help:

    i like to copy a file with File::Copy(String, String), but i need to copy a file located like "foo_*.zip". There ist just one file named "foo_836585.zip" but the file gets updates frequently and the number in the filename changes. So I need to copy with "*". But that doesn't work.

    Any body knows an answer?

    Thanks,

    Kanasaru

    Friday, April 20, 2012 9:48 AM

Answers

  • File::Copy only accepts real file names. If you want to use a search pattern like * then you have to use something like Directory::GetFiles. Something like:

    	for each (String ^filePath in Directory::GetFiles(sourceDir, "foo_*.zip"))
    		File::Copy(filePath, Path::Combine(targetDir, Path::GetFileName(filePath)));
    
    Note that you may need to add appropriate exception handling to deal with the case when the file name changes between the call to GetFiles and the call to Copy.
    • Marked as answer by Kanasaru Friday, April 20, 2012 10:22 AM
    Friday, April 20, 2012 10:13 AM

All replies

  • First you use Directory::GetFile(String, String) to retrieve a string array of found files. The first parameter contains the directory, the 2nd the search mask.

    Than you call File::Copy(String, String) for each string array entry.


    • Edited by Bordon Friday, April 20, 2012 10:13 AM
    Friday, April 20, 2012 10:12 AM
  • File::Copy only accepts real file names. If you want to use a search pattern like * then you have to use something like Directory::GetFiles. Something like:

    	for each (String ^filePath in Directory::GetFiles(sourceDir, "foo_*.zip"))
    		File::Copy(filePath, Path::Combine(targetDir, Path::GetFileName(filePath)));
    
    Note that you may need to add appropriate exception handling to deal with the case when the file name changes between the call to GetFiles and the call to Copy.
    • Marked as answer by Kanasaru Friday, April 20, 2012 10:22 AM
    Friday, April 20, 2012 10:13 AM