I am trying to write javascript which should run cmd.exe with a specified command line in it like this docs.google.com/file/d/0B7QHCoQDlEvKWUZSX3oxUDI2SDg/edit:
I prepare a code after reading shellexecute method on microsoft site:
var objShell = new ActiveXObject("Shell.Application");
objShell.ShellExecute("cmd.exe", "C: cd C:\\pr main.exe blablafile.txt auto", "C:\\WINDOWS\\system32", "open", "1");
but it does not insert command line in cmd.exe.
Could anybody help me? Thank you in advance.
Maybe you don't have this ActiveX-control installed (or registered) in your computer.
WScript.Shell should be found in every Windows:
var run=new ActiveXObject('WSCRIPT.Shell').Run("commands to run");
If there are spaces in commands to run, you need to use double quotes.
Edit
The content below is mainly from MSDN: http://msdn.microsoft.com/en-us/library/windows/desktop/gg537745(v=vs.85).aspx
iRetVal = Shell.ShellExecute(
sFile,
[ vArguments ],
[ vDirectory ],
[ vOperation ],
[ vShow ]
)
Let's take [vDirectory]. The documentation says: "The fully qualified path of the directory that contains the file specified by sFile. If this parameter is not specified, the current working directory is used."
This means that you have an invalid path for this argument (having .cmd.exe at the end of it). Also all examples for creating the ActiveX are like this:
var objShell = new ActiveXObject("shell.application");
Notice the lowercase in "shell.application".
And May12, thank's for asking this. I didn't know about this ActiveX control before, it seems to be very useful to me.
EDIT II
But have you understood it? Your example works perfect in my app:
objShell.ShellExecute("cmd.exe", "cd C: C:\\cd c:\\ext_file main.exe test.txt", "C:\\WINDOWS\\system32", "open", 1);
With three exceptions:
1) The one I mentioned early in this answer about the path
2) Escaped \ used also in arguments.
3) The last argument is type of number, not a string.
If I understood correctly, you are only interested in calling another file with parameters. This is my example of calling another file from a shortcut or batch file
If there are no spaces in the path
mshta.exe "javascript:new ActiveXObject('WScript.Shell').Run('cmd /c start /max C:\\Windows\\Notepad.exe',0,false);close()"
With spaces in the path. The double quote is replaced with #
mshta.exe "javascript:new ActiveXObject('WScript.Shell').Run('cmd /v /c set a=""&call set #=!a:~0,1!&start /max C:\\!#!Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe!#!',1,true);close()"
var objShell = new ActiveXObject("Shell.Application");
objShell.ShellExecute("cmd.exe", "C: cd C:\\pr main.exe blablafile.txt auto", "C:\\WINDOWS\\system32", "open", "1");
is usable
Related
When I say JavaScript file, I mean the whole file. Not a function. I've seen ways to run a JavaScript function from c# but nothing about running a file.
According to this question: https://stackoverflow.com/a/1469790/13105088, one could run the command with any normal shell.
string strCmdText;
strCmdText= "/C node myscript.js"; // the command to run from the command prompt
System.Diagnostics.Process.Start("CMD.exe",strCmdText);
Note that this will show the Command Prompt on Windows.
This following script prevents that.
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C node myscript.js";
process.StartInfo = startInfo;
process.Start();
(these example scripts were both provided by the answer I linked to above.)
In addition, you should probably also note:
Important is that the argument begins with /C otherwise it won't work. How Scott Ferguson said: it "Carries out the command specified by the string and then terminates."
Note: this is all assuming you are referring to NodeJS when you are saying a "JavaScript file", but any other interpreter (eg. /C python3 myfile.py) should also work.
i have googled around quite a bit but couldnt find anything that tells me how to get access to the stdin when executing a JS with jrunscript.
it seems not to be possible.
use case:
i want to write a little JS script that does a regex-replace for use on the cmd where i would want to pass in text data via piping, e.g.
>cat file | jrunsscript -f apply-regex.js
... and out comes the result (which i could pipe into a file, etc).
hence, i need to access to the stdin in order to read the what's piped unto jrunsscript.
side note: for this i usually use sed, but there are problems when the regex itself involves quotes in certain cases.
Not sure if this is on topic: http://docs.oracle.com/javase/7/docs/technotes/tools/share/jsdocs/GLOBALS.html
read(prompt, multiline)
Reads one or more lines from stdin after
printing a prompt
Based on your comment, I think you need to use arguments as shown: https://docs.oracle.com/javase/8/docs/technotes/tools/unix/jrunscript.html
Usage:
jrunscript -f test.js "Hello World"
Get the argument:
document.write(arguments[0]);
Upon more research, you may be able to read via Interactive mode:
jrunscript -f -
Interactive mode, read script from standard input. If this is used, this should be the last -f option
But still not clear if this is what you're looking for exactly.
the workaround thus far is to
pipe the input into a file
pass tempfile as arg to jsrunscript
use JS output as needed
Take a dip in Java.
in file JJS_SAYS_HI.js
//Java-y bit
var br = new java.io.BufferedReader(
new java.io.InputStreamReader(
java.lang.System.in,
java.nio.charset.StandardCharsets.UTF_8)
);
//JavaScript-y bit
var line = "";
while(br.ready()) {
line = br.readLine();
print("JJS says: ${line}");
};
Test the whole mess with a command like: echo Hi! | jjs -scripting JJS_SAYS_HI.js
Output is JJS says: Hi! (on Windows)
I have a bash script that spit out the cpu temperature in Beaglebon Black. I need to convert these to javascript. Any good at linux command and javascript have any idea how to do this ?
I was thinking of using fs.readfile command but not too sure.
Here the code in bash script and link of where i got it from:
bash script link
# CPU TEMP MONITOR
# CTRL-C TO STOP
# WRITTEN BY BRIAN HECKATHORNE - ME#GODFEAR.ORG
# Modified by Richard St-Pierre - inspire.logicsupply.com
# Simple script for monitoring the CPU temp on a BeagleBone Black running Debian
#!/bin/bash
for (( ; ; ))
do
echo -n "CPU Temp [Celsius]: "
cat /sys/class/hwmon/hwmon0/device/temp1_input | sed 's/...$//'
sleep 2
done
Here is a more-or-less literal port.
A few caveats:
I infer that the file only has one line in it. If that's not correct, this code may need a tweak.
This is a more-or-less literal port, meaning that I make no judgments about whether cat (and, therefore, fs.readFileSync()), regexp replacement, etc., are the best choices here. I'm just mapping the bash code to Node.js code, more or less.
With that out of the way, here you go:
#!/usr/bin/env node
var fs = require('fs');
var temperatureFile = '/sys/class/hwmon/hwmon0/device/temp1_input';
var displayTemperature = function () {
process.stdout.write("CPU Temp [Celsius]: ");
process.stdout.write(fs.readFileSync(temperatureFile, {encoding: 'utf-8'})
.replace(/...$/g, '\n'));
}
displayTemperature();
setInterval(displayTemperature, 2000);
How can I launch Word with a document path from activeX like for example:
function RunWord(cmdline, args){
var v;
v = new ActiveXObject("Shell.Application");
v.ShellExecute(cmdline+" "+args);
}
where cmdline is 'Word' that is a shortcut link in one of the system folder and args is the path to file like 'C:\Projects\Schedule.doc'
The problem is it combine the string like 'Word C:\Projects\Schedule.doc' and then give an error saying it does not find this application, however when I launch word alone without args it works ok.
Any ideas ?
Do you need to escaped the backslashes in your filename? I.e. use
'C:\\Projects\\Schedule.doc'
instead of
'C:\Projects\Schedule.doc'
I'm using HTA and in it I have a function that should run a command line with wshell.run , If I'm writing this line in Windows 'Run' util it is working fine, I want it to work also in the HTA with wshell.run.
The line is:
C:\xxxx\xxx\xxx.EXE aaa.psl abc
( The names are xxx just in here - not in the real code.. )
In the javascript code I'm using:
function runCmd()
{
wshShell.exec( "C:\xxxx\xxx\xxx.EXE aaa.psl abc" );
}
The error I got is in the xxx.EXE application says "couldn't open aaa.psl File not found".
Thanks,
Rotem
I'm surprised the xxx.EXE program is running at all. You need to escape those backslashes in the command:
wshShell.Exec( "C:\\xxxx\\xxx\\xxx.EXE aaa.psl abc" );
// ^-----^----^--- here
If you're doing the same thing in the aaa.psl filename, that's your problem.
If you're not passing a full path to the aaa.psl file, then most programs (not all) will expect it to be in the current directory, so you'll want to make sure you've set the current directory correctly (although using absolute paths may be a better option).
Here's an example, for instance, of telling Notepad to edit a file:
shell = WScript.CreateObject("WScript.Shell");
shell.Exec("c:\\windows\\system32\\notepad.exe c:\\temp\\temp.txt");
...or via the current directory:
shell = WScript.CreateObject("WScript.Shell");
shell.CurrentDirectory = "c:\\temp";
shell.Exec("c:\\windows\\system32\\notepad.exe temp.txt");
Okkkk T.J. is the man!! :)
I finnaly made it with your help by replacing exec to run:
This is the final (and working) code:
function runCmd()
{
wshShell.CurrentDirectory = "G:\\xxx\\xxx";
wshShell.run( "xxx.EXE xxx.psl abc" );
}