1

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?

  • It's pointless fixing your writing script until we've determined what the content of your written script should say! It doesn't look correct at all! You stated that the variable `%pof%` is defined in another file, what about the variable `%_filename%`? The written script doesn't need delayed expansion, your syntax for setting variables is not the recommended way, your `if` syntax could use some improvement too and there's a distinct lack of doublequotes in there too. In your writing script, there are also improvements to be made. – Compo Jun 25 '20 at 19:00
  • Escape the `%`, `echo ^%var^%` – Nico Nekoru Jun 25 '20 at 19:27
  • 4
    @NekoMusume - You escape `%` with another `%`, not a `^`. – SomethingDark Jun 25 '20 at 19:35
  • @SomethingDark Carets work too and doesn't that also need delayed expansion? – Nico Nekoru Jun 25 '20 at 19:36
  • @NekoMusume - Carets don't work and have never worked. https://i.imgur.com/Mq2vDzr.png And the delayed expansion thing you're thinking of is using `call %%var%%` to avoid enabling delayed expansion and just using `!var!`. – SomethingDark Jun 25 '20 at 19:42
  • 2
    @NekoMusume - This is another one of those "command-line-vs-script" things where `^%` only works from the command line (as I demonstrated in my previous comment.) – SomethingDark Jun 25 '20 at 19:45
  • @SomethingDark https://i.stack.imgur.com/NuRQ7.png – Nico Nekoru Jun 25 '20 at 19:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/216674/discussion-between-somethingdark-and-neko-musume). – SomethingDark Jun 25 '20 at 19:47

3 Answers3

2

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"
Compo
  • 36,585
  • 5
  • 27
  • 39
0

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
ScriptKidd
  • 803
  • 1
  • 5
  • 19
0

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

Io-oI
  • 2,514
  • 3
  • 22
  • 29