- 1 year ago
Copy and rename an wildcard image file c#
To copy and rename wildcard image files in C#, you can use the Directory.GetFiles
method to get the list of files matching the wildcard pattern, and then use the File.Copy
method to copy and rename each file.
Here's an example that demonstrates how to copy and rename wildcard image files:
csharpusing System.IO;
class Program
{
static void Main()
{
string sourceDirectory = "C:\\Path\\To\\Source";
string targetDirectory = "C:\\Path\\To\\Target";
string wildcardPattern = "*.jpg"; // Change this to your desired wildcard pattern
string[] files = Directory.GetFiles(sourceDirectory, wildcardPattern);
foreach (string file in files)
{
string fileName = Path.GetFileName(file);
string targetFileName = "new_" + fileName; // Change this to your desired new file name
string targetPath = Path.Combine(targetDirectory, targetFileName);
File.Copy(file, targetPath);
}
Console.WriteLine("Files copied and renamed successfully.");
}
}
In the example above, replace "C:\\Path\\To\\Source"
with the path to your source directory, "C:\\Path\\To\\Target"
with the path to your target directory, and "*.jpg"
with your desired wildcard pattern (e.g., "*.png"
for PNG files).
The code will copy each matching file from the source directory to the target directory and rename it with the prefix "new_"
. Adjust the targetFileName
variable to modify the new file name as per your requirements.
Make sure to include the necessary using
directives at the top of your C# file:
csharpusing System.IO;
After running the code, you should see the wildcard image files copied and renamed in the target directory.