0

How do I move muliple files with the same extension name to a different directory?

  • Get files with pattern match (*.extension) – Krishna Feb 27 '18 at 23:03
  • 1
    A search on "csharp move multiple files", first result: https://stackoverflow.com/questions/23424844/visual-c-move-multiple-files-with-the-same-extensions-into-another-directory?s=1|77.8944 – Michael O'Neill Feb 27 '18 at 23:05

2 Answers2

1

Try this:

string[] files = Directory.GetFiles("C:/YourPath/", "*.txt"); //THIS WILL ONLY MOVE TXT FILES

foreach (string s in files)
{
   File.Move(s, Path.Combine("C:/YourOutputPath/", Path.GetFileName(s)));
}

PD: Welcome to SO. In future posts try to show you´ve tried something. Otherwise your question is going to be closed and very downvoted.

NicoRiff
  • 4,803
  • 3
  • 25
  • 54
0

Something along the lines of:

var extension = ".txt"
var outputPath = Path.GetDirectoryName("C:/Output");
var path = Path.GetDirectoryName(@"C:/path");
var files = Directory.GetFiles(path, extension);


foreach (var file in files)
{
    if (file.ToUpper().Contains(".TXT"))
        System.Diagnostics.Process.Start("CMD.exe",$"/C copy /b {file} {outputPath}");
}

Edit: improvements based on NicoRiff's answer

Minijack
  • 726
  • 7
  • 23