Opening URLs with user input in CMD Batch File - batch-file

I'm a Static Web Dev. & I Don't know much about CMD & Batch Files.
But I need to make a batch file that does this:It first prompts user to enter a specific word & there are some links that have URL & a some kind of name. then if user's input matches one of the link's names, The URL opens in a browser (the default one). I think it is like this with HTML+JS:
<!doctype html>
<html>
<head>
<script>
function input(){
var stackoverflow = "http://stackoverflow.com";
var val = document.getElementById("input").value;
if (val = stackoverflow){
window.open(stackoverflow);
}
}
</script>
</head>
<body>
<input type="text" id="input" onKeyUp="input()"/>
</body>
</html>
but there should be more options & I think it should be done with START command.
Any help is appreciated.THX

What you are looking for is a simple menu,
:: turn off "verbose" command writeback.
#echo off
:: write a simple list of options to console,
:main
echo Options;
echo 1 : StackOverflow
echo 2 : Google
echo 3 : Youtube
echo 4 : This question
:: Prompt for input,
set /p "strMenu=Enter desired URL number:"
:: Compare input through if commands,
:: `if not defined strMenu goto :menu` can be used here if prefered.
if "%strMenu%" equ "1" start "" "https://www.stackoverflow.com"
if "%strMenu%" equ "2" start "" "https://www.Google.com"
if "%strMenu%" equ "3" start "" "https://www.youtube.com"
if "%strMenu%" equ "4" start "" "http://stackoverflow.com/questions/35945614/opening-urls-with-user-input-in-cmd-batch-file"
exit
Note the start command will use the default assigned browser, but this can be set with something like;
start "" "chrome" "URL"
Numbers are used for laziness in having to type more than 1 character before hitting enter.

Related

First simple batch file ever

I'm making a batch file with a menu that with options, type in an option and it opens multiple web pages at once. I can get it to open one web page but how do I get it to open several. So far I have:
For example, I want to hit "g" and have it open google and gmail and another website.
:: turn off "verbose" command writeback.
#echo off
:loop
:: write a simple list of options to console,
:main
echo Options;
echo f : facebook
echo g : google
:: Prompt for input,
set /p "strMenu=typey:"
:: Compare input through if commands,
:: `if not defined strMenu goto :menu` can be used here if prefered.
if "%strMenu%" equ "f" start "" "https://www.facebook.com"
if "%strMenu%" equ "g" start "" "http://www.google.com"
goto loop
start "" "www.google.com" opens google in a new tab in the default browser (when it's already open, of course - else it just opens the browser with that one tab) (at least it does for me (IE11) - different browsers may behave different here), so this should open three tabs in one browser:
if /i "%strMenu%" equ "f" (
start "" "https://www.facebook.com"
start "" "https://www.google.com"
start "" "https://www.yahoo.com"
)
You should also consider the use of choice instead of set /p (no need to press ENTER)

*.HTA with batch file user entered variables

I want to create a HTA that uses a batch file, when a user fills in the textboxes it will pass the data to the batch file.
My batch file is :
//batch
#ECHO OFF
CD "h:\tools\ffmpeg\bin"
set /p input="Enter input file: "
set /p output="Enter output file: "
ffmpeg.exe -i %input% %output%
I want to pass the input and output from the HTA to the batch file
set /p input="enter input"
set /p output="enter output"
%input% %output%
Pretty sure you don't need to use a batch file at all for this.
Something like this should work:
Set Shell = CreateObject("WScript.Shell")
Shell.run "h:\tools\ffmpeg\bin\ffmpeg.exe -i " & Input & " " & Output
Note - I am just guessing at the variable names (Input and Output) you are using in the HTA, so you will have to rename those to whatever the variables are that contain the inputs you originally wanted to pass through to the batch file. Also only guessing that it will be in VBScript...
EDIT:
To answer the comment more completely, something like this:
<html>
<head>
<title>HTA Test</title>
<HTA:APPLICATION
ID="objTest"
APPLICATIONNAME="HTATest"
SCROLL="yes"
SINGLEINSTANCE="yes"
>
</head>
<SCRIPT LANGUAGE="VBScript">
Sub TestSub
Set Shell = CreateObject("WScript.Shell")
Shell.run "h:\tools\ffmpeg\bin\ffmpeg.exe -i "& InputTextbox.Value & " " & OutputTextbox.Value
End Sub
</SCRIPT>
<body>
Input : <input type="text" name="InputTextbox" size="30"><P>
Output: <input type="text" name="OutputTextbox" size="30"><P>
<input id=runbutton type="button" value="Run Script" name="run_button" onClick="TestSub">
</body>
Note for people actually trying to pass arguments to a batch file from HTA:
Assuming you've taken input from the user already you can pass it to the batch file (just like the parameters are passed to the EXE above), but you wouldn't need the set /p lines. Instead you would reference input and output as %1 and %2 in the batch file.

how to open website link or folder location stored in variable using start command in batch scripting

I want to create a batch file that takes input as website name or folder location as input as opens site in browser or location in windowsexplorer respectively.
But whenever it runs, it just opens another command prompt. Please help.
Here's the code:
#echo off
:up
echo 1. Windows Explorer
echo 2. Website
echo 3. Exit
set /p ch=Enter choice for app to open:
if %ch%==1 (
set /p pth=Enter full path of folder:
start "%pth%"
pause
goto up
)
if %ch%==2 (
set /p site=Enter name of website:
start "%site%"
pause
goto up
)
Found the solution
Just had to add double quotes after start as I'm using double quotes to specify the address
Like this:
start "" "%site%"

batch set /p "id= type id: " not working

I have this in my batch file
set /p "id=type id: "
echo you typed %id%
pause
but echo just prints you typed and NOTHING else.
I have looked around the forum, and tried := %=% = but nothing makes any difference
Your code works as it!
Try it, paste it to an empty file and it works.
But I assume it's only a part of a bigger batch file, inside a code block.
That is the cause that it fails.
You could use delayed expansion
setlocal EnableDelayedExpansion
(
set /p "id=type id: "
echo you typed !id!
)

Is there anyway to have preset data for user input in a batch file?

So basically I have a batch file that requires alot of user input. I was wondering if it was possible to have any filler data already present when the question is asked, and if the user needs to change something, they can go edit that data. For example
And then the user enter their first and last name.
But is it possible to start with a default name that the user can go back and edit if they need?
This probably isn't necessary, But this is the code I use for the user input.
Set /p "Author=Please enter your name: "
And I understand for the Author it wouldn't make much sense to have preset data, but there are other instances where it would be useful for me to do this. Is it possible?
nearly impossible to edit a preset value with pure batch, but you can easily give a default value (works, because set /p is not touching the variable, if input is empty)
set "author=First Last"
set /p "author=Enter name or press [ENTER] for default [%author%]: "
echo %author%
The method below have the inconvenience that the screen must be cleared before the user enter the data, but I am working trying to solve this point:
EDIT: I fixed the detail of the first version
#if (#CodeSection == #Batch) #then
#echo off
rem Enter the prefill value
CScript //nologo //E:JScript "%~F0" "First Last"
rem Read the variable
echo -----------------------------------------------------------
set /P "Author=Please enter your name: "
echo Author=%Author%
goto :EOF
#end
WScript.CreateObject("WScript.Shell").SendKeys(WScript.Arguments(0));
For further details, see this post.
You can set the var first and then prompt the user only if it's not defined like so:
set Author=First Last
if not defined Author set /p "Author=Please enter your name: "
You can also do this backwards where you can define a value if the user didn't define it, like so:
set /p "Author=Please enter your name: "
if not defined Author set Author=First Last
There Is another way yet to achieve this. It uses vbs script to get input and assign it to a variable, -The script can be created within and called from .bat files.
I developed this method as an alternative to accepting user input from set /p in order to validate input and prevent setting of variables with spaces or special characters.
*Validation methods omitted as does not relate to the question
Best method is to have the script generator as a secondary .bat file that you can call, as it allows for a lot of versatility regarding the information the vbs input box conveys, as well as being able to be used in any circumstance within one or more .bat files you want to be able to control input defaults with.
In your main program, when Preparing to get input-
REM Define title:
Set inputtitle=The title you want the input box to have
REM Declare variable to be assigned:
Set variableforinput=VariableName
REM Define the default Variable Value:
Set defaultinput=VariableName's Desired Default Value
REM getting the Input:
CALL "inputscript.bat" %inputtitle% %variableforinput% %defaultinput%
inputscript.bat:
#echo off
SET inputtitle=%~1
SET variableforinput=%~2
SET defaultinput=%~3
SET %variableforinput%=
SET input=
:main
REM exit and cleanup once variable is successfully defined
IF DEFINED input GOTO return
REM this creates then starts the VBS input box, storing input to a text file.
(
ECHO Dim objFSO 'File System Object
ECHO Set objFSO = CreateObject("Scripting.FileSystemObject"^)
ECHO Dim objTS 'Text Stream Object
ECHO Const ForWriting = 2
ECHO Set objTS = objFSO.OpenTextFile("%userprofile%\getinput.txt", ForWriting,
True^)
ECHO objTS.Write(InputBox("Please enter %variableforinput% to continue.","%inputtitle%","%defaultinput%",0,0^)^)
ECHO objTS.Close(^)
ECHO Set bjFSO = Nothing 'Destroy the object.
ECHO Set objTS = Nothing 'Destroy the object.
) >GetInput.vbs
START GetInput.vbs
REM a pause that allows time for the input to be entered and stored is required.
cls
ECHO (N)ext
CHOICE /T 20 /C nz /N /D n >nul
IF ERRORLEVEL ==2 GOTO main
IF ERRORLEVEL ==1 GOTO loadinput
:loadinput
IF NOT EXIST "%userprofile%\getinput.txt" GOTO invInput
<%userprofile%\getinput.txt (
SET /P input=
)
IF NOT DEFINED input GOTO invInput
REM opportunity for other validation of input before returning to main.
GOTO main
:invInput
SET input=
IF EXIST "GetInput.vbs" (
DEL /Q "GetInput.vbs"
)
REM ends the vbs script ready for the next attempt to provide input
taskkill /pid WScript.exe /T >nul
Timeout 1 >nul
GOTO main
REM assigns the input value to the variable name being set in Your Main program.
:return
SET %variableforinput%=%input%
SET input=
IF EXIST "%userprofile%\getinput.txt" (
DEL /Q "%userprofile%\getinput.txt"
)
IF EXIST "GetInput.vbs" (
DEL /Q "GetInput.vbs"
)
GOTO :EOF
I wrote an open-source Windows console program called editenv that replaces my older editv32/editv64/readline.exe utilities:
https://github.com/Bill-Stewart/editenv
Basically, editenv lets you interactively edit the value of an environment variable. One of my common use cases is to edit the Path environment variable for the current process:
editenv Path
editenv also provides the following convenience features:
--maskinput allows you to hide the typed input (note that this feature does not provide any encryption or security)
--allowedchars and --disallowchars allow you to specify which characters are allowed for input
--minlength and --maxlength let you choose a minimum and maximum length of the input string
--timeout lets you specify a timeout after which input is entered automatically
The most recent binaries are available here:
https://github.com/Bill-Stewart/editenv/releases
askingFile.cmd < response.txt
Take the input to the batch from the indicated file, one line per answer

Resources