Is there a way I can make the output display the proper text. When I have tried this the inequality sign do not show up unless you put " around it and it displays the quotes too. Is there a way it can not print the parenthesis and still display the > or < sign?
The code I am trying to use looks like the following...
set /p projectname=Enter Project Name:
cd %USERPROFILE%\Desktop\Output
echo <html> >>%projectname%.html
echo <head> >>%projectname%.html
echo <link rel="stylesheet" href="%projectname%.css">">>%projectname%.html
echo </head> >>%projectname%.html
I want the output to look like the following...
<html>
<head>
<link rel="stylesheet" href="(project name).css">
Escape each redirector that is to be echoed as text with ^ Thus: ^< or ^>
for instance
echo ^<head^> >>%projectname%.html
or better (no trailing space)
>>%projectname%.html echo ^<head^>
or better still
(
echo whatever
echo whatever else
echo whatever else again
)>filename
where > becomes >> to append (> means 'create file anew')
(only need to redirect to file once)
Related
I generally use the echo method for writing "text" to an HTML-file by using:
echo. >> "C:\Users\Me\Desktop\test.html"
echo ^<div^>TestDIV^</div^> >> "C:\Users\Me\Desktop\test.html"
I've experienced, that echo always writes the additional lines to the end of the target file.
But what if my target file (test.html) already has a structure like:
<div id="wrapperDIV">
<div id="DIVcontent1">DIV 1</div>
<div id="DIVcontent2">DIV 2</div>
</div>
and I want to write the additional lines within this existing structure, for example right after:
<div id="DIVcontent1">DIV 1</div>
I have tried to set up a script for this, but so far I couldn't make it running. (It seems to fail in the for-loop)
set DIVInput=^<div^>TestDIV^</div^>
set inputfile=C:\Users\Me\Desktop\test.html
(for /f usebackq^ delims^=^ eol^= %%a in ("%inputfile%") do (
if "%%~a"=="DIV 1^</div^>" call echo %DIVInput%
echo %%a
))>>"%inputfile%"
pause
Your set DIVInput=... consumes the escaping carets, which is bad in this case, because you need it later to echo the variable correctly. The preferred syntax set "var=value" preserves them (they are safe inside the quoting).
With your if line, you want to check for a substring only, not the whole line (if always compares complete strings and doesn't support wildcards). I used echo fullstring|find "string" to check that. Note that I use quotes (echo "%%~a"') for the same reason as above: proper handling of poison chars like>and<Same reason for not escaping with thefind` command ("save inside quotes")
You redirect to your input file. Redirecting is the first thing for the parser, so the file gets overwritten even before for has a chance to read the file. Use a different file and overwrite your original file when done.
You want to insert a string after a certain trigger line, but your code inserts before that line (well, that's easy to fix).
#echo off
setlocal
set "DIVInput=^<div^>TestDIV^</div^>"
set inputfile=test.html
(for /f usebackq^ delims^=^ eol^= %%a in ("%inputfile%") do (
echo %%a
echo "%%~a" |find "DIV 1</div>" >nul && echo %DIVInput%
))>"%inputfile%.new"
REM move /y "%inputfile%.new" "%inputfile%
Note: I disabled the move command for security reasons. Remove the REM after checking test.html.new looks ok.
Following is the hybrid batch script:
<!-- :: Batch section
#echo off
setlocal
echo Select an option:
for /F "delims=" %%a in ('mshta.exe "%~F0"') do set "HTAreply=%%a"
echo End of HTA window, reply: "%{HTAreply}%"
pause
goto :EOF
-->
<HTML>
<HEAD>
<HTA:APPLICATION SCROLL="no" SYSMENU="no" >
<TITLE>HTA Buttons</TITLE>
<SCRIPT language="JavaScript">
window.resizeTo(374,100);
function closeHTA(reply){
var fso = new ActiveXObject("Scripting.FileSystemObject");
fso.GetStandardStream(1).WriteLine(reply);
window.close();
}
</SCRIPT>
</HEAD>
<BODY>
<button onclick="closeHTA(1);">First option</button>
<button onclick="closeHTA(2);">Second option</button>
<button onclick="closeHTA(3);">Third option</button>
</BODY>
</HTML>
Output:
I obfuscated it as stated here : [Post#21]
https://www.dostips.com/forum/viewtopic.php?t=7990&start=15
but then the HTA app output isn't as before. It is as follows:
The entire batch content is displayed in the HTML window.
I want to know why this happens and secondly is there any way to obfuscate this hybrid batch script
Same happens when I try to convert the batch file to exe.
Edit
Problem solved. There was an error in html code. apologies. I would have edited to show the problem but I guess it was too trivial to do that.
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.
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.
2 Questions:
1) Changing scripts based on input
Basically, say I have a file, like search.html that changes based on what you type in.
Aside from doing
set/p string=What would you like to search for?
echo ^<!DOCTYPE html^> >>file.html
echo ^<html^> >>file.html
echo ^<title^>^</title> >>file.html
echo ^<script language="JavaScript""^> >>file.html
echo string = '%site%'
...
Is there another way to do this?
2) Getting things returned from a file?
I have no example for this. I was simply wondering if you could start a file, use wait, and once it has closed get what was in it?
try this:
#echo OFF &setlocal
(
echo ^<!DOCTYPE html^>
echo ^<html^>
echo ^<title^>^</title^>
echo ^<script language="JavaScript""^>
echo string = '%site%'
)>file.html
2) Getting things returned from a file
#echo OFF &setlocal
FOR /f "delims=" %%a IN (file.html) DO (
ECHO(%%a
)
There are many ways to replace text in a file with something else. You can get input and then replace a MARKER (text like that) with the input text. VBS, Powershell, SED, AWK, and batch can do it.
Your second question is a bit short on detail - but FINDSTR etc can read lines from a file.