【问题标题】:vbscript to select file and save directory?vbscript 选择文件并保存目录?
【发布时间】:2015-10-17 19:39:23
【问题描述】:

我目前正在开发一个可以启动其他程序的程序。它是批量的。但我需要它对用户友好。我想要一个可以打开文件选择窗口的代码,并将我选择的目录保存到 txt 文件(或 csv 或其他文件)中。我是 VBS 的新手,如果我遗漏了一些简单的东西,请原谅我。但是我搜索了一段时间并接近了,结果失败了。

这是我目前所拥有的......

Set shell = CreateObject( "WScript.Shell" )

defaultLocalDir = shell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Desktop"
Set shell = Nothing

file = ChooseFile(defaultLocalDir)


wscript.echo file


Function ChooseFile (ByVal initialDir)
    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")

    Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
    Dim winVersion

    ' This collection should contain just the one item
    For Each objItem in colItems
        'Caption e.g. Microsoft Windows 7 Professional
        'Name e.g. Microsoft Windows 7 Professional |C:\windows|...
        'OSType e.g. 18 / OSArchitecture e.g 64-bit
        'Version e.g 6.1.7601 / BuildNumber e.g 7601
        winVersion = CInt(Left(objItem.version, 1))
    Next
    Set objWMIService = Nothing
    Set colItems = Nothing

    If (winVersion <= 5) Then
        ' Then we are running XP and can use the original mechanism
        Set cd = CreateObject("UserAccounts.CommonDialog")
        cd.InitialDir = initialDir
        cd.Filter = "ZIP files|*.zip|Text Documents|*.txt|Shell Scripts|*.*sh|All Files|*.*"
        ' filter index 4 would show all files by default
        ' filter index 1 would show zip files by default
        cd.FilterIndex = 1
        If cd.ShowOpen = True Then
            ChooseFile = cd.FileName
        Else
            ChooseFile = ""
        End If
        Set cd = Nothing    

    Else
        ' We are running Windows 7 or later
        Set shell = CreateObject( "WScript.Shell" )
        Set ex = shell.Exec( "mshta.exe ""about: <input type=file id=X><script>X.click();new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(X.value);close();resizeTo(0,0);</script>""" )
        ChooseFile = Replace( ex.StdOut.ReadAll, vbCRLF, "" )

        Set ex = Nothing
        Set shell = Nothing
    End If
End Function    

【问题讨论】:

  • 失败怎么办?你有错误吗?
  • 失败,因为它不是我想要的,或者它的命令与 win 7 不兼容...
  • 并且代码是与批处理文件一起使用的,实际上是一个文本编辑器,我正在制作......对不起,我不够清楚......
  • @lukpoper “失败,因为它不是我想要的......” -- 好的......但是我们怎么知道你在寻找什么?你需要一个清晰的问题陈述。 “这给了我错误 123”或“这不会在应该创建文件的时候创建文件”。

标签: batch-file vbscript


【解决方案1】:

也许您正在寻找类似下一个bat code sn-p 的东西?

@echo OFF
SETLOCAL enableextensions
set "_chosen="
if exist "_saved.txt" (
   rem read previously saved value
   <"_saved.txt" set /P "_chosen="
   echo read previously saved value
) else (
  for /f "delims=" %%G in ('
       cscript //NOLOGO "D:\VB_scripts\SO\31656148.vbs"
    ') do (
      set "_chosen=%%G"
      rem save value to a file
      >"_saved.txt" echo(%%G
      echo file chosen, value saved
  )
)
if defined _chosen (
    echo("%_chosen%"
) else (
    echo no file chosen
)

【讨论】:

  • 它只是说“磁盘驱动器中没有磁盘”
  • 看起来这将是一个拖放程序,我正在寻找类似于您在网络上单击“浏览”时获得的内容。
  • @lukpoper 已调整。请删除"_saved.txt" 并再次运行。您的 VBScript 工作正常,带来标准的 Windows Choose file to upload 对话框...cscript //NOLOGO "D:\VB_scripts\SO\31656148.vbs" 的输出是什么(将脚本路径和名称调整为您的)?
  • 我会尽快尝试一下,在假期里,所以我要到明天才能使用电脑。
  • 大家好,请给我一些回复,但我的电脑当然中了病毒,我把它发给了朋友,因为它真的很糟糕。我明天将尝试代码。对不起。
猜你喜欢
  • 2018-07-15
  • 1970-01-01
  • 2016-12-17
  • 1970-01-01
  • 1970-01-01
  • 2021-10-26
  • 1970-01-01
  • 1970-01-01
  • 2015-04-22
相关资源
最近更新 更多