根据文件名称选择数千个文件

2024-09-30 08:18:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个问题,我有一个包含30k个文件名的文本文件,我需要从包含100k个其他文件的windows文件夹中提取出来。在

这可能是一个很简单的问题,但是,我已经在网上搜索了很多次,没有找到解决方案,也许我用错了术语。在

很好地使用Windows批处理文件、c#、Java、Python等等。。。在

把文件重命名为xxfilename这样我就可以按文件名排序了吗?在

提前谢谢。在


Tags: 文件文件夹排序文件名windowsjava解决方案重命名
3条回答

如果文件包含文件名列表,则可以将其读取并将文件复制到目标位置。在

比如:

string destination = "D:\\";
foreach (var filename in File.ReadAllLines("fileWithFilenames").Where(f => File.Exists(f)))
{
    File.Copy(filename, Path.Combine(destination, Path.GetFileName(filename)));
}

使用批处理文件:

setlocal
set destination=c:\Temp\Destination

for /f %%f in (index.txt) do (
    copy "%%f" "%destination%"
)

endlocal

使用c#:

foreach (var filePath in File.ReadAllLines(indexFile))
{
    if (File.Exists(filePath))
    {
        var destinationPath = Path.Combine(destinationRoot, Path.GetFileName(filePath));
        File.Copy(filePath, destinationPath);
    }
}

相关问题 更多 >

    热门问题