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,是 ...
随机推荐
- svn: 命令行上传多个指定文件
上传指定后缀名文件 svn st | grep swift | cut -d' ' -f8- > targets.txt svn ci -m "comments" --tar ...
- C++中const引用的是对象的时候只能调用该对象的f()const方法
const引用的作用: 1. 避免不必要的复制. 2. 限制不能修改对象. const 引用的是对象时只能访问该对象的const 函数 例: class A { public: void cons ...
- jquery 1.9以上新版本不支持toggle()的解决方法
原文:http://blog.csdn.net/u011061889/article/details/50397462 参考: http://www.cnblogs.com/lionden/archi ...
- 3.3-ISDN
3.3-ISDN 综合业务数字网ISDN(Integrated Services Digital Network): ISDN主要有两种接口类型:分为BRI(2B+D=2×64+16K ...
- [Vue @Component] Pass Props to Vue Functional Templates
Functional templates allow you to create components consisting of only the template tag and exposing ...
- PHP的mod_rewrite重写模块将.php后缀换成.html
apache Rewrite mod_rewrite的魔力 简单举例:创建三个文件.分别命名为 test.html,test.php和.htaccess test.html 输入: <h1> ...
- C#使用oledb连接excel运行Insert Into语句出现“操作必须使用一个可更新的查询”的解决的方法
我错误发生时的环境:Windows 7,Framework 4.0,Microsoft Office 2007,VS2010,c# WinForm. 部分代码: string strConn = &q ...
- 【干货】Kafka 事务特性分析
特性背景 消息事务是指一系列的生产.消费操作可以要么都完成,要么都失败,类似数据库的事务.这个特性在0.10.2的版本是不支持的,从0.11版本开始才支持.华为云DMS率先提供Kafka 1.1.0的 ...
- 日常工作中常见的mysql优化技巧
1.介绍一下MYSQL经常使用的优化技巧. MySQL 自带 slow log 的分析工具 mysqldumpslow ,可是没有说明.本文通过分析该脚本,介绍了其用法. slow log 是 MyS ...
- ALSA声卡驱动中的DAPM详解之三:如何定义各种widget
上一节中,介绍了DAPM框架中几个重要的数据结构:snd_soc_dapm_widget,snd_soc_dapm_path,snd_soc_dapm_route.其中snd_soc_dapm_pat ...