使用VBS浏览本地文件的3种方式,获取完整路径

所属分类: 脚本专栏 / vbs 阅读数: 578
收藏 0 赞 0 分享

因为系统组件差异,所以代码也有不同文章最后脚本之家给出一个通用的代码

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监视网络连接与断开的代码

监视网络连接与断开,特殊情况下可能有点用,两个VBS脚本均来自微软官网
收藏 0 赞 0 分享

可以修改脚本自身运行次数的vbs(Self modifying script)

通过脚本修改脚本的内容,这里以统计脚本运行次数为例,想学习vbs的朋友可以参考下
收藏 0 赞 0 分享

VBScript之通过对比注册表查找隐藏的服务

系统服务有可能被 rootkit 隐藏,但有些时候我们仍可以从注册表中找到相关的信息。建议以管理员权限运行,否则有些服务列举不出来或出现错误的提示
收藏 0 赞 0 分享

VBScript 监控并结束指定进程的代码

有时候我们需要监控一些进程,防止一些进程的开启,发现指定进程运行就关闭它,那么就可以参考下面的代码
收藏 0 赞 0 分享

VBScript 输出中的对齐实现方法

有时候我们需要在vbs中输入的字符实现对齐效果,那么就可以参考下面的代码了
收藏 0 赞 0 分享

vbs 获取当前目录的实现代码

获取当前正执行的VBS的路径,通过Scripting.FileSystemObject组件实现
收藏 0 赞 0 分享

vbs 获取当前目录文件数量的代码(不包括子文件夹中的)

有时候我们需要获取当前目录下有多少个文件,那么就可以参考下面的代码
收藏 0 赞 0 分享

VBS获取当前目录下所有文件夹名字的代码

有时候我们需要获取当前目录下所有文件夹的名字,那么就可以参考下面的代码了
收藏 0 赞 0 分享

VBS读取注册表的两种方法

有时候我们需要用vbs来操作注册表,一般情况下有两种方法,一种是通过wshshell对象的regread,另外一种是使用WMI操作注册表,下面简单的介绍下
收藏 0 赞 0 分享

查看系统C盘剩余空间的VBS脚本

查看系统C盘剩下的空间方法有很多,在本文要为大家介绍的是通过VBS脚本是如何实现的,感兴趣的朋友不要错过
收藏 0 赞 0 分享
查看更多