Using this CMD technique from TCC

Code:
c:\users\jlc\utils>ver

TCC  24.02.48 x64   Windows 7 [Version 6.1.7601]
Is it possible to make TCC execute this .CMD script, without modification to the .CMD script, without TCC generating errors?
Code:
<# : batch portion
@echo off & setlocal
set "arg=%~1"
powershell -noprofile "iex (${%~f0} | out-string)"
goto :EOF
: end batch / begin powershell #>
# display %arg%
$env:arg
$x="123"
$x
While I am using PowerShell code for this example, this technique can be used with other scripting languages.

I have been doing;
Code:
c:\users\jlc\utils>cmd /c test.cmd
123
...in order to use this technique with no errors from TCC.

Joe
 
This is not specific to CMD/PowerShell. It can be used for any script language.

Here's an example for VBScript;
Code:
<!-- : Begin batch script
@echo off
cscript //nologo "%~f0?.wsf"
exit /b

----- Begin wsf script --->
<job><script language="VBScript">
  WScript.Echo "VBScript from a hybrid batch"
</script></job>
I believe this is called a Hybrid Batch file. I mainly use them with VBScript on cmd.exe systems, but there is now a need to incorporate with PowerShell on cmd.exe systems.

My only other option would be to put the script code into an NTFS Stream, and run it from there, but that means I have to change many .cmd files.

Joe
 
May 20, 2008
9,274
62
Syracuse, NY, USA
CMD not choking on a line beginning with "<" is pretty CMD specific ... yes/no?

<# ... #> is PowerShell's multi-line comment mechanism. I found this on stackoverflow (hardly documentation).
The trick is in the batch redirection priority - this line <# : will be parsed like :<# because redirection is with higher prio than the other commands. But the lines starting with : in batch files are taken as labels - i.e. not executed. Still this remains a valid powershell comment.
 

samintz

Scott Mintz
May 20, 2008
1,329
12
Solon, OH, USA
The <# ... #> syntax is a multi-line Powershell comment. https://devblogs.microsoft.com/scripting/powertip-use-powershell-multiple-line-comments/

However, I'm missing how CMD is able to ignore it. If I type that first line at a CMD prompt I get back an error that it cannot find the file.

If I type this command at a CMD prompt: ": hello"
It gets ignored, like a REM statement.
But if I type "<# : hello" I get an error that it cannot find the file specified. This is similar to what TCC does.
So CMD treats that line differently in a batch script than it does at the command line.
 
Ideally, if I had to start from scratch, and I could use TCC-RT, I would do it this way;
Code:
@echo off
type <<- endtext > %_batchname:test.vbs
WScript.echo Date
endtext
cscript //nologo %_batchname:test.vbs
However, running that from a .cmd file, it errors with;
Code:
c:\users\jlc\utils>cmd /c test.cmd
<< was unexpected at this time.
Joe
 

samintz

Scott Mintz
May 20, 2008
1,329
12
Solon, OH, USA
FWIW - in TCC, you can change the first line to just "<#" and it executes correctly. In CMD it gives a syntax error and aborts.
Code:
$ cmd /c test
The syntax of the command is incorrect.

[S:\LNX\Platform\Tools\ManufacturingApp\crypto_cli] <#

$ test hello world
hello
123
 
@vefatica;

You can run the following from cmd.exe as, for example, testvbs.cmd;
Code:
<!-- : Begin batch script
@echo off
echo cmd.exe ignores the first line of "%~f0"
echo cscript will see the batch commands as part of the comments of the .wsf file
echo.
set NameOfWSF="%~f0?.wsf"
echo cscript will be executing "%~f0" as a .wsf file
echo cscript will execute this file as %NameOfWSF%
echo If cscript tries to execute this file as "%~f0.wsf", it will fail.
cscript //nologo "%~f0.wsf"
cscript //nologo "%~f0?.wsf"
exit /b

----- Begin wsf script --->
<job><script language="VBScript">
  WScript.Echo "VBScript from a hybrid batch"
</script></job>
This is what happens when it is run from cmd.exe;
Code:
c:\utils>testvbs.cmd
cmd.exe ignores the first line of "c:\utils\testvbs.cmd"
cscript will see the batch commands as part of the comments of the .wsf file

cscript will be executing "c:\utils\testvbs.cmd" as a .wsf file
cscript will execute this file as "c:\utils\testvbs.cmd?.wsf"
If cscript tries to execute this file as "c:\utils\testvbs.cmd.wsf", it will fail.
Input Error: Can not find script file "c:\utils\testvbs.cmd.wsf".
VBScript from a hybrid batch
When I run it from tcc.exe;
Code:
c:\users\jlc\utils>testvbs.cmd
<!-- : Begin batch script
TCC: (Sys) C:\Users\jlc\utils\testvbs.cmd [1]  The system cannot find the file specified.
 "C:\Users\jlc\utils\!--"
cmd.exe ignores the first line of "C:\Users\jlc\utils\testvbs.cmd"
cscript will see the batch commands as part of the comments of the .wsf file

cscript will be executing "C:\Users\jlc\utils\testvbs.cmd" as a .wsf file
cscript will execute this file as "C:\Users\jlc\utils\testvbs.cmd?.wsf"
If cscript tries to execute this file as "C:\Users\jlc\utils\testvbs.cmd.wsf", it will fail.
Input Error: Can not find script file "C:\Users\jlc\utils\testvbs.cmd.wsf".
VBScript from a hybrid batch
Joe
 
May 20, 2008
9,274
62
Syracuse, NY, USA
Hmmm! I figured most of that. But what is the mechanism by which cscript, when told to run "C:\Users\jlc\utils\testvbs.cmd?.wsf" actually runs "C:\Users\jlc\utils\testvbs.cmd"? That seems mysterious (or I'm missing something obvious).
 
May 20, 2008
9,274
62
Syracuse, NY, USA
At least one contributor at stackoverflow says that the mechanism by which
Code:
cscript "anyfile?.wsf"
will cause cscript.exe to run "anyfile" is undocumented. I didn't try to find out if that (that it's undocumented) is actually true.
 
Last edited: