So, im pretty new to this here, but heres the issue I cannot seem to get past.
writing a section of code in batch to launch a program, and simulate a single keypress. here is that code block in question
#echo off
set SendKeys=CScript //nologo //E:JScript "%~F0"
ping -n 5 -w 1 127.0.0.1
start .\cc6.exe
%SendKeys% "{ENTER}"
// JScript section
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));
code returns "Conditional Compilation is off" error.
Searching for a solution tells me that adding
/*#cc_on #*/
to the code should resolve the sitation and turn conditional compilation on.
however adding that segment in results in " '/*#cc_on'is not recognized as an internal or external command, operable program or batch file." error.
You need to use the conditional compilation via #if command this way:
#if (#CodeSection == #Batch) #then
#echo off
set SendKeys=CScript //nologo //E:JScript "%~F0"
ping -n 5 -w 1 127.0.0.1
start .\cc6.exe
%SendKeys% "{ENTER}"
goto :EOF
#end
// JScript section
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));
The #if command ignore the following code until the next #end; the (#CodeSection == #Batch) part is just an expression that have false value in JScript but that is valid in Batch, and the #then part may be any word that just complete the syntax for Batch part. You must also include a goto :EOF command before the JScript section, otherwise it would be executed as Batch code.
Most programming languages use if to indicate conditions, or conditional expressions and statements.
To turn on conditional compilation for JScript, you'd also need an if statement.
Conventionally, you'd separate the batch code from JScript via commenting.
JScript was introduced in 1996 and supported only by IE 3.0 for client side scripting, and has syntax similar to Javascript, "Microsoft's dialect of the ECMAScript".
#cc_on activates conditional compilation in IE 10 and earlier versions within script comments, meaning the codes within comments are to be treated as codes & executed, and not treated as comments. Of course it'll only be executed by browsers/applications that know about it, otherwise it'll be ignored or treated as comment. (Kind of like 'I didn't know' excuse most offenders give for breaking the law, or the different behaviour in people who see beyond what can be observed by the known senses)
IE 11+ & Windows store apps don't support conditional compilation.
The code you're running is in a batch file, and not in Internal Explorer to execute client side scripting, so you won't need #cc_on in your code.
Try the below format in your batch file:
#if (#a==#a) #end /*
cscript //E:JScript //nologo "%~f0" %*
REM --- Insert other batch codes here ---
exit /b
*/
// --- JScript codes below this line ----
// --- insert more scripts ---
WScript.Echo(" Code activated, blah!");
WScript.Quit(0);
I believe the /*#cc_on #*/ needs to be inserted into the jscript script file, not in the batch file you posted.
Related
After doing some googling I got many answers(from stackoverflow and other sites) saying keypress automation & simulation is possible with Batch-JScript duo file, so I put together my fndings and made a script like below, but it doesn't work as expected at all.
Take a look at the below script:
#if (#CodeSection == #Batch) #then
#echo off
set SendKeys=CScript //nologo //E:JScript "%~F0"
start SystemPropertiesAdvanced.exe
%SendKeys% "{n}"
goto :EOF
#end
::JScript section
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));
As it is apparent from the code, I a trying open the Environment Variables section which is already elevated to Administrator rights, so that user can edit both User and System environment variables. But this script only opens the Window before Environment Variables in elevated privilege.
As you can see, I have already put the code to simulate & automate keypress of n letter after opening the SystemPropertiesAdvanced Window and thus bypassing that manual step, but this part doesn't work.
Maybe I have made some mistake, but I cannot find what would that be, so any help is appreciated.
Say I have the following batch script:
For ... DO (
SET VAL=%%B
IF defined VAL echo %%A=%%B >> %OUTPUT_FILEPATH%
)
How could I get the echo to output using Unix (just line feed) line endings?
Alternatively, could I write the file as-is then convert it from the batch script afterwards? (some kind of find /r/n and replace with /n? If so, how would I do that?)
I'd like a self-contained solution (i.e. one that doesn't involve downloading extra utilities, and can be done from within the batch script itself [Windows 7]).
The suitable way to perform this conversion is not via a Batch file, but using another programming language, like JScript; this way, the conversion process is fast and reliable. However, you don't need a hundreds lines program in order to achieve a replacement as simple as this one. The two-lines Batch file below do this conversion:
#set #a=0 /* & cscript //nologo //E:JScript "%~F0" < input.txt > output.txt & goto :EOF */
WScript.Stdout.Write(WScript.Stdin.ReadAll().replace(/\r\n/g,"\n"));
EDIT: I added a modification to the original code that allows to include more commands in the Batch part in the standard way.
#set #a=0 /*
#echo off
set "OUTPUT_FILEPATH=C:\Path\Of\The\File.txt"
cscript //nologo //E:JScript "%~F0" < "%OUTPUT_FILEPATH%" > output.txt
move /Y output.txt "%OUTPUT_FILEPATH%"
goto :EOF */
WScript.Stdout.Write(WScript.Stdin.ReadAll().replace(/\r\n/g,"\n"));
The first line is a trick that hide the cscript command from the JScript code, so the compilation of this hybrid .BAT file don't issue errors.
In the JScript code: WScript.Stdin.ReadAll() read the whole redirected input file; this may cause problems if the file is huge. The replace method use a regex to identify the text to replace and put in its place the second string; you may read a further description of this topic at this link. The WScript.Stdout.Write just take the output from replace and send it to the screen. Easy! Isn't it? ;-)
If you are OK with using PowerShell, you can produce a Unix newline like this:
PowerShell -Command Write-Host
You can combine this with the SET /P trick to output text without newlines and add newlines manually. For example:
( ECHO | SET /P="Hello World!" & PowerShell -Command Write-Host ) > output.txt
After this, output.txt will contain the text Hello World! with a single 0x0a character appended.
Taken from Macros with parameters appended:
Formatting is tricky, but try
set ^"LF=^
^" Don't remove previous line & rem line feed (newline)
set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"& rem Define newline with line continuation
For ... DO (
SET VAL=%%B
IF defined VAL <nul set/P^=%%A=%%B%\n%>> %OUTPUT_FILEPATH%
)
Or, to avoid the leading space after first line:
<nul set/P^=%%A=%%B%\n%^>> %OUTPUT_FILEPATH%
How can I start a batch file in full-screen mode? I know that this question was asked before, but it wasn't actually answered.
unfortunately, I don't know reverse engineering, so I cant decompile the code.
Here is something I tested:
#if (#CodeSection == #Batch) #then
#echo off
rem Use %SendKeys% to send keys to the keyboard buffer
set SendKeys=CScript //nologo //E:JScript "%~F0"
start cmd.exe
%sendkeys% "(%{enter})"
goto :EOF
#end
// JScript section
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));
The idea is that it starts a cmd window, and (this part of the code %sendkeys% "(%{enter})") is supposed to simulate the user pressing [alt] + [enter]. But it doesn't work.
I wrote a little c# tool to send an alt+enter to the cmd window, but I'm looking for an internal method.
If sendkeys parameter for "Alt-Enter" should be "%{enter}", then this line:
%sendkeys% "(%{enter})"
... should have not parentheses and there is one percent sign missing. Try:
%sendkeys% "%%{enter}"
See the example for "Alt-V" at this post
I made an autoIT script, extremely simple.
Run("cmd.exe")
sleep (1000)
Send("!{enter}")
Unfortunately, this isn't what I want. I was looking for more of an actual command, not some other 3d party program.
:: edit
here: This is a link to the compiled 1 line skiddy script that I made.
Its a command line tool, so all you have to do is type fullscreen in a cmd window to toggle fullscreen mode.
In case you don't know how to use a command line tool, you have to be in the same directory as the file.
For my new program I want to echo the code of a webpage. I searched on google and Stack Overflow but didnĀ“t found something like this. I do not want to use external programs like URL2FILE or something like this.
This code is from a previous question that only needed to do the query to the server (linked in comments) with the "display" of the page source code added.
#if (#This==#IsBatch) #then
#echo off
rem **** batch zone *********************************************************
setlocal enableextensions disabledelayedexpansion
rem Batch file will delegate all the work to the script engine
if not "%~1"=="" (
cscript //E:JScript "%~dpnx0" %1
)
rem End of batch area. Ensure batch ends execution before reaching
rem javascript zone
exit /b
#end
// **** Javascript zone *****************************************************
// Instantiate the needed component to make url queries
var http = WScript.CreateObject('Msxml2.XMLHTTP.6.0');
// Retrieve the url parameter
var url = WScript.Arguments.Item(0)
// Make the request
http.open("GET", url, false);
http.send();
// If we get a OK from server (status 200), echo data to console
if (http.status === 200) WScript.StdOut.Write(http.responseText);
// All done. Exit
WScript.Quit(0);
It is just an hybrid batch/javascript file. Saved as callurl.cmd and called as callurl "http://www.google.es" it will do what you ask for. No error check appart from correct response, no post, just a skeleton.
So you want to display the source code of a webpage in the console line?
In Linux you can use GET google.com.
The Batch file below display in the screen the HTML Code of the webpage given in the parameter, so I think it is a solution to this topic.
#if (#CodeSection == #Batch) #then
#echo off
rem Start explorer with the web page and wait for it to initialize
start "" Explorer.exe %1
timeout /T 5 > NUL
rem Send to Explorer: Alt-V (View tab)...
CScript //nologo //E:JScript "%~F0" "%%V"
timeout /T 1 > NUL
rem ... followed by S (Source)
CScript //nologo //E:JScript "%~F0" "S"
goto :EOF
#end
WScript.CreateObject("WScript.Shell").SendKeys(WScript.Arguments(0));
Use previous program this way:
test.bat http://www.google.com
For further details, see this post.
Quick question. I've been looking desperately for a way calculate logarithm in a DOS batch data.Please help kindly.
the best way is making executable logarithm file and use it in your batch file, msdos have not any log function.. you can use some source code like this:
http://en.literateprograms.org/Logarithm_Function_(Python)
You may use this very simple Batch-JScript hybrid file:
#if (#CodeSection == #Batch) #then
#echo off
rem JSExpr.bat: Evaluate a JScript (aritmethic) expression
rem Antonio Perez Ayala
Cscript //nologo //E:JScript "%~F0" %1
goto :EOF
End of Batch section
#end
// JScript section
WScript.Echo(eval(WScript.Arguments.Unnamed.Item(0)));
For example:
C:>jsexpr Math.log(10)
2.30258509299405
As a matter of fact, you may evaluate any valid JScript arithmetic expression with previous program. For example:
C:>jsexpr Math.E
2.71828182845905
Search for "jscript reference" (math object), for example: http://msdn.microsoft.com/en-us/library/ie/b272f386(v=vs.94).aspx
Antonio
There are several free command line calculators for DOS available.
For example EVAL.
It's well documented.
Or Mathfc24