Answer the question
In order to leave comments, you need to log in
How to move files according to a given condition?
Good afternoon, the essence of the question is as follows, there are several folders and files (for example, Documents and Service)
There is a C # code that should "scatter" files into folders (if the file is called "Document", then it must be placed in the "Documents" folder etc.)
using System;
using System.IO;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string common_path = @"C:\Users\User\Documents\Test";
string pathA = @"C:\Users\User\Documents\Test\Документы";
string pathB = @"C:\Users\User\Documents\Test\Служебные";
string[] files = Directory.GetFiles(common_path,"Д*"); // Получаем список всех файлов (Документов)
foreach(string s in files)
{
File.Move(s, pathA);
}
}
}
}
Answer the question
In order to leave comments, you need to log in
The second argument is not a folder, but the full path to a new file with a name.
var filename = Path.GetFileName(s);
File.Move(s, pathA + "\\" + filename);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question