How to write REM Comments in a new file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
hacxx
Posts: 57
Joined: 09 Apr 2015 13:18

How to write REM Comments in a new file

#1 Post by hacxx » 14 Jan 2021 14:55

Hi,

I'm trying to build a exe compressor for exes. The idea is to simply pack the exe with UPX and create a rar archive with the file inside. The compressed rar file is then embed with the final exe that will decompress the rar and execute the exe. The purpose is to see if the final exe is smaller than the original.

Currently i have an issue there is some REM Comments that i would like to be echo into a file using > but this doesn't pass in the command line, does anyone have a workaround?

Here is the code:

Code: Select all

echo REM  QBFC Project Options Begin >>index.bat
echo REM  HasVersionInfo: No >index.bat
echo REM  Companyname:  >index.bat
echo REM  Productname:  >index.bat
echo REM  Filedescription:  >index.bat
echo REM  Copyrights:  >index.bat
echo REM  Trademarks:  >index.bat
echo REM  Originalname:  >index.bat
echo REM  Comments:  >index.bat
echo REM  Productversion:  1. 0. 0. 0 >index.bat
echo REM  Fileversion:  1. 0. 0. 0 >index.bat
echo REM  Internalname:  >index.bat
echo REM  Appicon: %temp%\hacxx\stub.ico >index.bat
echo REM  AdministratorManifest: No >index.bat
echo REM  Embeddedfile: stub.rar >index.bat
echo REM  Embeddedfile: Rar.exe >index.bat
echo REM  QBFC Project Options End >index.bat
echo start stub.exe >index.bat
Thanks

Check my batches:
http://www.cyberlord.at/forum/?id=10589&forum=73587

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to write REM Comments in a new file

#2 Post by aGerman » 14 Jan 2021 15:01

I assume you know the difference between > and >>, right?

Steffen

hacxx
Posts: 57
Joined: 09 Apr 2015 13:18

Re: How to write REM Comments in a new file

#3 Post by hacxx » 14 Jan 2021 15:12

aGerman wrote:
14 Jan 2021 15:01
I assume you know the difference between > and >>, right?

Steffen
No i don't. I always assume that >> is at the beginning of echo to save into file.

EDIT: I know now, >> append to the file and > overwrites the file, if exists. Which is exactly what i want but only the last line gets saved into file.
Is the REM something that cmd will skip? If yes, how can i circunvent?

EDIT 2: After reading some random article i figure it out. I'm using > in multiple commands which overwrites a existing >.

Thanks

Check my batches:
http://www.cyberlord.at/forum/?id=10589&forum=73587

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How to write REM Comments in a new file

#4 Post by Squashman » 14 Jan 2021 17:20

To cut down on the amount of code you could do this.

Code: Select all

(
echo REM  QBFC Project Options Begin
echo REM  HasVersionInfo: No
echo REM  Companyname:
echo REM  Productname:
echo REM  Filedescription:
echo REM  Copyrights:
echo REM  Trademarks:
echo REM  Originalname:
echo REM  Comments:
echo REM  Productversion:  1. 0. 0. 0
echo REM  Fileversion:  1. 0. 0. 0 
echo REM  Internalname:
echo REM  Appicon: %temp%\hacxx\stub.ico
echo REM  AdministratorManifest: No
echo REM  Embeddedfile: stub.rar
echo REM  Embeddedfile: Rar.exe
echo REM  QBFC Project Options End
echo start stub.exe
)>index.bat

T3RRY
Posts: 250
Joined: 06 May 2020 10:14

Re: How to write REM Comments in a new file

#5 Post by T3RRY » 14 Jan 2021 23:05

Or if you have multiple templates, you could use a findstr approach such as this:

Code: Select all

@echo off & CD "%~dp0"
rem ID the line Call to writescript is made from to enable skipping of the call lines Delim
rem Delim must be unique to each template and not include in the template.
rem allows the line to be repositioned without haveing to modify the line ID
 Set "Template=For /F "tokens=1 Delims=:" %%1 in (' Findstr /L /N "?" "%~f0" ') Do Call :Template %%1 "%~dp0?""
rem example call output filename should only be present on the call line
 %Template:?=index.bat% #
==================== :SCRIPT BREAK ===================
Exit /B 0
====================== :FUNCTIONS ====================
:Template <%Template:?=Output Filename%> <Template Delim>
 Setlocal DisableDelayedExpansion
 Break>"%~2"
 Set "Delim=For /F "Tokens=2* Delims=%~3" %%G in ("%%l")Do"& Set "Skip=For /F "Skip=%~1 Delims=" %%l in (' Type "%~f0" ')Do"
 %Skip% >>"%~2" (
  %Delim% (
   Set "LineOut=%%G"
   Setlocal EnableDelayedExpansion
   echo/!LineOut!
   Endlocal
  )
 )
 Type "%~2"
 Endlocal
Goto :Eof
===================== :TEMPLATES =====================
=:: Emsure each template uses a unique Delim character
=::        not present elsewhere in the file
:# REM  QBFC Project Options Begin
:# REM  HasVersionInfo: No
:# REM  Companyname:
:# REM  Productname:
:# REM  Filedescription:
:# REM  Copyrights:
:# REM  Trademarks:
:# REM  Originalname:
:# REM  Comments:
:# REM  Productversion:  1. 0. 0. 0
:# REM  Fileversion:  1. 0. 0. 0 
:# REM  Internalname:
:# REM  Appicon: %temp%\hacxx\stub.ico
:# REM  AdministratorManifest: No
:# REM  Embeddedfile: stub.rar
:# REM  Embeddedfile: Rar.exe
:# REM  QBFC Project Options End
:# start stub.exe
======================================================

Post Reply