File / folder chooser dialog from a Windows batch script

后端 未结 9 684
一生所求
一生所求 2020-11-22 11:58

Typically, asking the user to supply a file name to a batch script is a messy affair, requiring no misspellings, quotes around paths with spaces, and so forth. Unfortunatel

9条回答
  •  醉梦人生
    2020-11-22 12:42

    Two more ways.

    1.Using a hybrid .bat/hta (must be saved as a bat) script .It can use vbscript or javascript but the example is with javascrtipt.Does not create temp files.Selecting folder is not so easy and will require an external javascript libraries , but selecting file is easy

    
    == FILE SELECTOR==
    
        
    
    

    1.1 - without submit form proposed by rojo (see comments):

    
    == FILE SELECTOR==
    
        
    
    
    

    2.As you already using powershell/net you can create selfcompiled jscript.net hybrid.It will not require temp cs file for compilation and will directly use the built-in jscrript.net compiler.There's no need of powershell too and the code is far more readable:

    @if (@X)==(@Y) @end /* JScript comment
    @echo off
    
    :: FolderSelectorJS.bat
    setlocal
    
    for /f "tokens=* delims=" %%v in ('dir /b /s /a:-d  /o:-n "%SystemRoot%\Microsoft.NET\Framework\*jsc.exe"') do (
       set "jsc=%%v"
    )
    
    if not exist "%~n0.exe" (
        "%jsc%" /nologo /out:"%~n0.exe" "%~dpsfnx0"
    )
    
    for /f "tokens=* delims=" %%p in ('"%~n0.exe"') do (
        set "folder=%%p"
    )
    if not "%folder%" == "" ( 
        echo selected folder  is %folder%
    )
    
    endlocal & exit /b %errorlevel%
    
    */
    
    import System;
    import System.Windows.Forms;
    
    var  f=new FolderBrowserDialog();
    f.SelectedPath=System.Environment.CurrentDirectory;
    f.Description="Please choose a folder.";
    f.ShowNewFolderButton=true;
    if( f.ShowDialog() == DialogResult.OK ){
        Console.Write(f.SelectedPath);
    }
    

提交回复
热议问题