输入某文件夹路径,遍历该文件夹及其子文件夹(包括子文件夹的子文件夹等),获取其中所有文件的函数:
/// <summary>
/// 查找指定文件夹下指定后缀名的文件
/// </summary>
/// <param name="directory">文件夹</param>
/// <param name="pattern">后缀名</param>
/// <returns>文件路径</returns>
public void GetFiles(DirectoryInfo directory, string pattern, ref List<string> fileList)
{
if (directoryExists || patternTrim() != stringEmpty)
{
try
{
foreach (FileInfo info in directoryGetFiles(pattern))
{
fileListAdd(infoFullNameToString());
}
}
catch (SystemException ex)
{
ConsoleWriteLine(exToString());
}
foreach (DirectoryInfo info in directoryGetDirectories())//获取文件夹下的子文件夹
{
GetFiles(info, pattern, ref fileList);//递归调用该函数,获取子文件夹下的文件
}
}
}
使用实例:
List<string> FindResult = new List<string>();
GetFiles(new DirectoryInfo(@"C:\","", ref FindResult); //获取C盘下的所有文件路径
GetFiles(new DirectoryInfo(@"\\1921681107\Save"), "", ref FindResult);//获取局域网上共享文件夹下的所有文件
def iterbrowse(path):
for home, dirs, files in oswalk(path):
for filename in files:
yield ospathjoin(home, filename)
for fullname in iterbrowse("/home/bruce"):
print fullnameSub LoopAllExcelFilesInFolder()
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
'这里很关键,决定宏执行快慢的关键
ApplicationScreenUpdating = False
ApplicationEnableEvents = False
ApplicationCalculation = xlCalculationManual
'打开目录选择框
Set FldrPicker = ApplicationFileDialog(msoFileDialogFolderPicker)
With FldrPicker
Title = "请选择目录"
AllowMultiSelect = False
If Show <> -1 Then GoTo NextCode
myPath = SelectedItems(1) & "\"
End With
'取消选择
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
'指定过滤的文件后缀
myExtension = "xls"
'遍历全路径
myFile = Dir(myPath & myExtension)
'循环处理每一个文件
Do While myFile <> ""
'打开
Set wb = WorkbooksOpen(Filename:=myPath & myFile)
'确保工作簿被打开,在处理下一个文件时
DoEvents
'设置背景色
wbWorksheets(1)Range("A1:Z1")InteriorColor = RGB(51, 98, 174)
'保存工作簿
wbClose SaveChanges:=True
'确保工作簿被关闭,在处理下一个文件时
DoEvents
'接着处理下一个
myFile = Dir
Loop
'提示处理完成
MsgBox "处理完成!"
ResetSettings:
'恢复设置
ApplicationEnableEvents = True
ApplicationCalculation = xlCalculationAutomatic
ApplicationScreenUpdating = True
End Subdeletree
b
这个是dos命令,删除所有名称为b的文件夹
其实你可以更方便的在这个文件夹上点右键,点击搜索,在出来的窗口里面输入b然后点确定,在右面的搜索结果窗口出来的就是你要求的文件了
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)