Unable to use batch script to write to a .vbs script - batch-file

I am using a batch script to create and write into a .vbs file.
The file is named invisible.vbs.
This is the batch script that creates and write the .vbs script.
echo.>"C:\Users\LinFamily\Desktop\invisible.vbs"
echo CreateObject("Wscript.Shell").Run &WScript.Arguments(0)&,0,False >> invisible.vbs
The first line of code runs fine and invisible.vbs is created, but the script does not write the second line of code to invisible.vbs , instead, it tries to run it like it is a code.
I used quotation marks,
"CreateObject("Wscript.Shell").Run &WScript.Arguments(0)&,0,False" >> invisible.vbs
and the code gets written onto invisible.vbs, but the quotation marks get written onto invisible.vbs too, and that is not what I want. Invisible.vbs will not work if there is a quotation mark.
I have tried using parenthesis but that also does not work.
Is there anyway to write the code to invisble.vbs without the quotation at the end? Thanks for the help!

special characters ) and & need ^ escaping.
rem [ write all lines in one operation using parenthesis ]
> "invisible.vbs" (
echo(CreateObject("Wscript.Shell"^).Run ^&WScript.Arguments(0^)^&,0,False
)

There are ampersands in your code that do nothing, that aren't in any standard for VBScript. In short voodoo code. It is illegal VBScript code.
Ampersands and redirection are evaluated by CMD exe before other commands. So it won't write them.
So two reasons why the code don't work.
If you had tried to run that VBScript is says Line 1 Character 35 (&) Syntax Error.

Related

batch file to create and write to VBScript file

I am creating a batch script that creates and writes to a VBScript. I am writing to the file like this:
echo (code for vbscript) >> [name of vbscript].vbs
However, there is one part of the code that is troubling me:
echo If Len(m) > 1 then >> sys.vbs
Since the code itself has a > symbol in it, the batch file interprets it as me trying to write to the VBScript and cuts off the code, but that is not the case. I want the code to say If Len(m) > 1 Then but instead it just says If Len(m) then.
I already tried working around this by modifying the code to say:
echo if NOT Len(m) < 1 then >> sys.vbs
but that does not work either. If I do that, then it omits the whole line of code altogether.
I'm sorry if I didn't explain this problem well enough, it's hard to describe.
<, > and >> are redirection operators in batch. As #rojo already pointed out in the comments you need to escape them with a caret (^) if you want to echo them as literal characters in the output file:
echo If Len(m) ^> 1 Then >> sys.vbs
Using double quotes around the echoed string does not work in this case, because echo includes the double quotes in the output.
However, I agree with #Tomalak that you may want to reconsider your approach. Generating VBScript from a batch file on the fly is usually not a good/clean way of solving whatever problem you have on your hands.

Batch File Command throwing error in loop

Here is the situation. I need to "type" a .sj files content, then save it as an .js file. This is not the same as a rename, as the encoding is different. I am very new to Batch File syntax, but proficient with other programming languages. Here is what I have tried:
call for %%i in (.\*.js) do type %%i > %%i.js
But this is giving me the "%%i is unexpected at this time" Error.
If you need me to provide more insight, I will be happy to.
BACKGROUND: Trying to use JSDoc3 on .sj files but the encoding is not compatible. Using an encoder did not work either. What did work is copying and pasting the contents into a new file with encoding UTF8. But like I stated, a program like UTFCast did not work.
Two percent signs without anything in between (in a batch file) are treated like a single percent sign in a command (not a batch file).
Moreover CALL command enables a user to execute a batch file from within another batch file.
So from cmd you need to run only below..
for %i in (.\*.js) do type %i > %i.js
if you want to learn more...
for command
call command

Bat file prints multiple statements

I am invoking bat command in C++.
The command below writes to console "connecting to bootrom: connected . writing 0x001A13".
system("D:\abc\abc.exe -u load D:\abc\13oct\agi\agit")
but when i execute same command to dump the above string into file like this:
system("D:\abc\abc.exe -u load D:\abc\13oct\agi\agit">>D:\abc\13oct\tempFile.txt");
It appears that the temp file is having multiple instances of connected . writing 0x001A13 in tempFile.txt
Does any body point me an appropriate fix for this.
Thanks in advance!
OK, I hope I understood this well: The same output you get once if the first command appears twice in the temp file.
That's probably because you're using >> for redirection, which doesn't replace the ouput file, but appends to it.
That means, if you execute echo Test >> tempfile.txt twice, it will have two lines reading Test.
If you want to save only the ouput of the last command to the file, use > instead of >>.

Escape comma in batch file to execute a graphicsmagick command

I've got the line gm -mogrify -level 30%,10,100% Edit.jpg in a .bat file that executes perfectly when I type it manually. However, it ends the line on the first comma of the argument when executed as a batch script.
How should I rewrite this to make it work?
Thank you.
% is a special character in batch script. Use %% instead.

Stupid Batch File Behavior. Tries to execute comments

I have tried prefixing lines with semicolons, 'REM', etc.. but no matter what when I run my batch file I keep getting "unknown command REM whatever"
"REM test" It is not recognized, and it is windows vista. I simply get "rem" output back to my console.
That's entirely normal behavior. Batch files are simply sequences of commands that are run one after another. So every line will get output to the console as if it were typed there.
H:\>echo rem test > test.cmd
H:\>test
yields the output
H:\>rem test
as if I typed rem test directly to the console.
You can suppress this by either prefixing the line with #:
#rem test
or by including echo off in the batch file:
#echo off
rem test
If I put ":: test" and execute it I get back "Test".
Can't reproduce here.
If I put "; test" it recursively executes itself
A semicolon at the start of the line seemingly gets ignored.
If you're talking about cmd.exe batch files under Windows, you can use:
rem this method or
:: this method.
For bash and a lot of other UNIX-type shells, you use:
# this method.
I'm pretty certain you're not using cmd.exe since that would give you an error like:
'rem' is not recognized as an internal or external command,
operable program or batch file.
rather then:
Unknown command ...
If you are using a UNIX-type shell, the # character is almost certainly what you're after. If you let us know exactly the shell you're using, we can probably help out further.
you probably created an UNICODE file. These files contain 2 bytes header named BOM
which is not shown by any editor but cmd attempts to execute them and fails.
To make sure this is indeed an issue: type any other command at the very beginning
of your file and see it throws the same error - for example #echo test
To fix it, just create a new plain text file and copy content of the original file there.
then remove the original file and replace it by the newly created one.
In my case the problems are line endings. Somehow Maven or the Jenkins pipeline running on a Linux machine changed the line endings from Windows style (CR LF) to Unix style (LF). Changing them back solves the issue for me.

Resources