A clever way to embed a entir text file within a batch file?
Moderator: DosItHelp
A clever way to embed a entir text file within a batch file?
My batch file depends on an xsl stylesheet. I would like to include the entire contents of the xsl file within my batch file; and, create the xsl file when necessary via the batch file. I need to have it self contained.
Is this possible? I've seen instances where the entire contents of a text file is loaded into a single variable in the batch file; however, this would be the opposite of that.
Is this possible? I've seen instances where the entire contents of a text file is loaded into a single variable in the batch file; however, this would be the opposite of that.
Re: A clever way to embed a entir text file within a batch f
There are several ways of do that. One of the simplest ones is this:However, if the file contains special batch characters, like > < | & etc., you must escape they with a ^ character placed before. For example:
Code: Select all
@echo off
rem Anything goes here...
if exist myfile.xsl goto fileExists
(
echo Line 1 of the file
echo Line 2 of the file
echo Etc...
) > myfile.xsl
:fileExist
rem Continue here...
Code: Select all
echo ^<a This is a tag ^<b^>This is b tag^</b^> end of a tag^</a^>
Re: A clever way to embed a entir text file within a batch f
This should be very easy.
You have two concerns:
1) Embed the xsl in a way that does not interfere with the batch file operation.
Embed option 1: put the xsl at the end of the batch file, and use EXIT /B before hand to prevent the batch from falling through to the xsl.
Embed option 2: put the xsl in the middle and use GOTO and a :label to skip over the xsl
2) Make sure that each line of XSL has a marker that allows FINDSTR to find it
FINDSTR has limited regular expression capability that should make it easy to pull out all of the XSL on demand.
Use "%~f0" to get the full path to the executing batch file.
To write the XSL content to a temp file, use:
Or you can pipe the XSL content directly into a command
Dave Benham
You have two concerns:
1) Embed the xsl in a way that does not interfere with the batch file operation.
Embed option 1: put the xsl at the end of the batch file, and use EXIT /B before hand to prevent the batch from falling through to the xsl.
Code: Select all
@echo off
rem batch file
...
...
exit /b
xsl content goes at end of document
Embed option 2: put the xsl in the middle and use GOTO and a :label to skip over the xsl
Code: Select all
@echo off
rem part 1 of batch
...
...
goto :skipXSL
xsl goes here
:skipXSL
rem part 2 of batch
...
...
2) Make sure that each line of XSL has a marker that allows FINDSTR to find it
FINDSTR has limited regular expression capability that should make it easy to pull out all of the XSL on demand.
- The XML declaration can be identified using "<?xml.*?>"
- XSL opening tags can be identified using "<xsl:.*>"
- XSL closing tags can be identified using "</xsl:.*>"
- Any line that doesn't have any of the above can be marked with an XML comment and identified using "<!--.*-->"
Use "%~f0" to get the full path to the executing batch file.
To write the XSL content to a temp file, use:
Code: Select all
findstr /r /c:"<?xml.*?>" /c:"<xsl:.*>" /c:"</xsl:.*>" /c:"<!--.*-->" "%~f0" >tempFileSpec
Or you can pipe the XSL content directly into a command
Code: Select all
findstr /r /c:"<?xml.*?>" /c:"<xsl:.*>" /c:"</xsl:.*>" /c:"<!--.*-->" "%~f0" | xslConsumer.exe
Dave Benham
Re: A clever way to embed a entir text file within a batch f
Here's another way:
Code: Select all
@echo off
echo hello world
more "%~0" +5 >file.txt
pause
goto :EOF
Researchers have recently discovered that modern use of antibiotics
has wreaked havoc on the health and content of our gut bacteria.
In turn, these changes have altered how our metabolisms work,
possibly making us more prone to getting fat.
Re: A clever way to embed a entir text file within a batch f
Speed Writing a File viewtopic.php?p=11845
Re: A clever way to embed a entir text file within a batch f
I had forgotten about the MORE skip option technique referred to by !k and foxidrive.
Instead of hard coding the number of lines to skip, or subtracting the number of lines to skip from the total number of lines in the file as computed by FIND - you can use FINDSTR to locate the beginning of your XSL text.
As stated before, instead of redirecting to a temp file, you can pipe the output directly into a command.
One warning: As discovered by Squashman, MORE cannot print more than 65534 lines without pausing, even if redirected or piped. Probably not an issue here, but...
Dave Benham
Instead of hard coding the number of lines to skip, or subtracting the number of lines to skip from the total number of lines in the file as computed by FIND - you can use FINDSTR to locate the beginning of your XSL text.
Code: Select all
@echo off
rem batch file content
::
::
for /f "delims=:" %%N in ('findstr /nxl /c:"::Begin XSL" "%~f0"') do (
more +%%N "%~f0" >tempFile
)
::
::
exit /b
::Begin XSL
<?xml version="1.0"?>
.
.
.
As stated before, instead of redirecting to a temp file, you can pipe the output directly into a command.
One warning: As discovered by Squashman, MORE cannot print more than 65534 lines without pausing, even if redirected or piped. Probably not an issue here, but...
Dave Benham
Re: A clever way to embed a entir text file within a batch f
Well, here is another one for the collection!
Code: Select all
@echo off
setlocal EnableDelayedExpansion
rem Anything goes here...
if exist myfile.xsl goto myXslFileEnd
set start=
for /F "skip=1 delims=:" %%i in ('findstr /N ":myXslFile" "%~F0"') do (
if not defined start (
set start=%%i
) else (
set /A count=%%i-start-1
)
)
< "%~F0" (
for /L %%i in (1,1,%start%) do set /P =
for /L %%i in (1,1,%count%) do (
set line=
set /P line=
echo(!line!
)
) > myfile.xsl
goto myXslFileEnd
:myXslFileBegin
Place here the contents of the xsl file
in the exact original way. You may even
copy a paste the xsl file here.
:myXslFileEnd
rem Continue here
Re: A clever way to embed a entir text file within a batch f
While the other replies answered the question, and did so in a general way, here is a possible alternative for XSLT in particular. It's not pretty, and it only works for XSLTs which don't require the <?xml version="1.0"?> declaration. But in those special cases, you can do away with the temporary file, and actually use the batch file itself as the XSLT. Essentially, the trick is to embed the batch code inside an <!--XML comments --> block. Cmd parses the first line as input redirection into a label, which apparently "works" i.e. gives no "missing !-- file" error.
Liviu
Code: Select all
<!-- ::
@echo off
rem body of batch code goes here
goto :eof
-->
<!-- body of XSLT goes below -->
<!-- note: it's too late to insert <?xml version="1.0"?> at this point -->
Liviu
-
- Posts: 14
- Joined: 20 Mar 2012 10:07
Re: A clever way to embed a entir text file within a batch f
Same mkanet from the SageTV forums?
Tony
Tony
Re: A clever way to embed a entir text file within a batch f
Hi all,
I am using the following method (this is the reason why I wanted this viewtopic.php?f=3&t=3089&start=0 .
Then I changed aGerman's batch to more 'user friendly'. I just couldn't make his version output a fixed length output. But his approach is that what I wanted - very clever idea in a way he done it.:
Code above is similar to aGerman's but it produces fixed length output I can then embed to .cmd.
Take a look here:
http://smart.code-makers.net/BatchGames/TicTacToe2.zip
Here you will find a method of creating mouse.exe from batch file.
Here is a part of it I use for creating an UnZIP.exe:
My unzip.ex_ is 75000+ bytes long, expanded unzip.exe has 159000+ bytes in size.
And now to answer OP's question:
1. makecab original_excel_file (So it is compressed so you have to embed less bytes.)
2. use the above .cmd (modified aGerman's)
3. Embed it into your .cmd batch file.
Hope this helps,
Saso
I am using the following method (this is the reason why I wanted this viewtopic.php?f=3&t=3089&start=0 .
Then I changed aGerman's batch to more 'user friendly'. I just couldn't make his version output a fixed length output. But his approach is that what I wanted - very clever idea in a way he done it.:
Code: Select all
@echo off &setlocal
set "infile=a.exe"
set "outfile=a.txt"
set colmax=48
cd /d "%~dp0"
if not exist "%infile%" goto :eof
if exist %outfile% del %outfile%
set "tmpf=#.tmp~"
set "tmpf2=#2.tmp"
del "%tmpf%" 2>nul
for %%i in ("%infile%") do (
set /a size=%%~zi || goto :eof
fsutil file createnew "%tmpf%" %%~zi >nul || goto :eof
)
set col=0
set niz=&rem
set /a x=1
fc /b "%infile%" "%tmpf%">%tmpf2%
for /f "skip=1 tokens=1,2 delims=: " %%i in (%tmpf2%) do call :DELAJ %%i %%j
for /l %%i in (%x% 1 %size%) do call :DELAJ1
echo %niz%>>%outfile%
del "%tmpf%"
del "%tmpf2%"
goto :EOF
:DELAJ
set /a y=0x%1
for /l %%k in (%x% 1 %y%) do call :DELAJ1
set niz=%niz%%2
set /a col=col+1
if %col%==%colmax% call :DELAJ2
set /a x=y+2
goto :EOF
:DELAJ1
set /a col=col+1
set niz=%niz%00
if %col%==%colmax% call :DELAJ2
goto :EOF
:DELAJ2
set /a col=0
echo %niz%>>%outfile%
set niz=&rem
goto :EOF
Code above is similar to aGerman's but it produces fixed length output I can then embed to .cmd.
Take a look here:
http://smart.code-makers.net/BatchGames/TicTacToe2.zip
Here you will find a method of creating mouse.exe from batch file.
Here is a part of it I use for creating an UnZIP.exe:
Code: Select all
:MakeUNZIPEXE
If Exist %temp%\UNZIP.EXE goto :EOF
for %%b in (
"4D53434600000000FD260100000000002C00000000000000030101000100000000000000460000000500010000700200"
"00000000000068401A7B2000756E7A69702E65786500E951EBDE783A0080434BEC3A7B7C53559AF7E6D1A625E506486B"
.
.
.
"91A646090E6B040B17665B533494B614B60C690AA94A1FE203431790857B1D74106192686F2EC5EE2CE3FA73C61DB29D"
"C01278A5CD323F1E06FF59FC05") Do >>%temp%\unzip.exe (Echo.For b=1 To len^(%%b^) Step 2
Echo WScript.StdOut.Write Chr^(CByte^("&H"^&Mid^(%%b,b,2^)^)^) : Next)
Cscript /b /e:vbs %temp%\unzip.exe > %temp%\unzip.ex_
Expand -r %temp%\unzip.ex_ %temp%\unzip.exe>nul 2>&1
if exist %temp%\unzip.ex_ del %temp%\unzip.ex_
Goto :Eof
My unzip.ex_ is 75000+ bytes long, expanded unzip.exe has 159000+ bytes in size.
And now to answer OP's question:
1. makecab original_excel_file (So it is compressed so you have to embed less bytes.)
2. use the above .cmd (modified aGerman's)
3. Embed it into your .cmd batch file.
Hope this helps,
Saso
Re: A clever way to embed a entir text file within a batch f
Wow thanks for all the replies. I couldn't have done it without you guys!