首先搜索文件后复制文件一个目录到另一个目录? [英] Firstly Search the file Afterthen copy file one directories to another directories?
问题描述
我有一个名为MyFolder1和Myfolder2的Tow目录。 MyFolder1存储大量文件,如
(1)1452_43552388(jsop2201).Mp3
(2)1452_43552389(jsop2202).Mp3
( 3)1452_43552390(jsop2203).Mp3
(4)1452_43552266(Apsp2201).Mp3
(5)1452_43552278(MNOP4281).Mp3
(6)1452_43552297(HSOP2258).Mp3
首先我想搜索文件名称只有 jsop
(1 )1452_43552388(jsop2201).Mp3
(2)1452_43552389(jsop2202).Mp3
(3)1452_43552390(jsop2203).Mp3
和自动复制并粘贴另一个目录(MyFolder2)。请帮助我........
尝试:
string [] files = Directory.GetFiles(pathtoMyFolder1, *(jsop * );
foreach ( string oldPath in files)
{
string newPath = Path.Combine(pathtoMyFolder2,Path.GetFileName(oldPath));
File.Copy(oldPath,newPath);
}
这是复制文件的解决方案:
// 复制一个文件:
string fileName = test.mp3;
string sourcePath = @ C:\dir \;
string targetPath = @ C:\\ \\dir\subdir\跨度>;
string sourceFile = System.IO.Path.Combine(sourcePath,fileName);
string destFile = System.IO.Path.Combine(targetPath,fileName);
if (!System.IO.Directory.Exists(targetPath))
{
System .IO.Directory.CreateDirectory(TARGETPATH);
}
System.IO.File.Copy(sourceFile,destFile, true );
一次复制更多文件:
// 复制所选文件:
if (System.IO.Directory。 Exists(sourcePath))
{
string [] files = { < span class =code-string> C:\\dir\\1.mp3, C:\\dir \\\mp3, C :\\dir\\3.mp3\" 跨度>};
// 复制文件并覆盖目标文件(如果已存在)。
foreach ( string s in 文件)
{
string fileName = 1.MP3跨度>;
string targetPath = @ C:\\ \\dir\subdir\跨度>;
string destFile = System.IO.Path.Combine(targetPath,fileName);
System.IO.File.Copy(s,destFile, true );
}
}
其他
{
}
I've a Tow Directories which name is MyFolder1 and Myfolder2. MyFolder1 store lots of file just like
(1) 1452_43552388(jsop2201).Mp3
(2) 1452_43552389(jsop2202).Mp3
(3) 1452_43552390(jsop2203).Mp3
(4) 1452_43552266(Apsp2201).Mp3
(5) 1452_43552278(MNOP4281).Mp3
(6) 1452_43552297(HSOP2258).Mp3
Firstly I want to search file Name only jsop
(1) 1452_43552388(jsop2201).Mp3
(2) 1452_43552389(jsop2202).Mp3
(3) 1452_43552390(jsop2203).Mp3
and automatic copy and paste another directories(MyFolder2). Please help me........
Try:
string[] files = Directory.GetFiles(pathtoMyFolder1, "*(jsop*"); foreach (string oldPath in files) { string newPath = Path.Combine(pathtoMyFolder2, Path.GetFileName(oldPath)); File.Copy(oldPath, newPath); }
Here is a solution for copy files:
//Copy one file: string fileName = "test.mp3"; string sourcePath = @"C:\dir\"; string targetPath = @"C:\dir\subdir\"; string sourceFile = System.IO.Path.Combine(sourcePath, fileName); string destFile = System.IO.Path.Combine(targetPath, fileName); if (!System.IO.Directory.Exists(targetPath)) { System.IO.Directory.CreateDirectory(targetPath); } System.IO.File.Copy(sourceFile, destFile, true);
Copy more files at once:
//Copy selected files: if (System.IO.Directory.Exists(sourcePath)) { string[] files = {"C:\\dir\\1.mp3", "C:\\dir\\2.mp3", "C:\\dir\\3.mp3"}; // Copy the files and overwrite destination files if they already exist. foreach (string s in files) { string fileName = "1.mp3"; string targetPath = @"C:\dir\subdir\"; string destFile = System.IO.Path.Combine(targetPath, fileName); System.IO.File.Copy(s, destFile, true); } } else { }
这篇关于首先搜索文件后复制文件一个目录到另一个目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!