VBS:如何使用特殊的FileDialog获取文件大小?

时间:2016-05-13 09:32:42

标签: vbscript

Function FileDialog() 
  Dim oExec1: Set oExec1=CreateObject("WScript.Shell").Exec( "mshta.exe ""about:" & "<" & "input type=file id=FILE" & ">" & "<" & "script>FILE.click();new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(FILE.value);close();resizeTo(0,0);" & "<" & "/script>""" )
  Dim sPathfile: sPathfile = oExec1.StdOut.ReadAll
  sPathfile = Replace( sPathfile, vbCRLF, "" )
  FileDialog = sPathfile
End Function

更改为: 函数FileDialog(filesize)

filesize将返回所选文件的字节值

1 个答案:

答案 0 :(得分:0)

阅读Function Statement (VBScript)文档,并按参考>传递参数,如下所示:

option explicit
On Error GoTo 0
Dim strResult: strResult = Wscript.ScriptName
Dim nFileSize: nFileSize = -1      ' for test that `byRef` declaration works 

strResult = strResult & vbNewLine & FileDialog( nFileSize)
strResult = strResult & vbNewLine & nFileSize

Wscript.Echo strResult
Wscript.Quit

Function FileDialog( byRef xSize) 
  Dim oExec1
  Set oExec1=CreateObject("WScript.Shell").Exec( "mshta.exe ""about:" & "<" & "input type=file id=FILE" & ">" & "<" & "script>FILE.click();new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(FILE.value);close();resizeTo(0,0);" & "<" & "/script>""" )
  Dim sPathfile: sPathfile = oExec1.StdOut.ReadAll
  sPathfile = Replace( sPathfile, vbCRLF, "" )
  If Len(sPathfile) > 0 Then
    With CreateObject("Scripting.FileSystemObject").GetFile(sPathfile)
      xSize = .Size                  ' the size, in bytes, of the specified file
    End With 
  Else
    ' No file chosen: red "×" or Esc or Cancel pressed
    xSize = 0
  End If
  FileDialog = sPathfile
End Function

阅读ByRef and ByVal Parameters文章。