A clever way to embed a entir text file within a batch file?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

A clever way to embed a entir text file within a batch file?

#1 Post by MKANET » 31 Mar 2012 21:42

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.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: A clever way to embed a entir text file within a batch f

#2 Post by Aacini » 31 Mar 2012 23:58

There are several ways of do that. One of the simplest ones is this:

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...
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 ^<a This is a tag ^<b^>This is b tag^</b^> end of a tag^</a^>

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: A clever way to embed a entir text file within a batch f

#3 Post by dbenham » 01 Apr 2012 00:41

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.

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: A clever way to embed a entir text file within a batch f

#4 Post by foxidrive » 01 Apr 2012 04:22

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.

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: A clever way to embed a entir text file within a batch f

#5 Post by !k » 01 Apr 2012 04:37

Speed Writing a File viewtopic.php?p=11845

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: A clever way to embed a entir text file within a batch f

#6 Post by dbenham » 01 Apr 2012 07:26

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.

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

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: A clever way to embed a entir text file within a batch f

#7 Post by Aacini » 01 Apr 2012 11:32

Well, here is another one for the collection! :wink:

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

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: A clever way to embed a entir text file within a batch f

#8 Post by Liviu » 01 Apr 2012 13:05

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.

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

tonysathre
Posts: 14
Joined: 20 Mar 2012 10:07

Re: A clever way to embed a entir text file within a batch f

#9 Post by tonysathre » 02 Apr 2012 07:03

Same mkanet from the SageTV forums?

Tony

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: A clever way to embed a entir text file within a batch f

#10 Post by miskox » 02 Apr 2012 12:05

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: 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

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: A clever way to embed a entir text file within a batch f

#11 Post by MKANET » 02 Apr 2012 16:24

Wow thanks for all the replies. I couldn't have done it without you guys!

Post Reply