当前位置: 移动技术网 > IT编程>脚本编程>VBScript > 使用VBS浏览本地文件的3种方式,获取完整路径

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

2020年03月09日  | 移动技术网IT编程  | 我要评论

尸姬玄结局,万劫成魔,项城人

因为系统组件差异,所以代码也有不同文章最后移动技术网给出一个通用的代码

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选择本地文件功能的代码就介绍到这了,需要的朋友可以参考一下。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网