dos命令在vba中应用
正常情况下想要遍历文件夹和子文件夹,可以采用递归的方式
Sub ListFilesTest()
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show Then myPath$ = .SelectedItems() Else Exit Sub
End With
If Right(myPath, ) <> "\" Then myPath = myPath & "\"
[a:a] = ""
Call ListAllFso(myPath)
End Sub
Function ListAllFso(myPath$)
Set fld = CreateObject("Scripting.FileSystemObject").GetFolder(myPath)
For Each f In fld.Files
' [a65536].End(3).Offset(1) = f.Name
[a65536].End().Offset() = f.Path
Next
For Each fd In fld.SubFolders
' [a65536].End(3).Offset(1) = " " & fd.Name & ""
[a65536].End().Offset() = fd.Path
Call ListAllFso(fd.Path)
Next
End Function
但用过DOS命令的都知道,DOS有个命令,一句话就可以遍历文件夹和子文件夹,下面用vba来实现DOS的dir命令,实现上面的功能
Sub 遍历文件夹()
Dim WSH, wExec, sCmd As String, Result As String, ar
Set WSH = CreateObject("WScript.Shell")
' Set wExec = WSH.Exec("ping 127.0.0.1")
Set wExec = WSH.exec("cmd /c dir /b /s D:\lcx\*.xls*")
Result = wExec.StdOut.ReadAll
ar = Split(Result, vbCrLf)
For i = To UBound(ar)
Cells(i + , ) = ar(i)
Next
Set wExec = Nothing
Set WSH = Nothing
End Sub
在学习使用这个功能的时候看到一个网上的例子,写的很好,而且还让我意外的学习到一个filter的函数,这个函数的功能也是相当强大了
Sub ListFilesDos()
Set myfolder = CreateObject("Shell.Application").BrowseForFolder(, "GetFolder", )
If Not myfolder Is Nothing Then myPath$ = myfolder.Items.Item.Path Else MsgBox "Folder not Selected": Exit Sub
'在这里输入需要指定的关键字,可以是文件名的一部分,或指定文件类型如 ".xlsx"
myFile$ = InputBox("Filename", "Find File", ".xlsx")
tms = Timer
With CreateObject("Wscript.Shell")
'所有文档含子文件夹 chr(34)是双引号"",因为代码中要表达"",需要写成"""" vbCrLf 回车换行
ar = Split(.exec("cmd /c dir /a-d /b /s " & Chr() & myPath & Chr()).StdOut.ReadAll, vbCrLf)
s = "from " & UBound(ar) & " Files by Search time: " & Format(Timer - tms, " 0.00000") & " in: " & myPath
这个filter竟然可以过滤数组,太厉害了,早知道有这个函数的话,以前写着玩的好些代码玩起来就省事多了
tms = Timer: ar = Filter(ar, myFile)
Application.StatusBar = Format(Timer - tms, "0.00000") & " Find " & UBound(ar) + IIf(myFile = "", , ) & " Files " & s
End With
[a:a] = "": If UBound(ar) > - Then [a2].Resize( + UBound(ar)) = WorksheetFunction.Transpose(ar)
End Sub
'上例简写如下
Sub ListFilesDos_lcx()
Set myfolder = CreateObject("Shell.Application").BrowseForFolder(, "GetFolder", )
If Not myfolder Is Nothing Then myPath$ = myfolder.Items.Item.Path Else MsgBox "Folder not Selected": Exit Sub
With CreateObject("Wscript.Shell")
'所有文档含子文件夹 chr(34)是双引号"",因为代码中要表达"",需要写成"""" vbCrLf 回车换行
ar = Split(.exec("cmd /c dir /a-d /b /s " & Chr() & myPath & "\*.xls*" & Chr()).StdOut.ReadAll, vbCrLf)
End With
[a:a] = "": If UBound(ar) > - Then [a2].Resize( + UBound(ar)) = WorksheetFunction.Transpose(ar)
End Sub
shell命令也是很强大很好用了,电脑里的可执行文件,shell都可以执行,shell也是可以执行cmd的,只是无法获取到cmd控制台的数据
Sub 打开路径()
Shell "cmd /c ipconfig > """ & ThisWorkbook.Path & "\ip.txt"""
Shell "explorer.exe " & ThisWorkbook.Path, vbNormalFocus
End Sub
dos命令在vba中应用的更多相关文章
- 在DOS命令行窗口中显示系统环境环境变量
(这是一个小技巧) 示例命令: echo %path% path是系统环境变量,使用百分号包围起来 http://www.cnblogs.com/danzhang 张洪君 微软ALM MVP
- 常用的几个Dos命令-持续更新中
1.服务相关 (1).查看服务 C:\Windows\system32>net start 已经启动以下 Windows 服务: (2).启动服务 C:\Windows\system32> ...
- Window中常见的dos命令
1.如何实行操作dos命令:如果是Windows电脑,从开始--->所有程序---->附件--->命令提示 这样就可以开始命令提示符了 2关于一些dos命令: 2.1 盘符切换:盘符 ...
- DOS命令行中的双引号
在DOS命令窗口下,运行C:\Program Files\WinRAR\WinRAR.exe,提示如下错误: 因为C:\Program Files\WinRAR\WinRAR.exe中含有空格,它被分 ...
- 在.net中悄悄执行dos命令,并获取执行的结果(转)
一.怎样使dos命令悄悄执行,而不弹出控制台窗口? 1.需要执行带“/C”参数的“cmd.exe”命令,它表示执行完命令后立即退出控制台.2.设置startInfo.UseShellExecute = ...
- Window 中常见的dos命令
在哪里操作dos命令: win7---->开始---->所有程序---->附件---->命令提示符 win7-- ...
- DOS命令中出现空格问题
1.DOS命令中路径出现空格时如何处理? 在DOS命令中,如果路径中出现空格,可能为报错:如参数错误 如: xcopy C:\ABC CD\txt.txt C:\ , 由于路径中包含空格,执行后 ...
- 在Windows的Dos命令中切换盘符
在Windows的Dos命令中切换盘符... ---------------------------- --------------------------------------- -------- ...
- Windows7 中常用的一些DOS命令总结
Windows7 中常用的一些DOS命令总结... ----------------------- -------------------------------------------- dos,是 ...
随机推荐
- Hadoop2.0之开启日志
配置 修改 mapred-site.xml <property> <name>mapreduce.jobhistory.address</name> <val ...
- kfk: async disk IO深度解析
http://www.itpub.net/thread-1724044-1-1.html
- python列表可以加可以乘
python列表可以加可以乘 list=['abcd',786,2.23,'runoob',70.2] tinylist = [123,'runoob'] print(list) print(list ...
- Cocos2d-HTML5搭载nodejs express3
源代码 已经上传到github Cocos2d-HTML5 入门第一天搭载了express3 server.Cocos2d-html5配置改了不少路径,改得有点乱. 今天又重搭了一遍server,力求 ...
- configure: error: mysql configure failed. Please check config.log for more information.
为php添加mysql模块时报错 configure: error: mysql configure failed. Please check config.log for more informat ...
- InnoDB: Error: log file .\ib_logfile0 is of different size 0 10485760 bytes
启动WAMP Server的时候报例如以下的错误: 140618 23:12:32 [Note] Plugin 'FEDERATED' is disabled. 140618 23:12:32 Inn ...
- 【二】注入框架RoboGuice使用:(Your First View Injection)
上一篇我们简单的介绍了一下RoboGuice的使用([一]注入框架RoboGuice使用:(A brief example of what RoboGuice does)),今天我们我看下View的注 ...
- jquery 推断checkbox 是否选中
这是一个蛋疼的节奏.曾经写的代码如今失效了. jquery 推断checkbox 是否被选中,刚開始我是这样写的,并且没问题 $("#ziduana").attr("ch ...
- BNU 13024 . Fi Binary Number 数位dp/fibonacci数列
B. Fi Binary Number A Fi-binary number is a number that contains only 0 and 1. It does not conta ...
- YOCTO
Yocto ,是一个开源社区它通过提供模版.工具和方法帮助开发者创建基于linux内核的定制系统,支持ARM, PPC, MIPS, x86 (32 & 64 bit)硬件体系架构.