user13814410
user13814410

Reputation:

Echo %variables% without getting the data of the variables in

So I am trying to make a running batch file to create a new batch file which contains:

@setlocal enableextensions enabledelayedexpansion
@echo off
:remloop
set recfilepath=%pof%
set filename=%pof%
SET filename=%_filename:*\=% 
if not x%pof:bcd=%==x%pof% goto remloop
endlocal

(Variable POF has already been declared in another file) So I tried using echo command to make another batch file

echo @setlocal enableextensions enabledelayedexpansion > file2.bat
echo @echo off > file2.bat
echo :remloop >> file2.bat
echo set recfilepath=%pof% >> file2.bat
echo set filename=%pof% >> file2.bat
echo SET filename=%_filename:*\=%  >> file2.bat
echo if not x%pof:bcd=%==x%pof% goto remloop >> file2.bat
echo endlocal >> file2.bat

but the result was unexpected, the results are:

@echo off 
:remloop 
set recfilepath=C:\Users\Palm2570Playz\Desktop\djfbjfbfbj.txt 
set filename=C:\Users\Palm2570Playz\Desktop\djfbjfbfbj.txt 
SET filename=*\=  
if not xC:\Users\Palm2570Playz\Desktop\djfbjfbfbj.txt==xC:\Users\Palm2570Playz\Desktop\djfbjfbfbj.txt goto remloop 
endlocal 

The "SET filename=*= " lines was unexpected so the code cannot run correctly Is there any command to fix this?

Upvotes: 1

Views: 175

Answers (3)

Io-oI
Io-oI

Reputation: 2565

If you don't need escaping anything?!

The best and most efficient way to escape is to use no escape, use only the bat file itself to decode the second bat file with the content in Base64 itself:

Where it is possible to use CertUtil to decode File2.bat, taking advantage that it already comes with Windows.

  • Your code bat:
@(%__APPDIR__%CertUtil.exe -f -decode "%~f0" "%~dp0File2.bat" >nul & goto :EOF) 

<-----BEGIN -----QGVjaG8gb2ZmICYmIHNldGxvY2FsIGVuYWJsZWRlbGF5ZWRleHBhbnNpb24NCj
psb29wDQpzZXQgInJlY2ZpbGVwYXRoPSVwb2YlIiAmJiBjbWQuZXhlIC92Om9uIC9jICJzZXQgImZpb
GVuYW1lPSFyZWNmaWxlcGF0aDoqXD0hIiAiDQplY2hvOyJ4IXBvZjpiY2Q9ISJ8ZmluZC9pIC92ICJ4
IXBvZiEiID5udWwgJiYgZ290byA6bG9vcCB8fCBlbmRsb2NhbCAmIGdvdG86RU9G-----END ----->
  • The File2.bat content result:
@echo off && setlocal enabledelayedexpansion
:loop
set "recfilepath=%pof%" && cmd.exe /v:on /c "set "filename=!recfilepath:*\=!" "
echo;"x!pof:bcd=!"|find/i /v "x!pof!" >nul && goto :loop || endlocal & goto:EOF

  • This code above has some suggestions, liable to (critical/not), so follow code use the same method using your current code without modifications:
@(%__APPDIR__%CertUtil.exe -f -decode "%~f0" "%~dp0File2.bat" >nul & goto :EOF) 

<-----BEGIN CERTIFICATE----- QHNldGxvY2FsIGVuYWJsZWV4dGVuc2lvbnMgZW5hYmxlZGVsYX
llZGV4cGFuc2lvbg0KQGVjaG8gb2ZmDQo6cmVtbG9vcA0Kc2V0IHJlY2ZpbGVwYXRoPSVwb2YlDQpzZ
XQgZmlsZW5hbWU9JXBvZiUNClNFVCBmaWxlbmFtZT0lX2ZpbGVuYW1lOipcPSUNCmlmIG5vdCB4JXBv
ZjpiY2Q9JT09eCVwb2YlIGdvdG8gcmVtbG9vcA0KZW5kbG9jYWw= -----END CERTIFICATE----->
  • The File2.bat content result:
@setlocal enableextensions enabledelayedexpansion 
@echo off 
:remloop
set recfilepath=%pof%
set filename=%pof%
SET filename=%_filename:*\=% 
if not x%pof:bcd=%==x%pof% goto remloop
endlocal

Upvotes: 0

ScriptKidd
ScriptKidd

Reputation: 851

You can use FINDSTR / a FOR loop (eol&skip) / MORE on the batch file itself, and redirect it to file2.bat. That way no parsing is involved.

I made no edits/improvements as it's out of scope of the question.

@echo off
%__APPDIR__%findstr.exe /R "^;" "%~f0" >file2.bat
exit /b

<--- FILE2.BAT --->
;@setlocal enableextensions enabledelayedexpansion 
;@echo off 
;:remloop
;set recfilepath=%pof%
;set filename=%pof%
;SET filename=%_filename:*\=% 
;if not x%pof:bcd=%==x%pof% goto remloop
;endlocal

Upvotes: 0

Compo
Compo

Reputation: 38654

To show you what your question asks, without providing any fixes for any of the content of `file2.bat`:

@(  Echo @setlocal enableextensions enabledelayedexpansion
    Echo @echo off
    Echo :remloop
    Echo set recfilepath=%%pof%%
    Echo set filename=%%pof%%
    Echo SET filename=%%_filename:*\=%% 
    Echo if not x%%pof:bcd=%%==x%%pof%% goto remloop
    Echo endlocal)>"file2.bat"

Upvotes: 2

Related Questions