using
System;
using
System.IO;
using
System.Collections.Generic;
using
System.Threading.Tasks;
class
Program
{
static
void
Main()
{
string
sourceFile =
"files.txt"
;
string
targetPath =
@"d:\gin\c#"
;
string
newDirectory =
@"d:\gin\NewDirectory"
;
string
[] SourceFileContent = File.ReadAllLines(sourceFile, System.Text.Encoding.Default);
List<
string
> TargetAllDir;
List<
string
> TargetAllDirName = GetFileName(Directory.EnumerateDirectories(targetPath,
"*"
, SearchOption.AllDirectories),
out
TargetAllDir);
List<
string
> TargetAllFile;
List<
string
> TargetAllFileName = GetFileName(Directory.EnumerateFiles(targetPath,
"*"
, SearchOption.AllDirectories),
out
TargetAllFile);
List<
int
> res;
bool
found =
false
;
foreach
(
string
source
in
SourceFileContent)
{
found =
false
;
res = FindDirAndFileName(source, TargetAllDirName);
if
(res.Count > 0)
{
found =
true
;
foreach
(
int
index
in
res)
{
string
newTargetDir = PathReplace(TargetAllDir[index], newDirectory);
if
(!Directory.Exists(newTargetDir))
{
Directory.CreateDirectory(newTargetDir);
}
foreach
(
string
subDir
in
Directory.EnumerateDirectories(TargetAllDir[index],
"*"
, SearchOption.AllDirectories))
{
string
newSubDir = PathReplace(subDir, newDirectory);
if
(!Directory.Exists(newSubDir))
{
Directory.CreateDirectory(newSubDir);
}
}
foreach
(
string
subFile
in
Directory.EnumerateFiles(TargetAllDir[index],
"*"
, SearchOption.AllDirectories))
{
File.Copy(subFile, PathReplace(subFile, newDirectory),
true
);
}
}
}
res = FindDirAndFileName(source, TargetAllFileName);
if
(res.Count > 0)
{
found =
true
;
foreach
(
int
index
in
res)
{
string
newFileFullName = PathReplace(TargetAllFile[index], newDirectory);
string
newFileFullDir = Path.GetDirectoryName(newFileFullName);
if
(!Directory.Exists(newFileFullDir))
{
Directory.CreateDirectory(newFileFullDir);
}
File.Copy(TargetAllFile[index], newFileFullName,
true
);
}
}
if
(!found)
{
Console.WriteLine(
"{0} 未找到!"
, source);
}
}
}
static
List<
string
> GetFileName(IEnumerable<
string
> Data,
out
List<
string
> ToTList)
{
List<
string
> FileName =
new
List<
string
>();
ToTList =
new
List<
string
>();
foreach
(
var
dir
in
Data)
{
FileName.Add(Path.GetFileName(dir));
ToTList.Add(dir);
}
return
FileName;
}
static
List<
int
> FindDirAndFileName(
string
source, List<
string
> Target)
{
List<
int
> result =
new
List<
int
>();
int
index = Target.IndexOf(source);
if
(index == -1)
return
result;
result.Add(index);
while
(
true
)
{
index = Target.IndexOf(source, index + 1);
if
(index != -1)
{
result.Add(index);
}
else
{
break
;
}
}
return
result;
}
static
string
PathReplace(
string
source,
string
newDirectory)
{
string
newPath ;
if
(Path.GetPathRoot(source).ToUpper() == Path.GetPathRoot(newDirectory).ToUpper())
{
int
index = 0;
while
(index < newDirectory.Length)
{
if
(
char
.ToUpper(source[index]) ==
char
.ToUpper(newDirectory[index]))
{
index++;
}
else
{
break
;
}
}
while
(
true
)
{
if
(source[--index] ==
'\\'
)
{
newPath = Path.Combine(newDirectory, source.Substring(index + 1));
break
;
}
}
}
else
{
newPath = Path.Combine(newDirectory, source.Substring(3));
}
return
newPath;
}
}