使用VBS浏览本地文件的3种方式,获取完整路径
2019-12-09 12:04
1036 查看
因为系统组件差异,所以代码也有不同文章最后脚本之家给出一个通用的代码
XP下:
Function BrowseForFile( ) ' Select File dialog based on a script by Mayayana ' Known issues: ' * Tree view always opens Desktop folder ' * In Win7/IE8 only the file NAME is returned correctly, the path returned will always be C:\fakepath\ ' * If a shortcut to a file is selected, the name of that FILE will be returned, not the shortcut's 'On Error Resume Next '===========1, file browserer in XP ================== Set objDialog = CreateObject("UserAccounts.CommonDialog") objDialog.Filter = "txt|*.txt" objDialog.InitialDir = "C:\" intResult = objDialog.ShowOpen If intResult <> 0 Then BrowseForFile = objDialog.FileName exit function else msgbox "Error." End If end function BrowseForFile
2003下'for windows 2003
Function SelectAFile Set objDialog = CreateObject("SAFRCFileDlg.FileOpen") intResult = objDialog.OpenFileOpenDlg SelectAFile = objDialog.FileName End Function
浏览器方式:
Function BrowseForFile( ) '===========2, fileselect in IE ====================== 'Another way to get file path Dim objIE, strSelected BrowseForFile = "" Set objIE = CreateObject( "InternetExplorer.Application" ) objIE.ToolBar = False objIE.Resizable = False objIE.StatusBar = False objIE.Width = 300 objIE.Height = 100 objIE.visible = True objIE.Navigate( "about:blank" ) Do Until objIE.ReadyState = 4 Loop ' Center the dialog window on the screen With objIE.Document.ParentWindow.Screen objIE.Left = (.AvailWidth - objIE.Width ) \ 4 objIE.Top = (.Availheight - objIE.Height) \ 4 End With objIE.Document.Write "<HTML><BODY><INPUT ID=""FileSelect"" NAME=""FileSelect"" TYPE=""file""><BODY></HTML>" With objIE.Document.all.FileSelect .focus .click strSelected = .value End With objIE.Quit Set objIE = Nothing If Trim(strSelected) = "" Then Msgbox "You selected no file." Wscript.Quit End If BrowseForFile = strSelected End Function BrowseForFile
有没有发现上面的代码运行不易啊,这里脚本之家小编为大家分享一个可用的,虽然上面的的代码不能用但学习参考一下还是不错的,现在因为平台愿意导致,原来这些代码都是可以使用的。
Function ChooseFile() Dim Result Result = "" Dim IE : Set IE = CreateObject("InternetExplorer.Application") With IE .Visible = False .Navigate("about:blank") Do Until .ReadyState = 4 : Loop With .Document .Write "<html><body><input id='f' type='file'></body></html>" With .All.f .Focus .Click Result = .Value End With End With .Quit End With Set IE = Nothing ChooseFile = Result End Function ChooseFile
好了这篇 关于vbs选择本地文件功能的代码就介绍到这了,需要的朋友可以参考一下。
相关文章推荐
- 使用VBS通过3种方式浏览本地文件,获取完整路径。
- "打开方式"打开本地文件,获取文件路径
- javaSE基础知识——day16 IO流异常处理方式: try...catch、 throws、File类的使用:获取文件路径,文件名及文件名称过滤器、文件更改名字、删除更多文件
- [总结]Android 获取本地视频文件路径并播放的几种方式
- java中使用ssh方式获取服务器的文件到本地iteye技术网站
- 获取/使用偏好设置文件共有3种方式
- 获取快捷方式原文件路径
- 获取完整的系统临时文件路径
- 使用C++获取文件路径,已获取当前路径为基础
- 快速浏览Silverlight3 Beta:使用SaveFileDialog保存到本地文件
- 完整的获取当前jsp文件路径的语句
- 本地登录环境的获取文件的命令使用
- MFC 浏览文件夹,浏览保存文件, 获取当前路径
- Shell右键菜单获取快捷方式(LNK文件)的实际路径
- VS2008 VC 如何把图片等资源文件通过本地路径加载方式加载
- 使用递归方式遍历指定磁盘路径下的文件及文件夹
- 获取程序所在文件路径及保存STREAM到本地文件
- 3种Java从文件路径中获取文件名的方法
- 使用C++获取文件路径,已获取当前路径为基础
- jsp页面中引入文件路径问题的解决方案(使用request获取项目路径)【原创】