找不同文件夹下面相同文件名的文件,紧急,立即解决立即给分!

qifachen 2009-12-02 09:21:13
比如说,文件夹A 下面有两个文件夹B与C B下面有一个文件Cary.jpg C下面也有一个文件Cary.jpg
那么怎么实现找Cary.jpg出来呢?

这里只是一个比方,也有可能文件夹B与C下面还有文件夹,又有相同的文件名。总是是找文件夹A下面所有相同文件名的文件出来。
...全文
307 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
flybao51 2009-12-02
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 heng_s 的回复:]
思路:
用递归来获取一个指定目录下的所有文件(包括路径)列表,赋到一个数组里,然后从数组里找出相同文件名的Index即可
[/Quote]+1
oo渣渣oo 2009-12-02
  • 打赏
  • 举报
回复
把我以前写的一个过程给你参考,应该能用的


'获取指定目录下的所有文件名(包括子目录)(使用了递归调用)
Private Function DynamicList(ByVal strParentDir As String) As String()
Dim strSubDir() As String
Dim strSubFile() As String
Dim strSubSubFile() As String
If Tools.IsFileExist(strParentDir) Then
ReDim strSubFile(0)
strSubFile(0) = strParentDir
Else
Dim objDir As Directory
Dim I As Integer = 0
Try
strSubDir = objDir.GetDirectories(strParentDir)
strSubFile = objDir.GetFiles(strParentDir)
If Not IsNothing(strSubDir) Then
For I = 0 To strSubDir.Length - 1
strSubSubFile = DynamicList(strSubDir(I))
If Not IsNothing(strSubSubFile) Then Tools.UniteStrArrayA(strSubFile, strSubSubFile)
Next
End If
Catch e As Exception
AddSysLog("取要备份的文件列表时发生错误:" & e.Message & "(" & strParentDir & ")", EventLogEntryType.Error, 101)
strSubFile = Nothing
Finally
strSubDir = Nothing
strSubSubFile = Nothing
objDir = Nothing
End Try
End If
Return strSubFile
End Function

Public Sub UniteStrArrayA(ByRef Array1() As String, ByVal Array2() As String)
Dim intLength1 As Integer = 0
Dim intLength2 As Integer = 0
Dim I As Integer = 0
If Not IsNothing(Array1) Then intLength1 = Array1.Length
If Not IsNothing(Array2) Then intLength2 = Array2.Length
If intLength2 > 0 Then
If intLength1 = 0 Then
Array1 = Array2
Else
ReDim Preserve Array1(intLength1 + intLength2 - 1)
For I = intLength1 To Array1.Length - 1
Array1(I) = Array2(I - intLength1)
Next
End If
End If
intLength1 = Nothing
intLength2 = Nothing
I = Nothing
End Sub


很久以前写的了,可能有些不合理的地方,自己优化一下
oo渣渣oo 2009-12-02
  • 打赏
  • 举报
回复
思路:
用递归来获取一个指定目录下的所有文件(包括路径)列表,赋到一个数组里,然后从数组里找出相同文件名的Index即可
zhujiazhi 2009-12-02
  • 打赏
  • 举报
回复
public void SearchFile(DirectoryInfo dirInfo, List<string> list1, List<string> list2)
{
if (!IsSystemHidden(dirInfo))
{
try
{
foreach (FileInfo fileInfo in dirInfo.GetFiles())
{
if(list1.Contains(fileInfo.FullName))
{
list2.Add(fileInfo.FullName);
}
else
{
list1.Add(fileInfo.FullName);
}
}
}
catch (Exception e)
{
}
try
{
foreach (DirectoryInfo childDirInfo in dirInfo.GetDirectories())
{
SearchFile(childDirInfo, list1, list2);
}
}
catch (Exception exception)
{
}
}
}

private bool IsSystemHidden(DirectoryInfo dirInfo)
{
if(dirInfo.Parent == null)
{
return false;
}
string attributes = dirInfo.Attributes.ToString();
if(attributes.IndexOf("Hidden") > -1 && attributes.IndexOf("System") > -1)
{
return true;
}
return false;
}

把想要找的路径传进去的,list2就是重复的,只是用C#写的
lang_csdn 2009-12-02
  • 打赏
  • 举报
回复
Private files As New System.Collections.Generic.List(Of String)
Sub GetFiles(ByVal d As System.IO.DirectoryInfo)
For Each f In d.GetFiles
If files.Contains(f.Name) Then
Console.WriteLine("重复的文件:" & f.FullName)
Else
files.Add(f.Name)
End If
Next

For Each child In d.GetDirectories
GetFiles(child)
Next

End Sub

Sub Main()

GetFiles(New IO.DirectoryInfo("c:\a"))
end sub
这样可以吗?



http://www.kg114.com

16,718

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧