I want to build a batch program to work with youtube-dl, and I want to ask some questions (choice command I think it should be) and depending on answers to build query which will be executed by main youtube-dl command.
Example:
echo Do you want to extract audio from this clip? (y/n)
echo Write subtitle file? (y/n)
And do this at the end:
youtube-dl -x --write-sub some link
I hope I was clear, english is not my first language. Thanks
Try this:
#echo off
setlocal
:Input
set opt1=
set opt2=
set /p opt1=Do you want to extract audio from this clip (y/n)
if not defined opt1 goto :Invalid
if .%opt1% EQU . goto :Invalid
echo %opt1%|findstr /i /r /c:"y" /c:"n">nul||goto :Invalid
set /p opt2=Write subtitle file (y/n)
if not defined opt2 goto :Invalid
if .%opt1% EQU . goto :Invalid
echo %opt2%|findstr /i /r /c:"y" /c:"n">nul||goto :Invalid
if /i %opt1% EQU Y set opts=-x
if /i %opt2% EQU Y set opts=%opts% --write-sub
echo youtube-dl %opts% some link
goto :eof
:Invalid
Echo You have entered an invalid option. Please enter only y or n
goto :Input
Related
I have a batch-game where there's some questions for one person in particular, I always change the file when she(the person) isn't nearby so its kind of a surprise.
The thing is I keep on displaying questions of different types but sometimes the GOTO or IF statements just simply doesn't work properly, it just follow the line or exit the file.
#ECHO OFF
setlocal EnableDelayedExpansion
ECHO.
ECHO "i just skipped all this echo for you"
pause>nul
:choice
cls
set /p c=Question 1[y/n]
if /I "%c%" EQU "Y" goto :somewhere
if /I "%c%" EQU "N" goto :somewhere_else
goto :choice
:somewhere
cls
cd "-somewhere in my pc-"
start "-the file-"
cls
goto :somewhere1
:somewhere1
cls
echo.
echo "skip"
pause>NUL
goto :somewhere2
:somewhere2
cls
echo.
set /p c1=question 2
if /I "!c1!"== "option1" goto :correct
if not /I "!c1!"== "option1" goto :somewhere_else1
:correct
cls
echo.
echo "skip"
echo.
echo enter to exit
pause>nul
exit
:somewhere_else1
cls
echo.
echo bad answer
pause>nul
goto :somewhere2
Here are my problems:
When it ask's you the question
:choice
cls
set /p c=Question 1[y/n]
if /I "%c%" EQU "Y" goto :somewhere
if /I "%c%" EQU "N" goto :somewhere_else
goto :choice
it's just work properly, it wasn't working in the past but I magically fixed it with no idea what i'm doing.
but then I just wanted to make it bigger and add the next lines:
:somewhere2
cls
echo.
set /p c1=question 2
if /I "!c1!"== "option1" goto :correct
if not /I "!c1!"== "option1" goto :somewhere_else1
and now the file is executing perfectly the ":correct" part but no the ":somewhere_else1". in the case of ":somewhere_else1" its just exit the file. in the past was exiting the file the right one too but again I fixed it just writing it again.
The problem is your line if not /I "!c1!"== "option1" goto :somewhere_else1
The /I needs to be before not
if /I not "!c1!"== "option1" goto :somewhere_else1
Though I should also mention that your use of ! instead of % is unnecessary in this example.
You should have a read through this link to get some insight into how to fix these errors yourself in the future, and perhaps this link on good practice.
#echo off
title Music
color 0f
:main
cls
echo Welcome to you Music!
echo.
set /p var=What would you like to do:
if %var%==find goto find
if %var%==play goto play
goto main
:find
cls
set /p song=What song would you like me to check for (Case Sensitive):
if exist music\"*%song%*.mp3" goto exist
if not exist music\"*%song%*.mp3" goto notExist
:exist
cls
echo %song% is here.
echo.
pause
goto main
:notExist
cls
echo %song% is not here. Check the spelling and letter case for errors.
echo.
pause
goto main
:play
cls
set /p song=Enter the name of a song to play:
if exist music\"*%song%*.mp3" goto play2
if not exist music\"*%song%*.mp3" goto notExist
goto main
:play2
cls
set songName = [file name]
:playing
:: Will put more code later.
The purpose of this program is to search my music for a specific song and be able to play it from command line. I will later add code to control the music but for now I just want to be able to find the song and play it. I am using the '' so someone could enter "Another Brick in the Wall" and let it find "Another Brick in the Wall Part 2" The issue is when saving a variable, it can not use '' or they will be saved as part of the string.
your question isn't very clear. The following should answer what I guessed from your question and comments:
set /p "song=What song would you like me to check for (Case insensitive): "
REM if you are afraid of qoutes entered by the user, remove them:
set song=%song:"=%
<nul set /p "=Songs found: "
dir /b "music\*%song%*.mp3"|find /c /v ""
echo the names are:
dir /b "music\*%song%*.mp3"
echo executing them...
for /f "delims=" %%i in ('dir /b /s "music\*%song%*.mp3"') do (
echo ... executing %%i
REM whatever you use to play it
)
I can use ERRORLEVEL, but tried and with a loop it failed.
I am writing a batch "shell."
Since I have tried and tried, I am finally asking for help.
The reason I don't want to use errorlevel is because the loop.
(FULL) SHELL
#set /p build=<"C:\Users\%username%\Desktop\WellOS2\WellOS\Build".txt
#title WellOS V.%build%
#echo off
goto boot
:register
cls
echo You are registering...
echo If this is an error press CTRL + C NOW...
pause
cls
set /p user= Enter your username:
set /p passwordreg= Enter your password:
mkdir "C:\Users\%username%\Desktop\WellOS2\Users\%user%"
mkdir "C:\Users\%username%\Desktop\WellOS2\Users\%user%\Documents"
echo %passwordreg% >"C:\Users\%username%\Desktop\WellOS2\Users\%user%\password".txt
echo 2 >"C:\Users\%username%\Desktop\WellOS2\OSfiles\bootset".txt
echo Your done.
pause
goto welloslog
:booterror
echo Sorry the boot file has an error. Check the user manual for BOOT$
pause
:boot
set /p boot=<"C:\Users\%username%\Desktop\WellOS2\OSfiles\bootset".txt
if %boot% == 1 goto register
if %boot% == 2 goto welloslog
goto booterror
cls
:ERROR
cls
echo ----------ERROR-------------------
echo %error%
pause
goto %back%
:welloslog
cls
echo Welcome to WellOS2!
echo ----------------LOGIN-------------
set /p user= Username:
if exist "C:\Users\%username%\Desktop\WellOS2\Users\%user%" goto pass
set error= Sorry that account doesn't exist.
set back=welloslog
goto welloslogerror
:pass
set /p password=<"C:\Users\%username%\Desktop\WellOS2\Users\%user%\password".txt
set /p passwordlog= Password:
if /i %passwordlog% == %password% goto wellos
set error= Sorry! wrong password.
set back= welloslog
goto error
:wellos
cls
:wellosnocls
echo --------------MAIN---------------
echo type help for help
set /p command= #:
if exist "C:\Users\%username%\Desktop\WellOS2\Programdata\%command%.sys" set type=sys
if exist "C:\Users\%username%\Desktop\WellOS2\Programdata\%command%.pro" set type=pro
if exist "C:\Users\%username%\Desktop\WellOS2\Programdata\%command%.sys" goto po
if exist "C:\Users\%username%\Desktop\WellOS2\Programdata\%command%.pro" goto po
set error= !Unreconized program/system program!
set back=wellos
goto error
:po
set lines=0
echo --------------%command%.%type%---------------
:porep
set /a lines=%lines% + 1
set /p "code="<"C:\Users\%username%\Desktop\WellOS2\Programdata\%command%.%type%\%command%.%type%-%lines%".wellcode
if "%code%"=="GOWELL" goto wellosnocls
findstr /I /L "if" "C:\Users\%username%\Desktop\WellOS2\Programdata\%command%.%type%\%command%.%type%-%lines%.wellcode"
:skip
call %code%
goto porep
::Tools
:iftl
%code%
goto porep
PROGRAM OPENER (What I am talking about, and having problems with...)
:po
set lines=0
echo --------------%command%.%type%---------------
:porep
set /a lines=%lines% + 1
set /p "code="<"C:\Users\%username%\Desktop\WellOS2\Programdata\%command%.%type%\%command%.%type%-%lines%".wellcode
if "%code%"=="GOWELL" goto wellosnocls
findstr /I /L "if" "C:\Users\%username%\Desktop\WellOS2\Programdata\%command%.%type%\%command%.%type%-%lines%.wellcode" goto iftl
:skip
call %code%
goto porep
::Tools
:iftl
%code%
goto porep
findstr "targetstring" datafilename >flagfilename
for %%a in (flagfilename) do if %%~za==0 echo not found
for %%a in (flagfilename) do if %%~za neq 0 echo found
beyond that, your question is too vague.
The following command returns all lines of a text file textfile.txt that contain the word word (remove the /I switch if you want the search to be case-sensitive):
findstr /I /L "word" "textfile.txt"
With for /F you can capture the output and test whether it is empty, as the loop does not iterate if no match is encountered:
set "FOUND="
for /F "delims=" %%F in ('
findstr /I /L "word" "textfile.txt"
') do (
set "FOUND=Yes"
)
if defined FOUND (
echo One or more matches found.
rem do something...
) else (
echo No match found.
rem do something else...
)
Type for /? and if /? in command prompt to get details about the used commands.
There is also a way to use ErrorLevel implicitly, meaning you do not have to query its value by something like %ErrorLevel%, !ErrorLevel! or if ErrorLevel, namely when using conditional command separators:
the && separator executes the following command only in case the previous one succeeded, that is, it returned an ErrorLevel of 0; (findstr returns 0 in case a match is encountered;)
the || separator executes the following command only in case the previous one failed, that is, it returned an ErrorLevel other than 0; (findstr returns a non-zero ErrorLevel in case no match is encountered;)
The following line of code demonstrates the usage:
findstr /I /L "word" "textfile.txt" && (echo One or more matches found.) || echo (No match found.)
This question already has an answer here:
Variables are not behaving as expected
(1 answer)
Closed last year.
i have written this batch file code but it has this error i appreciate u if answer me .
the error is that when i choose 2 it gone to ask me to enter desire site but when i write the address it has an error says ECHO IS OFF
HELP ME WHAT SHOULD I DO...
#echo off
title trace
:main
echo 1)TRACE GOOGLE
echo 2)TRACE YOUR SITE
set /p choice= Enter your choice:
echo %choice%
if %choice%==1 (
tracert www.google.com
goto main
pause >nul
)
if %choice%==2 (
set /p s=Enter your desired site:
echo %s%
pause >nul
)
pause >nul
It's not an error message, it's what happens when you run echo without arguments. In other words, you see this because %s% winds up being empty.
To fix your problem use enabledelayedexpansion as in a block % will expand to the value prior to the block:
#echo off
setlocal enabledelayedexpansion
title trace
:main
echo 1)TRACE GOOGLE
echo 2)TRACE YOUR SITE
set /p choice= Enter your choice:
echo %choice%
if %choice%==1 (
tracert www.google.com
goto main
pause >nul
)
if %choice%==2 (
set /p s=Enter your desired site:
echo !s!
pause >nul
)
pause >nul
Which should work
When you have #echo off at the begening, and you echo a empty var, would get this message. Try to check your echoed vars, ensure them not empty.
So here's my ENTIRE code:
#echo off
cls
color fc
:Start
cls
echo Welcome to -{GAMELOADER}-
set/p u=Username:
if %u%==username goto Correct1
if not %u%==username goto Incorrect
:Incorrect
cls
echo You Have Entered Incorrect Pass And/Or Username!
set/p t=Try Again? (Y/N)
if %t%==Y goto Start
if %t%==y goto Start
if %t%==N goto Quit
if %t%==n goto Quit
:Correct1
set/p p=Password:
if %p%==password goto Open
if not %p%==password goto Incorrect
:Open
cls
echo Games:
echo ------------------------
echo [1]Kerbal Space Program
echo ------------------------
set/p g=Choice:
if %g%== 1 goto KSPEnd
:KSPEnd
start "" "C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program\KSP.exe"
cls
goto Quit
:Quit
cls
echo Goodbye
Timeout 1
But the code opens the .exe AND a .txt file with exactly the same name. I can't rename the files. So basically i'm asking how to open a specific file type.
Thanks
Instead of starting C:\....\KSP.exe, first go to the right directory, then start KSP:
cd "C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program"
KSP.exe
Ok I've got two things for you. Firstly I'll give you you desired solution.
Treat it like an operatable program
rem To start Kerbal Space Program:
set Path=C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program;%Path%
start KSP
Thats it. Really.
Secondly:
Use the Choice command
you keep on using set /p where choice would be much better.
Just to be convenient I redid you code with everything I would do. Have fun!
Code :
#echo off
cls
color fc
title -{GAMELOADER}-
:Start
echo Welcome to -{GAMELOADER}-
set/p u=Username:
if %u%==username goto Correct1
if not %u%==username goto Incorrect
set Er=Userid
goto :Crash
:Incorrect
cls
echo You Have Entered Incorrect Pass And/Or Username!
choice /c yn /m "Try Again?"
if %errorlevel%==1 goto Start
if %errorlevel%==2 goto Quit
set Er=Loop-End_of_stream
goto :Crash
:Correct1
set/p p=Password:
if %p%==password goto Open
if not %p%==password goto Incorrect
set Er=Passid
goto :Crash
:Open
cls
echo Games:
echo ------------------------
echo [1]Kerbal Space Program
echo ------------------------
echo.
Choice /c 1 /m "Game: "
if %errorlevel%==1 goto KSPEnd
set Er=Gameid
goto :Crash
:KSPEnd
set Path=C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program;%Path%
start KSP
goto Quit
set Er=End_of_file___UNKNOWN
goto :Crash
:Quit
cls
echo Goodbye
Timeout 1
Exit
:Crash
Rem Always useful :)
Echo Program has crashed Error: %Er%
Pause
Exit
Hope that helped. Mona