batch file to create and write to VBScript file - batch-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.

Related

DOS v6 era batch echo and ver on same line

What I have:
I have a simple batch file with ASCII characters to make a semi graphical menu for MS DOS, drawing a box and placing text with "#echo off" at the start of my file and each line in editor beginning with "echo". I have not implemented color codes, nothing fancy yet.
Is there a way to display the output of the 'ver' command on the same line as something that is 'echo'd?
Basically, I would like to attempt and achieve and output on a line similar to:
|| <---MS-DOS ver. 6.22---> ||
Where the pipes are ASCII characters (ALT-186) and 'escape' the echo to run 'ver' then return to printing the end character (alt-186)
I remember doing something like it YEARS ago when I was fumbling around on my computer, but unlike riding a bike, I have forgotten many tricks I had at the time.
I have Googled for quite some time, the last few days between work and sleep on my free time, but everything is geared toward the Windows XP or newer command line instead of DOS. I have read many articles on batch scripting and while helpful in relearning other tricks, they are all still geared toward newer CLI. I have read up on escaping and for some reason I am just not getting it. Maybe I have spent too much time trying to figure this out on my own and have burned out? Any help or link to the proper articles will be appreciated. Sample code even better as that is how I learned way back then.
For the case of MSDOS-Version exists a simple solution.
echo #prompt $b$b $l--$V--$g $b$b > temp.bat
%comspec% /c temp.bat
This works, because $V will be translated into the MSDOS-Version, for output of other programs it's more complicated.
Attention: It only works inside a batch program, because variable expansion isn't supported on the command line.
On the command line you could use:
echo #prompt $b$b $V $b$b > temp.bat
C:\command.com /c temp.bat
Output: || <--MS-DOS Version 6.22--> ||
In MS-DOS 6.22 it's tricky to output text without line feed.
In the most cases you should build the complete line before you try to output it.
Or you can try predefined files without a linefeed.
Today I do the echo without line feed this way, ...
echo.|set /P =text without CR/LF
... but I can't say if this would have worked in the early nineties.

How to escape escape "=" or Equal to sign in batch script in a double quote string

SET "gmail5=https://mail.google.com/mail/u/5/?tab=wm&ogbl#inbox"
echo %gmail5%
The output is
H:\local\CODE\Batch scripting\powershell\Config>.\test.bat
H:\local\CODE\Batch scripting\powershell\Config>SET "gmail5=https://mail.google.com/mail/u/5/?tab=wm&ogbl#inbox"
H:\local\CODE\Batch scripting\powershell\Config>echo https://mail.google.com/mail/u/5/?tab=wm & ogbl#inbox
https://mail.google.com/mail/u/5/?tab=wm
'ogbl#inbox' is not recognized as an internal or external command,
operable program or batch file.
I checked the StackOverflow most of the post said that anything in "" is escaped along with =. I cannot figure out,why it gets recognized in echo. My use case is to use these strings in another batch script for vdesk.
vdesk create:4
vdesk on:1 run:%gmail5%
Batch uses & to separate different commands on one line. It does not assume that "..." is a string - it might be, however unlikely, that the " is a legitimate character as a command parameter.
Hence, you need to escape the & with a caret (^) which should work with all "awkward" characters except % for which the escape is % itself.
the thing you are trying to accomplish can be done in following way,your code was all right except every time when you print special characters use ^ or Cmd will confuse it for &(And) operater.
#echo off
SET "gmail5=https://mail.google.com/mail/u/5/?tab=wm^&ogbl#inbox"
echo %gmail5%
pause >nul

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

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.

Writing data using batch files

So I am using the "#echo" command to write data files in batch.
"#echo set value=%userinput%>> test2.bat"
The idea of this is to create a batch file named test2.bat that has 1 line of text which should be "set value=%userinput%" user input being what ever the user typed. The problem is that when every the users input is only a single digit such as 1, 2, 3, and so fourth, the command won't function. If I turned the 1 into a 10, then the command works but if I chose anything between 1 and 9 it just won't work the way it should. Could someone explain to me why? and recommended work around?
>> test2.bat echo set value=%userinput%
A digit directly before a redirector redirects that device number (0=stdin, 1=stdout, 2=stderr, 3-9 undefined)
If you want exactly one line in your file, use >. >> appends to any existing data; > recreates the file.
unless you want the literal % in your file, when you should use %% in place of each literal % you are using. %var% evaluates var, %%var%% puts the literal %var% because % "escapes" %
Due to redirection notation being in the form 1> & 2> your output is eating those input digits. I think that it is easier to wrap your echo command into parentheses to prevent it instead of placing the redirection notation at the start of the line.
#(echo set value=%userinput%)>>test2.bat

Ignore percent sign in batch file

I have a batch file which moves files from one folder to another. The batch file is generated by another process.
Some of the files I need to move have the string "%20" in them:
move /y "\\myserver\myfolder\file%20name.txt" "\\myserver\otherfolder"
This fails as it tries to find a file with the name:
\\myserver\myfolder\file0name.txt
Is there any way to ignore %? I'm not able to alter the file generated to escape this, such as by doubling percent signs (%%), escaping with / or ^ (caret), etc.
You need to use %% in this case. Normally using a ^ (caret) would work, but for % signs you need to double up.
In the case of %%1 or %%i or echo.%%~dp1, because % indicates input either from a command or from a variable (when surrounded with %; %variable%)
To achieve what you need:
move /y "\\myserver\myfolder\file%%20name.txt" "\\myserver\otherfolder"
I hope this helps!
The question's title is very generic, which inevitably draws many readers looking for a generic solution.
By contrast, the OP's problem is exotic: needing to deal with an auto-generated batch file that is ill-formed and cannot be modified: % signs are not properly escaped in it.
The accepted answer provides a clever solution to the specific - and exotic - problem, but is bound to create confusion with respect to the generic question.
If we focus on the generic question:
How do you use % as a literal character in a batch file / on the command line?
Inside a batch file, always escape % as %%, whether in unquoted strings or not; the following yields My %USERNAME% is jdoe, for instance:
echo My %%USERNAME%% is %USERNAME%
echo "My %%USERNAME%% is %USERNAME%"
On the command line (interactively) - as well as when using the shell-invoking functions of scripting languages - the behavior fundamentally differs from that inside batch files: technically, % cannot be escaped there and there is no single workaround that works in all situations:
In unquoted strings, you can use the "^ name-disrupter" trick: for simplicity, place a ^ before every % char, but note that you're not technically escaping % that way (see below for more); e.g., the following again yields something like My %USERNAME% is jdoe:
echo My ^%USERNAME^% is %USERNAME%
In double-quoted strings, you cannot escape % at all, but there are workarounds:
You can use unquoted strings as above, which then requires you to additionally ^-escape all other shell metacharacters, which is cumbersome; these metacharacters are: <space> & | < > "
Alternatively, unless you're invoking a batch file, , you can individually double-quote % chars as part of a compound argument (most external programs and scripting engines parse a compound argument such as "%"USERNAME"%" as verbatim string %USERNAME%):
some_exe My "%"USERNAME"%" is %USERNAME%
From scripting languages, if you know you're calling a binary executable, you may be able to avoid the whole problem by forgoing the shell-invoking functions in favor of the "shell-free" variants, such as using execFileSync instead of execSync in Node.js.
Optional background information re command-line (interactive) use:
Tip of the hat to jeb for his help with this section.
On the command line (interactively), % can technically not be escaped at all; while ^ is generally cmd.exe's escape character, it does not apply to %.
As stated, there is no solution for double-quoted strings, but there are workarounds for unquoted strings:
The reason that "^ name-disrupter" trick (something like ^%USERNAME^%) works is:
It "disrupts" the variable name; that is, in the example above cmd.exe looks for a variable named USERNAME^, which (hopefully) doesn't exist.
On the command line - unlike in batch files - references to undefined variables are retained as-is.
Technically, a single ^ inside the variable name - anywhere inside it, as long as it's not next to another ^ - is sufficient, so that %USERNAME^%, for instance, would be sufficient, but I suggest adopting the convention of methodically placing ^ before each and every % for simplicity, because it also works for cases such as up 20^%, where the disruption isn't even necessary, but is benign, so you can apply it methodically, without having to think about the specifics of the input string.
A ^ before an opening %, while not necessary, is benign, because ^ escapes the very next character, whether that character needs escaping - or, in this case, can be escaped - or not. The net effect is that such ^ instances are ultimately removed from unquoted strings.
Largely hypothetical caveat: ^ is actually a legal character in variable names (see jeb's example in the comments); if your variable name ends with ^, simply place the "disruptive" ^ somewhere else in the variable name, as long as it's not directly next to another ^ (as that would cause a ^ to appear in the resulting string).
That said, in the (very unlikely) event that your variable has a name such as ^b^, you're out of luck.
In batch files, the percent sign may be "escaped" by using a double percent sign ( %% ).
That way, a single percent sign will be used within the command line. from http://www.robvanderwoude.com/escapechars.php
I think I've got a partial solution working. If you're only looking to transfer files that have the "%20" string in their name and not looking for a broader solution, you can make a second batch file call the first with %%2 as the second parameter. This way, when your program tries to fetch the second parameter when it hits the %2 in the text name, it will replace the %2 with an escaped %2, leaving the file name unchanged.
Hope this works!
How to "escape" inside a batch file withoput modify the file**
The original question is about a generated file, that can't be modified, but contains lines like:
move /y "\\myserver\myfolder\file%20name.txt" "\\myserver\otherfolder"
That can be partly solved by calling the script with proper arguments (%1, %2, ...)
#echo off
set "per=%%"
call generated_file.bat %%per%%1 %%per%%2 %%per%%3 %%per%%4
This simply sets the arguments to:
arg1="%1"
arg2="%2"
...
How to add a literal percent sign on the command line
mklement0 describes the problem, that escaping the percent sign on the command line is tricky, and inside quotes it seems to be impossible.
But as always it can be solved with a little trick.
for %Q in ("%") do echo "file%~Q20name.txt"
%Q contains "%" and %~Q expands to only %, independent of quotes.
Or to avoid the %~ use
for /F %Q in ("%") do echo "file%Q20name.txt"
You should be able to use a caret (^) to escape a percent sign.
Editor's note: The link is dead now; either way: It is % itself that escapes %, but only in batch files, not at the command prompt; ^ never escapes %, but at the command prompt it can be used indirectly to prevent variable expansion, in unquoted strings only.
The reason %2 is disappearing is that the batch file is substituting the second argument passed in, and your seem to not have a second argument. One way to work around that would be to actually try foo.bat ^%1 ^%2... so that when a %2 is encountered in a command, it is actually substituted with a literal %2.

Resources