簡體   English   中英

vbscript選擇文件並保存目錄?

[英]vbscript to select file and save directory?

我目前正在開發一個可以啟動其他程序的程序。 它是批量的。 但是我需要它對用戶友好。 我想要一個可以打開文件選擇窗口的代碼,並將我選擇的目錄保存到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    

也許您正在尋找類似下一個bat代碼片段的東西?

@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
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM