*.HTA with batch file user entered variables - batch-file

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.

Related

How to share values between batch file and embedded script

I am trying to share values between my batch file and embedded script within it and I can't find a way to make it work. I am relatively new to embedded scripting...
I have tried to find an answer on the web and I can't find an answer to my question. It would need to be (all the scripts) in the same .bat file...
<!-- : Begin batch script
#echo off
cls
set "Shared_UserName=VelocityDK"
goto ShareValue
:ShareValue
cls
cscript //nologo "%~f0?.wsf" //job:UserName
pause >nul
cls & exit /force
----- Begin wsf script --->
<package>
<job id="UserName">
<script language="VBScript">
Dim Shared_UserName As String = %Shared_UserName%
WScript.Echo "Your username is: " & Shared_UserName
</script>
</job>
</package>
I am expecting the embedded VBScript to write the following output: Your username is VelocityDK. but instead, I get a message saying:
Microsoft VBScript compilation error: Expected end of statement
If you plan to use shell methods inside VBScript then use Windows Scripting Host (WSH) automation object. Please find the code:
Set wshShell = CreateObject( "WScript.Shell" )
userName = wshShell.ExpandEnvironmentStrings("%Shared_UserName%")
WScript.Echo "Your username is: " & userName
See refs :devguru.com or ss64.com

Batch Remembering Configuration

I'm trying to create a batch code to remember user input. Like if you put in "echo What is your name?" then put in your name. It would then save it somewhere to remember it or recall it later on. Even if it were to create and write it to a file to remember things from.
This should do the trick:
#echo off
cls
set INPUT=
set /P INPUT=Input your name (it will be logged in names.txt): %=%
echo Your inputted name was: %INPUT%
echo %INPUT% >>names.txt
Here's a demonstration of using Windows batch commands to write user input to a file. Note the file will be written in the same directory you run the batch file from.
set /p name="What is your name: "
#echo off
echo %name% > name.txt
The /p flag turns this into a prompt, setting the value of name to the result of the user input string. #echo off prevents echoing the next line, which echoes the stored variable into a text file called name.txt. If name.txt doesn't exist, it's created; if it does exist, it's overwritten with this value.

Opening URLs with user input in CMD 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.

Trying to create text file using user-input with Batch

echo.
set /p textfileName=What would you like the file to be named?
echo.
echo On the line below, write what text you would like the file to contain:
echo.
set /p textfileContents=
echo %textfileWrite% >> %textfileWriteName%.txt
echo.
echo Your file has been created and is in the same directory as this batch file.
echo It is named %textfilename%.txt
echo.
goto inputCommand
Whenever I try to use this code, it comes up with "Echo is off" (from #echo off previously in the file) but not what the user inputted. Then, if I run it again in the same instance, it will create the previous file but not the one I just told it to create.
I've tried having > and it didn't work, >> didn't work either.
I think you have a few problems going on here. First, you mentioned #echo, but looking at your code, you're just using echo.
Also, I think there's some confusion with your variables. You capture the user's filename into textfilename, but then write to textfileWriteName. You capture the user's file contents in textfileContents, but then write textfileWrite to the file.
Finally, you specify a goto label that doesn't exist. Maybe this is part of a larger batch file that you just partially copied?
Anyhoo, I think this is along the lines of what you intended:
#echo off
set /p textfileName=What would you like the file to be named?
#echo On the line below, write what text you would like the file to contain:
set /p textfileContents=
#echo %textfileContents% > %textfileName%.txt
#echo Your file has been created and is in the same directory as this batch file.
#echo It is named %textfilename%.txt
you have serious space issues in your variables " text filename and text contents"
avoid spaces in your variables or if you must use more than one word in your variable use symbols to separate the strings .. the code below should work fine
#echo off
: main_menu
echo.
echo.
set /p notf=name of text file :
set / p cof=contents of file :
echo %cof%>>"%notf%.txt"
echo %notf% text file was successfuly created...
echo.
echo.
echo press any key to return to main menu
pause>null
cls
goto main_menu

Find and Replace from batch file not working

I am trying to find and replace values of a string within a batch file but having issues. I have the user run the batch file and it asks the user 1)what drive the file is on 2)what is the name of folder in the TEST parent folder 3)what is the name of the new server. I want the batch file to look within a file called importer.config and replace a value called server_name with whatever the input from the user is. Here is what I have:
#echo off
SET drive=
SET /P drive=Please enter the drive:
SET folder=
SET /P folder=Enter name of folder desired:
SET server=
SET /P server=Enter name of new server:
#echo off > newfile.txt
setLocal EnableDelayedExpansion
if exist newfile.txt del newfile.txt
for /f "tokens=* delims= " %%a in (%drive%\test\%folder%\importer.config) do (
set str=%%a
set str=!str:server_name=%server%!
echo !str! >> newfile.txt
)
del importer.config
rename newfile.txt importer.config
pause
Every time I run this, the cmd prompt shows:
The system cannot find the file specified c:\test\users_input_they_entered\importer.config. The issue is that file is there so trying to understand what I am missing and why it cant find the file that does exist.
It then also states "Could not find c:\windows\system32\importer.config" which not sure why that happens as well
I have searched on stackoverflow, but cannot figure this out with any assistance.
You're pushing your luck using the for loop for that.
A tool like sed would work well.
If you look at this post they have a vbscript implementation that you could use
Is there any sed like utility for cmd.exe
set input_file=importer.config
set output_file=temp.config
set new_server_name=server1984
cscript /Nologo sed.vbs s/server_name/%new_server_name%/ < %input_file% > %output_file%

Resources