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,是 ...
随机推荐
- 多个线程对hashmap进行put操作的异常
多个线程对hashmap进行put操作的异常 Exception in thread "Thread-0" java.lang.ClassCastException: java.u ...
- POJ_1679_The Unique MST(次小生成树模板)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23942 Accepted: 8492 D ...
- table形式的列表页面显示
(该案例在项目中的reserve_bchmc.html,其对应的后台在CountBean中) 先看一下效果图: 该列表页面并不是用easyUI中的datagrid实现的,而是用table实现页面显示的 ...
- vim g 和 % 区别
vim中的g(global)和%的区别: g:全局的 s/pattern/replacement/ : 替换行中出现的每一个pattern g/pattern/s/pattern/replaceme ...
- Linux ALSA声卡驱动之七:ASoC架构中的Codec
1. Codec简介(ad/da) 在移动设备中,Codec的作用可以归结为4种,分别是: 对PCM等信号进行D/A转换,把数字的音频信号转换为模拟信号 对Mic.Linein或者其他输入源的模拟信 ...
- 使用display:flex;实现垂直水平居中
body,div{margin:0px;padding:0px;} .flex-container{display:flex;height:300px;background-color:#ddd;ju ...
- Windows虚拟机中无法传输Arduino程序的问题
现象 最近儿子在学习机器人编程,其中有一步需要把板子和电脑用USB线相连接,然后把在电脑中编辑好的程序传输到Arduino板子上.在Windows笔记本上能正常工作,但在我的Mac笔记本的Window ...
- [App Store Connect帮助]三、管理 App 和版本(2.4)输入 App 信息:提供加密出口合规证明文稿
上传至 App Store Connect 的 App 被上传至位于美国的 Apple 服务器.如果您提交 App 的目的是为了在 App Store 上分发您的 App 或通过美国或加拿大的境外 T ...
- [Swift通天遁地]四、网络和线程-(13)创建一个Socket客户端
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- ubuntu 更显列表 [Connecting to archive.ubuntu.com (2001:67c:1360:8001::21)] 超时的解决方法
问题描述: 在使用apt-get update 时更行列表,显示[Connecting to archive.ubuntu.com (2001:67c:1360:8001::21)]超时 分析: 我已 ...