0

I have following batch script to open a file selection dialog box from which I need to get name and path of selected file(my OS is Windows 7 64Bit):

@echo off
setlocal

> "%temp%\fileSelectorDialog.vbs" (
    echo DIM objFile
    echo Set objShell = CreateObject^( "Shell.Application" ^)
    echo Do
    echo Set objFile = objShell.BrowseForFolder^(0,"Select a file",^&H4000,""^)
    echo on error resume next
    echo if objFile.Items.Item.Path = Null OR objFile is nothing OR err.number ^<^> 0 then
    echo wscript.echo "ERROR"
    echo wscript.quit
    echo end if
    echo wscript.echo objFile.ParentFolder.ParseName^(objFile.Title^).path
    echo if instr^(objFile.items.item.path,"."^)^>0 then
    echo wscript.echo objFile.Items.Item.Path
    echo wscript.quit
    echo end if
    echo Msgbox "Please try again to choose a file rather than a folder. " ^& objFile.items.item.path
    echo Loop
)

set file=ERROR
for /f "tokens=*" %%a in ('cscript //nologo "%temp%\fileSelectorDialog.vbs"') do set file=%%a
if "%file%"=="ERROR" (
echo There was an error or you cancelled
) ELSE (
echo Path chosen was %file%
)
pause

But this snippet only allows the basic "Computers" path as the initial directory to be opened when I trigger it, whereas I need to get my current directory (the directory in which script is executing) to be the initial directory when the file selection is triggered.

Can someone help me achieve this by Vbscript/Jscript or Powershell maybe ?

Vicky Dev
  • 1,893
  • 2
  • 27
  • 62

3 Answers3

1

One Batch - PowerShell hybrid file without using temp files.
This batch requires a more recent PowerShell version than the PsV2 which Win7 provides.

<# : batch portion (begins with PowerShell multi-line comment block)
:: from rojo/npocmaka http://stackoverflow.com/a/41195176/1683264
@echo off & setlocal
Set "InitialDir=%CD%"
Echo InitialDir=%InitialDir%
For /f "delims=" %%A in (
 'powershell -noprofile -NoLogo "iex (${%~f0} | out-string)"'
) Do Set "File=%%A

Echo You selected file %file%
Pause

Exit /b 

: ---------------- end batch / begin PowerShell hybrid  --------------------#>

[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $Env:initialDir
#$OpenFileDialog.filter = "Text (*.txt) | *.txt | All Files| *.*"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename

Save with .bat or .cmd extension and run - will use current dir.

In this question there is another batch-powershell hybrid by rojo which also works with PowerrShell V2.


A working solution for vbscript is provided by Rob van der woude but take care, it's the very last version down the page.

  • This script, when I execute it by clicking on it, it just opens the command window and stays there blank and the file dialog never opens at all. – Vicky Dev Aug 14 '17 at 11:01
  • Changed the batch to show the current dir, please check what is displayed when you run it. I prresume you did a complete copy/paste. Did you try on a local drive? –  Aug 14 '17 at 11:10
  • Yes Itried on local drive, but the GUI File dialog never appears, as I said before too, the Initial directory path is correct, but I don't get why it doesn't open GUI File selection dialog like the Windows OS NATIVE ones ? – Vicky Dev Aug 14 '17 at 11:16
  • I've updated my OS info in the question,if that could be helpful. – Vicky Dev Aug 14 '17 at 11:21
  • Looks like a version issue, Win 7 comes with PowerShell 2. Try this [version from rojo](https://stackoverflow.com/a/15885133/6811411) –  Aug 14 '17 at 11:42
  • Thanks man, please update your answer with reference link and some code and explanation for this version and I will accept it. Looks like VBScript doesn't support current directory in file dialog, so I have to go with Powershell-Batch Hybrid, byt the way, if you can solve the vbscript answer also, it would be very helpful, if you have worked in vbscript too. – Vicky Dev Aug 14 '17 at 11:49
  • Found/remembered a vbscript solution, see updated answer. Rob explains the drawbacks of the different vbs versions. –  Aug 14 '17 at 12:36
0

Try replacing "17^" with "", like this:

:: echo Set objFile = objShell.BrowseForFolder^(0,"Select a file",^&H4000,"")
ioverflow
  • 1
  • 2
  • But commented out for batch (not for vbs - will retrun an error) it`s of no use. Rem is valid in batch **and** vbscript for commenting out. Your suggestion is missing the escaped closing parenthesis –  Aug 14 '17 at 06:54
  • Ofcourse I removed the `rem` or `::` and checked. otherwise I wouldn't get to be able to select file, `rem` makes command disabled, whether double qwotes or no double quotes, I have commendted out that line as it wasn't working. – Vicky Dev Aug 14 '17 at 07:01
  • Please check my updated code inthe question, let me know how can I get to open my current script execution directory by default ? – Vicky Dev Aug 14 '17 at 07:06
  • I've updated my OS info in the question,if that could be helpful. – Vicky Dev Aug 14 '17 at 11:21
0

Take a look at the below VBScript example:

Option Explicit
Dim sIniDir, sFilter, sTitle, sShowInTaskBar
 
sIniDir = "C:\*"
sFilter = "All files (*.*)|*.*|Microsoft Word (*.doc;*.docx)|*.doc;*.docx"
sTitle = "GetFileDlg"
sShowInTaskBar = "yes"

MsgBox GetFileDlg(sIniDir, sFilter, sTitle, sShowInTaskBar)

Function GetFileDlg(sIniDir, sFilter, sTitle, sShow)
    ' source http://forum.script-coding.com/viewtopic.php?pid=75356#p75356
    Dim sSignature, oShellWnd, oWnd, oProc
    sSignature = Left(CreateObject("Scriptlet.TypeLib").Guid, 38)
    Set oProc = CreateObject("WScript.Shell").Exec("mshta ""about:<script>moveTo(-32000,-32000);document.title=' '</script><object id=d classid=clsid:3050f4e1-98b5-11cf-bb82-00aa00bdce0b></object><object id=s classid='clsid:8856F961-340A-11D0-A96B-00C04FD705A2'><param name=RegisterAsBrowser value=1></object><script>s.putproperty('" & sSignature & "',document.parentWindow);function q(i,f,t){return d.object.openfiledlg(i,null,f,t)};</script><hta:application showintaskbar=" & sShow & "/>""")
    On Error Resume Next
    Do
        If oProc.Status > 0 Then
            GetFileDlg = ""
            Exit Function
        End If
        For Each oShellWnd In CreateObject("Shell.Application").Windows
            Err.Clear
            Set oWnd = oShellWnd.GetProperty(sSignature)
            If Err.Number = 0 Then Exit Do
        Next
    Loop
    On Error GoTo 0
    oWnd.Document.Title = sTitle
    GetFileDlg = oWnd.q(sIniDir, sFilter, sTitle)
    oWnd.Close
End Function

Source

omegastripes
  • 12,351
  • 4
  • 45
  • 96
  • This just opens the Parent directory of current directory of script in file selection dialog, I need current directory of script to be default opened path in the file selection dialog box. – Vicky Dev Aug 14 '17 at 11:18
  • I've updated my OS info in the question,if that could be helpful. – Vicky Dev Aug 14 '17 at 11:21