Search code examples
batch-filecmdtext-filesuser-input

Opening a folder for user input using Batch File


I have to extract the names of files from a folder and paste them to a text document (Word document would be ideal but I found a way to link and automatically update a word file with the contents of a text file).

What I was thinking of doing was to have a dialog box open up to ask the user a path to the folder. Then I would use the dir function and paste the values to a text file.

I can get the names but the batch file has to be in the same folder. I wanted the dialog box, is there a way to do that? I didn't really want the user to type in the entire folder path in cmd prompt.


Solution

  • This what you need as code to browse for folder and choose it :

    Browse4Folder.bat is used from this batch file Local_Batch_Engine.bat

    @echo off
    Title Browse4Folder
    Color 0A
    Call :Browse4Folder "Choose source folder to scan" "c:\scripts"
    echo You have chosen this location "%Location%"
    pause & exit
    ::***************************************************************************
    :Browse4Folder
    set Location=
    set vbs="%temp%\_.vbs"
    set cmd="%temp%\_.cmd"
    for %%f in (%vbs% %cmd%) do if exist %%f del %%f
    for %%g in ("vbs cmd") do if defined %%g set %%g=
    (
        echo set shell=WScript.CreateObject("Shell.Application"^) 
        echo set f=shell.BrowseForFolder(0,"%~1",0,"%~2"^) 
        echo if typename(f^)="Nothing" Then  
        echo wscript.echo "set Location=Dialog Cancelled" 
        echo WScript.Quit(1^)
        echo end if 
        echo set fs=f.Items(^):set fi=fs.Item(^) 
        echo p=fi.Path:wscript.echo "set Location=" ^& p
    )>%vbs%
    cscript //nologo %vbs% > %cmd%
    for /f "delims=" %%a in (%cmd%) do %%a
    for %%f in (%vbs% %cmd%) do if exist %%f del /f /q %%f
    for %%g in ("vbs cmd") do if defined %%g set %%g=
    goto :eof
    ::***************************************************************************