Package multiple scripts into one .cmd file
I wrote a very simple (but I find very useful) PowerShell script and created an installer for it as a .cmd file. The distribution contains two .cmd files and two .ps1 files. The installer is currently downloading all the required files from github .
My goal is to make the installer self-contained so that people can use it even behind a strict firewall.
One approach might be like this:
echo param(>!filename!
echo [string] $pfilename>>!filename!
echo )>>!filename!
...
But this approach requires escaping special characters, which can get very complicated. Besides, it needs to be automated somehow to be useful.
Another approach is to include the script access code in the .cmd file directly, mark with comments like ::begin file1
and ::end file1
, and then extract it. But how?
My first thought was to look into pl2bat
. But it turns out that it perl
has a special switch ( -x
) that just removes all the text before the line #!perl
, and the .bat file generated pl2bat
just runs perl -x
on its own. Therefore, I cannot use the same approach I am using pl2bat
.
Any ideas?
PS: I know about NSIS, InnoSetup and WiX. I just want my installer to be text-based, not binary, so everyone can see that it is harmless.
source to share
When I faced this same problem, I decided to use an established standard for this purpose instead of developing my own term, so I selected the "resource" element of the XML Specification . This is my method:
@echo off
setlocal EnableDelayedExpansion
rem Extract "FirstSection.txt" and place in its own file:
call :getResource FirstSection > FirstSection.txt
rem Extract "SecondSection.txt" and place in its own file:
call :getResource SecondSection > SecondSection.txt
goto :EOF
:getResource resourceId
rem Resource data start format: ^<resource id="resourceId"^>
set start=
set lines=
for /F "tokens=1,3 delims=:=>" %%a in ('findstr /N "^</*resource" "%~F0"') do (
if not defined start (
if "%1" equ "%%~b" set start=%%a
) else (
if not defined lines set /A lines=%%a-start-1
)
)
set line=0
for /F "skip=%start% tokens=1* delims=]" %%a in ('find /N /V "" ^< "%~F0"') do (
setlocal DisableDelayedExpansion
echo(%%b
endlocal
set /A line+=1
if !line! equ %lines% exit /B
)
For example:
<resource id="FirstSection">
Place here
the contents
of First Section
</resource>
<resource id="SecondSection">
The same
... for Second Section
</resource>
Antonio
source to share