简体   繁体   中英

vbscript to select file and save directory?

I'm currently working on a program that can start other Programs. Its in batch. But i need it to be user friendly. I want a code that can open a file select window, and save the directory of what i selected into a txt file (or csv or whatever). I'm new on VBS so forgive me if I'm missing something simple. But i searched for a while and got close, only to fail.

Here's what i have so far...

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    

Maybe you are looking for something like next bat code snippet?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM