1

I'm totally new to scripting. For the following example of codes written in batch file:

@ECHO OFF
ECHO ----------------------------------------------------------------------------------------------------
SET /P URL="[Enter video URL] "
ECHO ----------------------------------------------------------------------------------------------------
goto formatList

:formatList
ECHO.
ECHO ----------------------------------------------------------------------------------------------------
youtube-dl -F %URL%
ECHO ----------------------------------------------------------------------------------------------------
goto selection

:selection
ECHO.
ECHO ----------------------------------------------------------------------------------------------------
ECHO a) Video + Audio
ECHO b) Single format (Audio only / Video only)
ECHO.
SET /P option="Select option: "
if %option% == a (goto download)
if %option% == b (goto downloadSingle)
ECHO.
ECHO Unknown value
ECHO ----------------------------------------------------------------------------------------------------
goto selection

:download
ECHO ----------------------------------------------------------------------------------------------------
ECHO.
ECHO ----------------------------------------------------------------------------------------------------
SET /P video="Select video format: "
SET /P audio="Select audio format: "
SET /P location="Specify download location: "
ECHO.
youtube-dl --write-sub --embed-subs -o %%location%%/%%(title)s.%%(ext)s -f %video%+%audio% -i --ignore-config --hls-prefer-native %URL% 
ECHO ----------------------------------------------------------------------------------------------------
ECHO.
PAUSE
EXIT

:downloadSingle
ECHO ----------------------------------------------------------------------------------------------------
ECHO.
ECHO ----------------------------------------------------------------------------------------------------
SET /P format="Select format: "
ECHO.
youtube-dl --write-sub --embed-subs -o %%location%%/%%(title)s.%%(ext)s -f %%format%% -i --ignore-config --hls-prefer-native %URL% 
ECHO ----------------------------------------------------------------------------------------------------
ECHO.
PAUSE
EXIT

How to instead of having to type the address of folder path via 'SET /P location="Specify download location:"', have the batch file open up File Browser to select a folder and set it in the %location% variables.

Any kind of help is greatly appreciated.

  • The simplest you coud do in pure batch is open explorer.exe and use set /P to allow the user to drop and drag the file into the console to be assigned to the variable. – T3RR0R Feb 24 '21 at 14:02
  • 2
    You could use another scripting language to assist you, PowerShell and Windows Scripting Host are both included as part of Windows, and can both be run from a batch file. But then for the task you're trying to achieve, I'd probably advise that you cut out the middle man, (drop the batch file), and do it directly in `.JS` `.PS` or `.VBS`. – Compo Feb 24 '21 at 14:36
  • In the future please consider providing a [mcve]. And I am stressing minimal. The majority of your code has nothing to do with your question. – Squashman Feb 24 '21 at 15:31
  • My apology. Points noted. – Muhammad Firdaus Nata Feb 24 '21 at 16:39

1 Answers1

0

Here is an example using a vbscript into a batch file :

@echo off
Title Browse a Folder and select it
Call :BrowseFolder "Select the Source folder" "C:\Program"
Set "LocationFolder=%MyFolder%"
echo "%LocationFolder%"
Pause & Exit
::-----------------------------------------------------------
:BrowseFolder
    set MyFolder=
    set vbs="%temp%\_.vbs"
    set cmd="%temp%\_.cmd"
    >%vbs% echo set WshShell=CreateObject("WScript.Shell") 
    >>%vbs% echo set shell=CreateObject("Shell.Application") 
    >>%vbs% echo set f=shell.BrowseForFolder(0,%1,0,%2) 
    >>%vbs% echo if typename(f)="Nothing" Then  
    >>%vbs% echo wscript.echo "set MyFolder=Dialog Cancelled" 
    >>%vbs% echo WScript.Quit(1)
    >>%vbs% echo end if 
    >>%vbs% echo set fs=f.Items():set fi=fs.Item() 
    >>%vbs% echo p=fi.Path:wscript.echo "set MyFolder=" ^& p
    cscript //nologo %vbs% > %cmd%
    @for /f "delims=" %%a in (%cmd%) do %%a
    @for %%f in (%vbs% %cmd%) do if exist %%f del %%f
    @for %%g in ("vbs cmd") do if defined %%g set %%g=
Exit /B
::-----------------------------------------------------------

Here is another code with Powershell and Batch

:: fchooser.bat
:: launches a folder chooser and outputs choice to the console
:: https://stackoverflow.com/a/15885133/1683264

@echo off
Title Browse a Folder and select it with Powershell and Batch
setlocal
set "psCommand="(new-object -COM 'Shell.Application')^
.BrowseForFolder(0,'Please choose a folder.',0,0).self.path""
for /f "usebackq delims=" %%I in (`powershell %psCommand%`) do set "folder=%%I"
setlocal enabledelayedexpansion
echo You chose !folder!
pause
endlocal
Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • @MuhammadFirdausNata [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – Hackoo Feb 25 '21 at 12:19