How to uglify a javascript file from VBA? - node.js

I installed node.js in my Windows environment, and also uglify-js
I created a sequence of uglifyjs commands and saved it in a file called uglify.js
uglifyjs K:\Temp\jsfiles\jsslashcharts.js -c -m -o K:\Temp\jsfiles\jsslashcharts.js
uglifyjs K:\Temp\jsfiles\jsslashdates.js -c -m -o K:\Temp\jsfiles\jsslashdates.js
How can I run this in node.js using VBA?
The closest thing I found on Stack Overflow is Run Node.js using VBA
So I tried (unsuccessfully)
Shell "C:\Program Files\nodejs\node.exe uglify.js", vbMaximizedFocus

The exe path contains spaces, so that will need to be double quoted, as "C:\Program Files\nodejs\node.exe" within the literal containing the command. Same goes for any arguments containing spaces.
Shell """C:\Program Files\nodejs\node.exe"" uglify.js", vbMaximizedFocus
Take a look at Quotation marks in string expressions (my emphasis)
You should include double quotation marks within the criteria argument in such a way so that when the value of the variable is evaluated, it will be enclosed within the quotation marks. Within a string, you must use two sets of double quotation marks to represent a single set of double quotation marks.

Related

Argument escaping not interpreted correctly when running node.js script from Windows PowerShell

Given the following script:
const yargs = require('yargs');
const argv =
yargs
.usage('Usage: $0 [--whatIf]')
.alias('d', 'directory')
.alias('wi', 'whatIf')
.nargs('d', 1)
.describe('d', 'alphabetize this directory')
.describe('whatIf', 'show what would happen if run')
.demandOption(['d'])
.argv;
console.log(argv.directory);
If I invoke the script from Windows PowerShell like so: node .\alphabetizer.js -d 'l:\my folder\Files - Some Files In Here\' --whatIf I get the output l:\my folder\Files - Some Files In Here\" --whatIf where I would expect just l:\my folder\Files - Some Files In Here\. It works OK with folder names that require no escaping, but it seems to get confused by the escaping.
If I examine process.argv, I can see the same escaping issue.
I have noticed that if I remove the trailing slash it will work. However, this still points to the node script not handling the input properly, because this should not be necessary with string set off by single quotes.
Is there a way to make this work?
Both Windows PowerShell (powershell.exe) and PowerShell [Core] v6+ (pwsh) are fundamentally broken with respect to quoting arguments for external programs properly - see this answer for background info.
Generally, PowerShell on Windows has to perform re-quoting behind the scenes in order to ensure that just "..."-quoting is used, given that external programs can't be assumed to understand '...'-quoting too when parsing their command line (which on Windows every program has to do itself).
Windows PowerShell is more broken with respect to arguments that end in \ and have embedded spaces, re-quoting them improperly; e.g.:
PS> foo.exe 'c:\foo \' bar
is translated into the following command line behind the scenes:
foo.exe "c:\ foo \" bar
This is broken, in that most applications - including PowerShell's own CLI - sensibly assume that the \" is an escaped " char. to be taken verbatim, thinking that the argument continues with  bar and then implicitly ends, despite the formal lack of a closing ".
PowerShell [Core] v6+ more sensibly translates the above to foo.exe "c:\foo \\" bar, where the \\ is interpreted as an escaped \, and the following " again has syntactic function.
If you're stuck with Windows PowerShell, your only choices are:
either: if possible, leave off the trailing \
otherwise: manually double it (\\), but only do so if the argument also contains spaces (otherwise, the \\ will be retained as-is, though in the case of filesystem paths that is usually benign).

Quotes around quotes on the Windows command line

So I found this Stack Overflow question which relates to what I would like to do; however, I am having trouble with the directory having spaces within it. I tried looking at several other Stack Overflow questions, but either I misunderstood them, or none have really addressed my problem. I've seen stuff on passing arguments as arrays and using %1 or something to address the special characters, but nothing has worked yet.
I tried entering the following into cmd.exe:
schtasks /Create /SC DAILY /TN PythonTask /TR "python "C:\Users\me\stuff with spaces \pythonprogram.py""
However, the quotes appear to not be taken in the correct order. I would like the command to be input as python "C:\Users\me\stuff with spaces \pythonprogram.py" to cmd.exe everyday.
How can I use quotes around quotes on the Windows command line?
ANSWER FROM BELOW:
Add a backslash \ before the argument which you are putting in quotes. I.e.:
do_some_command_in_windows_shell_with_this_given_string "run "something.exe""
is replaced with:
do_some_command_in_windows_shell_with_this_given_string "run \"something.exe""
Educated guess:
Escape the inner quotes with a backslash.

build bash string containing a variable value surrounded with single quotes

I'm having a nightmare from what should be the most trivial of tasks.
My final goal is issue the following command from a bash script:
sqlite3 my_db.db '.read my_file.sql'
There are two catches here:
1. The single-quotes are obligatory, and can't be replaced by, say, double-quotes
2. my_file.sql is a variable known only at run-time.
So what I need is a way to have bash build a string that on one hand contains a variable value, while on the other hand that value should be surrounded by single quotes.
I would also much prefer a solution not relying on additional tools like AWK, Perl or the like. Maybe sed if it's really necessary.
Thanks.
Thanks Jonathan and Nelson.
I tried all three suggestions, but they all failed.
For simplicity I reduced the problem to the following:
I wrote the following script (tst.sh):
#!/bin/bash
file=/tmp/1
ls "'"$file"'"
ls \'$file\'
ls "'$file'"
Then I isuues the following commands:
$ touch /tmp/1
$ ls '/tmp/1'
/tmp/1
$ ./tst.sh
'/tmp/1': No such file or directory
'/tmp/1': No such file or directory
'/tmp/1': No such file or directory
It seems the quotes were indeed added, but the resulting command was not the same as when entered manually.
Any ideas ?
Single-quotes are not obligatory. All of the following commands run sqlite3 with exactly the same arguments:
sqlite3 my_db.db '.read my_file.sql'
sqlite3 my_db.db ".read my_file.sql"
sqlite3 my_db.db .read\ my_file.sql
sqlfile="my_file.sql"
sqlite3 my_db.db ".read $sqlfile"
In all cases, the quotes (/escape) are parsed and removed before the arguments are passed to sqlite3. This is what you want. You want sqlite3 to get two arguments: my_db.db and .read my_file.sql. You do not want sqlite3 to see the quotes around the command -- that would be the equivalent of:
$ sqlite3 my_db.db
SQLite version 3.7.7 2011-06-25 16:35:41
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> '.read my_file.sql'
...>
...which, as you can see, just confuses sqlite3.
BTW, this is the same as the problem in your ls examples: you're passing single-quotes as part of the argument to ls, so it's looking for a file with single-quotes in the name and not finding it. You want the shell to remove the quotes rather than pass them to the command as part of an argument.
This will do what you say you want to do (getting single quotes to the program), but it uses double quotes:
sqlite3 my_db.db "'".read" "my_file.sql"'"
Avoiding double quotes, you can write:
sqlite3 my_db.db \'.read\ my_file.sql\'
For both of these, the second argument will be seen by sqlite3 as a string containing:
'.read my_file.sql'
If the file name is in a variable (file=my_file.sql), then:
sqlite3 my_db.db "'".read" "$file"'"
sqlite3 my_db.db \'.read\ $file\'
These notations are vulnerable to confusion if the file name contains spaces.
However, I don't think that's likely to be what you really want. The proscription on double quotes is puzzling, and the requirement for single quotes is likewise puzzling.
You can do as follows:
VAR=my_file.sql
VAR2="'.read $VAR'"
sqlite3 my_db.db $VAR2
user1860085, if you check out documentation for sqlite3 command and you will know how shell treats quotes and white spaces, you will probably come to conclusion that you want double quotes for your case.
but if you really want single quotes, here is solution:
eval sqlite3 my_db.db \'.read $VARIABLE\'
which in the fly will change to:
sqlite3 my_db.db '.read my_file.sql'
But I don't see why you could want it...
OK, problem solved !!
All that was missing is adding a little 'eval' command before the line.
So, in the simple example script I gave, changing:
ls "'$file'" to:
eval ls "'$file'"
did the job.
Thanks to all replyers :-)

Passing a node.js script an argument with a space on Windows

I'm running a node script from the command line on Windows and am trying to pass in a folder path that includes a space. When accessing this argument, via require modules or via the process.argv variable I don't seem to get what I would expect. For the following command:
node script.js "C:\path\to\folder\with a space\"
I seem to get the following value:
process.argv[2] = C:\path\to\folder\with a space\"
Notice the trailing " in the string. If the argument is passed without quotes, it obviously passes it as different arguments split on the space.
Am I doing something wrong, or is this a bug? And if it is a bug, is there a possible workaround?
The trailing backslash escapes the quote which is then again implied by the shell (instead of aborting due to lack of a closing quote).
The fix is simply escaping that backslash with another backslash or omitting it altogether:
C:\Users\myself> python -c "import sys; print sys.argv[1]" "foo\bar\"
foo\bar"
C:\Users\myself> python -c "import sys; print sys.argv[1]" "foo\bar\\"
foo\bar\
Note that you may only escape the last backslash this way - any other backslash ins the string will not act as an escape character:
C:\Users\myself> python -c "import sys; print sys.argv[1]" "foo\\bar\\"
foo\\bar\

How to create a triple nested command line string for Shell function in VBA?

I need to create a VBA Macro in Excel.
The task is that when a button is clicked a command will be executed in cmd.
The command to be executed is rfrompcb and it takes the path of a file as argument so there is the first level string which wraps the path up as it contains spaces. As this command is to be executed in cmd there is the second level string, which is the argument for cmd command, i.e. cmd /c "rfrompcb ""file_path""" (I hope I have got this one right). Then since it is to be called by Shell in VBA there is the third level string which wraps the cmd command and serves as the argument for Shell.
My question is: How many double quotation marks should there be? I am quite confused about what the final command line string would look like. Can anyone show me how to construct such string? Or is there another way to do it which involves less string nesting?
Thanks.
You would only need to quote file_path, for example:
shell environ$("COMSPEC") & " /c dir ""c:\program files\"" /b"
environ$("COMSPEC") just returns the full path for cmd.exe.
If the executable path you wish to run does not need to be quoted then to pass it an argument that does need to be quoted:
Shell Environ$("COMSPEC") & " /c rfrompcb ""file path"""
If the exe path does need to be quoted you need to wrap it all in another pair of quotes so it looks like:
cmd.exe /c ""c:\path to\rfrompcb" "file path""
Which can be done:
Shell Environ$("COMSPEC") & " /c """"c:\path to\rfrompcb"" ""file path"""""

Resources