I've been learning some batch programming, and decided to make a roguelike, as it's one of my favorite types of games. I have researched any information on making a roguelike in batch, but haven't found much. From the little bit I've gathered, this is the code I have so far:
#echo off
rem init starting position
set pos=6
:level
set c1=#
set c2=#
set c3=#
set c4=#
set c5=#
set c6=.
set c7=.
set c8=#
set c9=#
set c10=.
set c11=.
set c12=#
set c13=#
set c14=#
set c15=#
set c16=#
echo %c1%%c2%%c3%%c4%
echo %c5%%c6%%c7%%c8%
echo %c9%%c10%%c11%%c12%
echo %c13%%c14%%c15%%c16%
This works so far, and draws the simple 4x4 room.I made the room only 4x4 for testing purposes, so it would be simple.
Now I'm at a point where I'm not sure how to write the rest. I know I'll need to call subroutines, and get the input (WASD), but I don't know how to structure those subroutines in the file. I'd appreciate anyone's help on how to structure a batch roguelike, get input to move the player, or even just ideas about what could work.
Thanks.
I give you here a technic without CHOICE and without an External command.
It use Xcopy to get the INPUT (W,A,S,D). IN this Exemple i don't make any test of position (where is your object on the screen), So to test it go first right (D).
It's just an exemple to help you.
#echo off
:level
set c1=#
set c2=#
set c3=#
set c4=#
set c5=#
set c6=.
set c7=.
set c8=#
set c9=#
set c10=.
set c11=.
set c12=#
set c13=#
set c14=#
set c15=#
set c16=#
#echo off
setlocal enableextensions enabledelayedexpansion
set haut=
set larg=
:boucle
cls
echo WASD TO MOVE THE CURSOR Q TO QUIT&echo.
for %%a in ( !haut! ) do echo.
call:aff
Set "Key="
For /F "delims=" %%# In ('Xcopy /W "%~f0" "%~f0" 2^>Nul') Do If Not Defined Key Set "Key=%%#"
Set "Key=%Key:~-1%"
if /i %key%==q (exit /b)
if /i %key%==w goto:UP
if /i %key%==s goto:DOWN
if /i %key%==a goto:LEFT
if /i %key%==d goto:RIGHT
:LEFT
set larg=!larg:~0,-1!
goto boucle
:RIGHT
set larg= !larg!
goto boucle
:UP
set haut=!haut:~0,-2!
goto boucle
:DOWN
set haut=!haut! a
goto boucle
:aff
echo !larg!%c1%%c2%%c3%%c4%
echo !larg!%c5%%c6%%c7%%c8%
echo !larg!%c9%%c10%%c11%%c12%
echo !larg!%c13%%c14%%c15%%c16%
#ECHO OFF
setlocal enableextensions enabledelayedexpansion
SET /a maxx=13
SET /a maxy=7
SET /a level=1
:: current x,y position in cx, cy - set start position
SET /a cx=3
SET /a cy=2
SET objdesc16=*Book of something
CALL :putobjects 1 6 3 16
SET userprompt=Q always Quits - MOVE WASD
SET moveoptions=wasd
:newlevel
:: Set physical map
CALL :map%level%
:loop
CALL :showmap
CALL :getmove
IF /i "%key%"=="Q" GOTO :eof
CALL :makemove
GOTO loop
:getmove
ECHO(%userprompt%
SET "userprompt="
:getmovel
Set "key="
For /F "delims=" %%Z In ('Xcopy /W "%~f0" "%~f0" 2^>Nul') Do If Not Defined Key Set "key=%%Z"
IF NOT DEFINED key GOTO getmovel
Set "key=%key:~-1%"
IF /i "%key%"=="Q" GOTO :eof
IF /i "%moveoptions%"=="!moveoptions:%key%=!" GOTO getmovel
GOTO :eof
:: make a move given KEY
:makemove
if /i %key%==w CALL :movedir 0 -1
if /i %key%==a CALL :movedir -1 0
if /i %key%==s CALL :movedir 0 1
if /i %key%==d CALL :movedir 1 0
GOTO :eof
:movedir
SET /a $m=%1+%cx%
SET /a $n=%2+%cy%
CALL :mapsquare %$m% %$n%
IF "%$s%"=="." SET cy=%$n%&SET cx=%$m%&CALL :setprompt wasd&GOTO :EOF
IF "%$s%"=="#" CALL :setprompt ouch&GOTO :EOF
GOTO :eof
:: standard userprompts
:setprompt
IF /i "%1"=="wasd" SET userprompt=MOVE WASD&GOTO :EOF
IF /i "%1"=="ouch" SET userprompt=OUCH!&GOTO :EOF
GOTO :EOF
:map1
CALL :initmap
:: Special map symbols for level 1 (stairs, etc)
CALL :mapsymbol 4 2 ?
GOTO :eof
:mapsymbol
SET "c_%1_%2=%3"
GOTO :eof
:: set border to '#', internal to '.'
:initmap
FOR /l %%y IN (0,1,%maxy%) DO (
FOR /l %%x IN (0,1,%maxx%) DO (
SET c_%%x_%%y=.
IF %%x==0 SET c_%%x_%%y=#
IF %%y==0 SET c_%%x_%%y=#
IF %%x==%maxx% SET c_%%x_%%y=#
IF %%y==%maxy% SET c_%%x_%%y=#
)
)
GOTO :eof
:: map on new screen
:showmap
CLS
FOR /l %%y IN (0,1,%maxy%) DO (
SET "mapline="
FOR /l %%x IN (0,1,%maxx%) DO (
CALL :mapsquare %%x %%y
SET mapline=!mapline!!$s!
)
ECHO(!mapline!
)
GOTO :eof
:: get the symbol to show in x,y
:mapsquare
:: From map
SET $s=!c_%1_%2!
:: Object
IF DEFINED obj%level%_%1_%2 CALL :getobj %level%_%1_%2
:: Character
IF %cx%==%1 IF %cy%==%2 SET $s=#
SET $s=%$s:~0,1%
GOTO :eof
:: Get object description for object (%1) to $s
:getobj
SET $s=!obj%1!
SET $s=!objdesc%$s%!
GOTO :eof
:: PUTOBJECTS onlevel at-x at-y object#
:putobjects 1 1 3 16
SET "obj%1_%2_%3=%4"
GOTO :eof
This code may be useful.
The main loop simply repeats showmap, getmove, makemove.
makemove is the critical routine that needs to be expanded to build your game. The principle is to see which moves are valid for the next input, place those in moveoptions and generate an appropriate userprompt
Otherwise, your mapcells are in c_x_y and objects in obj_level_x_y I simply chose the object-description to be displaysymbolDESCRIPTION in objdescX where the X is stored in obj_level_x_y. In this way, you can set up extra object characteristics simply by setting variables like objhitbonusX or objdosesX.
You could extend the scheme to opponenthealthX opponentweaponX etc.
You'd not that GOTOs are minimised to avoid spaghetti-code.
Related
I have these 2 scripts .bat working fine individually, but I want to combine them into one, so they can run their commands at the same time, instead of consecutively.
Let me explain my idea:
I'm testing a program made by a German, which consists of showing bmp images on your cmd console (also using gifs frames on a loop to show animated images as well). the plugin made for this to work is called Insertbmp.exe:
#echo off
title BMP console
cd bin\sprimg
pause
cls
color 0f
mode 81,41
cls
set name=openedk
:loop1
openedk\insertbmp /p:"%name%%loop%.bmp" /x:0 /y:0 /z:0
set /a loop=%loop%+1
if not exist openedk\%name%%loop%.bmp goto endofloop1
timeoutms 100
goto loop1
:endofloop1
set /a lo=%lo%+1
set loop=0
if %lo% lss 4 goto loop1
set lo=0
pause
color f0
mode 62,32
cls
set name=alien
:loop2
alien\insertbmp /p:"%name%%loop%.bmp" /x:0 /y:0 /z:0
set /a loop=%loop%+1
if not exist alien\%name%%loop%.bmp goto endofloop2
timeoutms 100
goto loop2
:endofloop2
set /a lo=%lo%+1
set loop=0
if %lo% lss 4 goto loop2
set lo=0
pause
color 0f
mode 62,22
cls
set name=sleep
:loop3
sleep\insertbmp /p:"%name%%loop%.bmp" /x:0 /y:18 /z:0
set /a loop=%loop%+1
if not exist sleep\%name%%loop%.bmp goto endofloop3
timeoutms 100
goto loop3
:endofloop3
set /a lo=%lo%+1
set loop=0
if %lo% lss 3 goto loop3
set lo=0
pause
color 0f
mode 33,2
cls
echo End of TEST file
pause
exit
the other one is also a batch script made to type lines by itself, it's called ghost typer:
#echo off
:: Ghost typer
setlocal enableextensions enabledelayedexpansion
set lines=7
set "line1=This was a triumph"
set "line2=I'm making a note here: huge success"
set "line3=It's hard to overstate my satisfaction"
set "line4=Aperture science"
set "line5=We do what we must because we can"
set "line6=For the good of all of us"
set "line7=Except the ones who are dead"
for /f %%a in ('"prompt $H&for %%b in (1) do rem"') do set "BS=%%a"
for /L %%a in (1,1,%lines%) do set num=0&set "line=!line%%a!"&call :type
pause>nul
goto :EOF
:type
set "letter=!line:~%num%,1!"
set "delay=%random%%random%%random%%random%%random%%random%%random%"
set "delay=%delay:~-6%"
if not "%letter%"=="" set /p "=a%bs%%letter%" <nul
:: adjust the 3 in the line below: higher is faster typing speed
for /L %%b in (1,2,%delay%) do rem
if "%letter%"=="" echo.&goto :EOF
set /a num+=1
goto :type
Basically, what I have in mind is: run those commands simultaneously on the same line so it doesn't consecutively run them, giving me a cmd console typing whatever I want by it self, while showing me my animated images one by one which I already have on my folder.
on my current situation, it only runs the loop command before it runs the typer codes.
any suggestions?
(I'm rookie on coding anything, sorry)
I have a batch file that contains a structured array. The problem is that it is adding a space to the end of the variable. The code below will output:
Name=Joe zzzzzz
Value=1 zzzzzz
...
Where is the space after the name and value coming from, and how can I get rid of it?
Thanks!
#echo off
set len=3
set obj[0].Name=Joe
set obj[0].ID=1
set obj[1].Name=Mark
set obj[1].ID=2
set obj[2].Name=Mohan
set obj[2].ID=3
set i=0
:loop
if %i% equ %len% goto :eof
set cur.Name=
set cur.ID=
for /f "delims==. tokens=1-3" %%j in ('set obj[%i%]') do (
set cur.%%k=%%l
)
echo Name=%cur.Name%zzzzzz
echo Value=%cur.ID%zzzzzz
set /a i=%i%+1
goto loop
When I try iterating the folders with a for each approach I have no access to the current index and I've also failed to manually keep one:
#echo off
set "i=0"
set folders='dir /b /ad'
for /f "eol=: delims=" %%D in (%folders%) do (
:: echo %%D
echo %i%
set /a "i+=1"
)
When I try iterating with a fori approach based on this example I can't even get it working:
#echo off
cls
set "i=0"
:SymLoop
set folders='dir /b /ad'
if defined folders[%i%] (
echo %%folders[%i%]%%
set /a "i+=1"
GOTO :SymLoop
)
I'm aware of my total lack of knowledge on the topic so I'd appreciate any kind of correction and/or advice.
#ECHO OFF
SETLOCAL
#echo off
set /a i=0
set folders='dir /b /ad'
for /f "eol=: delims=" %%D in (%folders%) do (
REM echo %%D
CALL echo %%i%%
CALL SET "folders[%%i%%]=%%D"
set /a i+=1
)
SET fol
ECHO ---------------------------
#echo off
set /a i=0
:SymLoop
set folders='dir /b /ad'
if defined folders[%i%] (
CALL echo %%folders[%i%]%%
set /a "i+=1"
GOTO SymLoop
)
GOTO :EOF
Please refer to endless examples on SO about delayed expansion for simpler ways.
Not a good idea to use ::-comments within a (code block) as it can break the block.
set /a does not ordinarily require "quotes"
In short I want to give my friend a bat file which does the below (untested)
echo Your mother is so fat
pause
echo the recursive function computing her mass causes a stack overflow
I might copy/paste him the bat file so I don't want the punch line to be ruined. How can I hide the text? I was thinking I can store the string in a variable and before I echo it I should XOR each letter with 32. But I have no idea how to take a string, XOR each letter than echo it to display the joke. How might I hide the text? I could also BASE64 encode/decode it but IDK how to do that either if I am only using a bat file
1) Here's one way to hide the text using mshta as a command line tool (with ascii codes in this case) :
#echo off
mshta vbscript:execute("CreateObject(""Scripting.FileSystemObject"").GetStandardStream(1).Write(Chr(89) & Chr(111)& Chr(117) & Chr(114) & Chr(32) & Chr(109) & Chr(97) & Chr(109) & Chr(97) & Chr(32) ):Close")|more
2) You can use CERTUTIL to encode/decode base64/hex files but it requires a temporary file that can be silently deleted (more info ):
echo 796f7572206d616d6120697320736f20666174>"%temp%\fat.hex"
certutil -decodehex "%temp%\fat.hex" "%temp%\fat.txt" >nul 2>&1
type "%temp%\fat.txt"
del /q /f "%temp%\fat.txt"
3) Dbenham's hex print function
#echo off
setlocal
::Define a Linefeed variable
set LF=^
::above 2 blank lines are critical - do not remove.
::Create a string variable with encoded TABs
call :hexprint "0x790x6f0x750x720x200x6d0x610x6d0x610x200x690x730x200x730x6f0x200x660x610x74" var
echo %var%
exit /b
:hexPrint string [rtnVar]
for /f eol^=^%LF%%LF%^ delims^= %%A in (
'forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo(%~1"'
) do if "%~2" neq "" (set %~2=%%A) else echo(%%A
exit /b
4) carlos' genCar function that uses MAKECAB:
#echo off
setlocal
break>fat.txt
for %%# in (121 111 117 114 32 109 97 109 97 32 105 115 32 115 111 32 102 97 116) do (
call :genChar %%#
type %%#.chr>>fat.txt
del /q /f %%#.chr >nul 2>&1
)
type fat.txt
del /q /f fat.txt
goto :eof
:genChar
setlocal
set "USAGE=echo:Usage: Supply an integer 0-255& goto :EOF"
if "%~1" equ "" %USAGE%
set /a "val=%~1" 2>nul
if "%~1" neq "%val%" %USAGE%
if %~1 lss 0 %USAGE%
if %~1 gtr 255 %USAGE%
set tempfile=%~1.tmp
set "options=/d compress=off /d reserveperdatablocksize=26"
if %~1 neq 26 (type nul >"%tempfile%"
makecab %options% /d reserveperfoldersize=%~1 "%tempfile%" %~1.chr >nul
type %~1.chr | (
(for /l %%N in (1 1 38) do pause)>nul&findstr "^">"%tempfile%")
>nul copy /y "%tempfile%" /a %~1.chr /b
del "%tempfile%"
) else (copy /y nul + nul /a 26.chr /a >nul)
endlocal
for more cryptic script you can combine hem.Only MSHTA and MAKECAB solutions will work on every windows machine. FORFILES and CERTUTIL are default form Vista and above I think.It is possible to create a few more examples ...
This is a subject near and dear to my heart because I did something similar in my implementation of the classic Colossal Cave Adventure game as a Windows batch file.
Within the game script I selectively encrypt display text, variable names, and comments. The code to decode the encrypted text is embedded directly within the same script! I write the source code for the game normally, and use braces to denote what portion is to be encrypted. A function within the game is able to generated the encrypted form of itself!
I used a simple symmetric rotation cipher, so really it is more obfuscation than encryption. But that is all that is needed for both the game, and your situation.
I've extracted a simplified version of the routines and provide them below.
The first script is a standalone script that selectively encrypts text within a source file and writes the result to stdout. Simply redirect the output to a new file to get the encrypted version of the file.
selectiveROT13.bat
#echo off
:selectiveROT13 InFile
::
:: Selectively applies the simple "rotate alphabet 13 places" cipher
:: to the contents of file InFile. Only text between curly braces
:: is affected. The affected content can span multiple lines.
::
:: Writes the results to stdout.
:: Percent completion is continuously written to stderr.
::
setlocal enableDelayedExpansion
set "upper=ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set "lower=abcdefghijklmnopqrstuvwxyz"
for /l %%A in (0 1 25) do (
set /a "B=(%%A+13)%%26"
for /f %%B in ("!B!") do (
set "upper!upper:~%%A,1!=!upper:~%%B,1!"
set "lower!lower:~%%A,1!=!lower:~%%B,1!"
)
)
setlocal disableDelayedExpansion
>&2 cls
set "active="
for /f %%N in ('type %1^|find /c /v ""') do set /a "lnCnt=%%N, pct=-1"
for /f "skip=2 tokens=1,* delims=[]" %%a in ('find /v /n "" %1') do (
set "ln=%%b"
setlocal enableDelayedExpansion
set "str=A!ln!"
set "len=0"
for /L %%A in (12,-1,0) do (
set /a "len|=1<<%%A"
for %%B in (!len!) do if "!str:~%%B,1!"=="" set /a "len&=~1<<%%A"
)
set /a len-=1
set rtn=
for /l %%n in (0,1,!len!) do (
set "c=!ln:~%%n,1!"
if "!c!" equ "{" set "active=1"
if "!c!" equ "}" set "active="
if defined active if defined upper!c! for /f %%c in ("!c!") do (
if "!upper:%%c=%%c!" equ "!upper!" (
set "c=!upper%%c!"
) else (
set "c=!lower%%c!"
)
)
set "rtn=!rtn!!c!"
)
echo(!rtn!
for %%A in ("!active!") do (
endlocal
set "active=%%~A"
)
)
exit /b 0
Below is your joke program with a simplified version of the code to decode encrypted text. My original code worked with string variables, but this version works with string literals. The source script is written normally, without encryption. Braces indicate which code is to be encrypted. Besides your joke, I've included documentation and examples to demonstrate some of the features.
joke_src.bat
#echo off
setlocal enableDelayedExpansion
call :init
:: Disable delayed expansion to protect ! within string literals
setlocal disableDelayedExpansion
:: Curly braces are used to denote text that should be encrypted.
:: Encryption can span multiple lines
:: {
:::Line1
:::Line2
:::Line3
:: }
:: I defined a simple SHOW macro that expands to CALL :SHOW
:: Use the %show% macro to display encrypted text.
:: The braces can be hidden by using the undefined %{% & %}% variables
%show% %{%"Quote literals ("") must be doubled ("""") in the source"%}%
:: Here I use a FOR loop to show all encrypted lines within this script
:: that begin with :::
echo(
for /f "delims=: tokens=*" %%A in ('findstr /b ":::" "%~f0"') do %show% "%%A"
echo(
echo And now it is time for a little joke.
echo(
echo Your mother is so fat...
pause
%show% %{%"the recursive function computing her mass causes a stack overflow!"%}%
exit /b
:show Str
::{
:: Applies the simple "rotate alphabet 13 places" cipher to string Str
:: and writes the result to stdout. Consecutive quotes ("") are converted
:: into a single quote (").
::}
setlocal disableDelayedExpansion
set "str=%~1"
setlocal enableDelayedExpansion
set "str=!str:""="!^"
if defined {obfuscated} (
set "len=0"
set "str2=.!str!"
for /L %%A in (12,-1,0) do (
set /a "len|=1<<%%A"
for %%B in (!len!) do if "!str2:~%%B,1!"=="" set /a "len&=~1<<%%A"
)
set /a len-=1
set rtn=
for /l %%n in (0,1,!len!) do (
set "c=!str:~%%n,1!"
if defined {upper}!c! for /f %%c in ("!c!") do (
if "!{upper}:%%c=%%c!" equ "!{upper}!" (
set "c=!{upper}%%c!"
) else (
set "c=!{lower}%%c!"
)
)
set "rtn=!rtn!!c!"
)
) else set "rtn=!str!"
echo(!rtn!
exit /b 0
:init
set "}="
set "{="}
set "{upper}=ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set "{lower}=abcdefghijklmnopqrstuvwxyz"
for /l %%A in (0 1 25) do (
set /a "B=(%%A+13)%%26"
for /f %%B in ("!B!") do (
set "{upper}!{upper}:~%%A,1!=!{upper}:~%%B,1!"
set "{lower}!{lower}:~%%A,1!=!{lower}:~%%B,1!"
)
)
set "{obfuscated}="
set "{obfuscationTest}={A}"
if "!{obfuscationTest}:A=!" equ "!{obfuscationTest}!" set {obfuscated}=1
set "show=call :show"
exit /b
The following command will generate the encrypted version of the script:
selectiveROT13 joke_src.bat >joke.bat
Below is the encrypted form. This is what you would send to your friend. (Without the extra documentation and examples of course)
joke.bat
#echo off
setlocal enableDelayedExpansion
call :init
:: Disable delayed expansion to protect ! within string literals
setlocal disableDelayedExpansion
:: Curly braces are used to denote text that should be encrypted.
:: Encryption can span multiple lines
:: {
:::Yvar1
:::Yvar2
:::Yvar3
:: }
:: I defined a simple SHOW macro that expands to CALL :SHOW
:: Use the %show% macro to display encrypted text.
:: The braces can be hidden by using the undefined %{% & %}% variables
%show% %{%"Dhbgr yvgrenyf ("") zhfg or qbhoyrq ("""") va gur fbhepr"%}%
:: Here I use a FOR loop to show all encrypted lines within this script
:: that begin with :::
echo(
for /f "delims=: tokens=*" %%A in ('findstr /b ":::" "%~f0"') do %show% "%%A"
echo(
echo And now it is time for a little joke.
echo(
echo Your mother is so fat...
pause
%show% %{%"gur erphefvir shapgvba pbzchgvat ure znff pnhfrf n fgnpx biresybj!"%}%
exit /b
:show Str
::{
:: Nccyvrf gur fvzcyr "ebgngr nycunorg 13 cynprf" pvcure gb fgevat Fge
:: naq jevgrf gur erfhyg gb fgqbhg. Pbafrphgvir dhbgrf ("") ner pbairegrq
:: vagb n fvatyr dhbgr (").
::}
setlocal disableDelayedExpansion
set "str=%~1"
setlocal enableDelayedExpansion
set "str=!str:""="!^"
if defined {boshfpngrq} (
set "len=0"
set "str2=.!str!"
for /L %%A in (12,-1,0) do (
set /a "len|=1<<%%A"
for %%B in (!len!) do if "!str2:~%%B,1!"=="" set /a "len&=~1<<%%A"
)
set /a len-=1
set rtn=
for /l %%n in (0,1,!len!) do (
set "c=!str:~%%n,1!"
if defined {hccre}!c! for /f %%c in ("!c!") do (
if "!{hccre}:%%c=%%c!" equ "!{hccre}!" (
set "c=!{hccre}%%c!"
) else (
set "c=!{ybjre}%%c!"
)
)
set "rtn=!rtn!!c!"
)
) else set "rtn=!str!"
echo(!rtn!
exit /b 0
:init
set "}="
set "{="}
set "{hccre}=ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set "{ybjre}=abcdefghijklmnopqrstuvwxyz"
for /l %%A in (0 1 25) do (
set /a "B=(%%A+13)%%26"
for /f %%B in ("!B!") do (
set "{hccre}!{hccre}:~%%A,1!=!{hccre}:~%%B,1!"
set "{ybjre}!{ybjre}:~%%A,1!=!{ybjre}:~%%B,1!"
)
)
set "{boshfpngrq}="
set "{boshfpngvbaGrfg}={N}"
if "!{boshfpngvbaGrfg}:A=!" equ "!{boshfpngvbaGrfg}!" set {boshfpngrq}=1
set "show=call :show"
exit /b
The beauty of this system is that both joke.bat and joke_src.bat generate the exact same output:
Quote literals (") must be doubled ("") in the source
Line1
Line2
Line3
And now it is time for a little joke.
Your mother is so fat...
Press any key to continue . . .
the recursive function computing her mass causes a stack overflow!
Another nice feature is that selectiveROT13.bat can be applied to joke.bat to regenerate the original un-encrypted source.
This code creates a base64 encoded file:
#echo off
set "var=the recursive function computing her mass causes a stack overflow"
>file.tmp echo %var%
certutil -f -encode file.tmp file.tmp2 >nul
echo file.tmp2|find /v "-----" >file.txt
del file.tmp?
pause
and you can use the file like so (adding echo at the start of each line of the encoded file):
#echo off
cls
echo Your mother is so fat
pause
(
echo dGhlIHJlY3Vyc2l2ZSBmdW5jdGlvbiBjb21wdXRpbmcgaGVyIG1hc3MgY2F1c2Vz
echo IGEgc3RhY2sgb3ZlcmZsb3cNCg==
)>file.tmp
certutil -f -decode file.tmp file.txt >nul
timeout /t 2 /nobreak >nul
type file.txt
timeout /t 5 /nobreak >nul
I think you can just replace some chars with others in pure BAT without any temporary files using following string replacing script.
%str:old_char=new_char%
For example, I have defined some encoding and decoding functions. The codes are attached here, and it will print what you want.
#echo off
set str1=Y urke ohtrkmsks kfao
set str2=ohtkrtcursmvtkfuncom nkc epuomngkhtrkeasskcaustskaksoacik vtrfl w
call :decode "%str1%"
call :decode "%str2%"
pause
goto :eof
:decode
set "str=%~1"
set str=%str: =#%
set str=%str:k= %
set str=%str:i=k%
set str=%str:m=i%
set str=%str:e=m%
set str=%str:t=e%
set str=%str:o=t%
set str=%str:#=o%
echo %str%
goto :eof
I also attached the encoding script below.
#echo off
set str1=Your mother is so fat
set str2=the recursive function computing her mass causes a stack overflow
call :encode "%str1%"
call :encode "%str2%"
pause
goto :eof
:encode
set "str=%~1"
set str=%str:o=#%
set str=%str:t=o%
set str=%str:e=t%
set str=%str:m=e%
set str=%str:i=m%
set str=%str:k=i%
set str=%str: =k%
set str=%str:#= %
echo %str%
goto :eof
I'm giving user options to select certain properties on command line. User want to select comma separated list. So user will see something like this
Prop1
Prop2
Prop3
So user can give 1,3 and hit enter and user will get 1 and 3 databases created.
But these Prop1, Prop2, Prop3 has unique names and ids, which I've given in same batch script as properties and I want to concat all those depending on options user has selected and pass to my build script.
Example of properties:
SET propertyID1=11
SET propertyID2=12
SET propertyID3=13
SET propertyID4=14
SET propertyID5=15
SET propertyIDPref1=011
SET propertyIDPref2=012
SET propertyIDPref3=013
SET propertyIDPref4=014
SET propertyIDPref4=015
SET propertyName1=A
SET propertyName2=B
SET propertyName3=C
SET propertyName4=D
SET propertyName5=E
call :parse "%M%"
pause
goto :eof
:parse
setlocal
set list=%~1
for /F "tokens=1* delims=," %%f in ("%list%") do (
if not "%%f" == "" call :getLineNumber %%f
if not "%%g" == "" call :parse "%%g"
if "%%g" == "" call :printPropertiesConcatenation
)
endlocal
goto :eof
:printPropertiesConcatenation
setLocal
echo "Gr8 " %buildPropertiesList%
endLocal
goto :eof
:getLineNumber
setlocal
echo file name is %1
set propID = 'propertyID'%1%
set propStr=propertyID
set propID=%1
set newvar=!%propStr%%propID%!
echo %newvar%
set propNameStr=propertyName
set propName=!%propNameStr%%propID%!
echo %propName%
set propIDPrefix=propertyIDPref
set propIDPrefixWithPrefix=!%propIDPrefix%%propID%!
echo %propIDPrefixWithPrefix%
set buildPropertiesList=%buildPropertiesList%','!%propIDPrefix%%propID%!
goto :eof
I can read correct values from these properties on iteration and see it using echo in loop.
But I want to concat these values and pass it to build script. But I don't get way to see all concatenated values in one variable.
I want something like this.
set propNames = A,C and set propIds = 11,13 in the end so that I can pass propNames and PropIds.
From above code I want buildPropertiesList to have 011,013
Is there any way
this might work for you:
#ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION
SET propertyID1=11
SET propertyID2=12
SET propertyID3=13
SET propertyID4=14
SET propertyID5=15
SET propertyIDPref1=011
SET propertyIDPref2=012
SET propertyIDPref3=013
SET propertyIDPref4=014
SET propertyIDPref4=015
SET propertyName1=A
SET propertyName2=B
SET propertyName3=C
SET propertyName4=D
SET propertyName5=E
for /f "tokens=1*delims==" %%a in ('set "property"') do (
set "apx=%%a"
set "apx=!apx:*property=!"
for /f "delims=0123456789" %%b in ("!apx!") do set "apx=%%b"
if defined props!apx! (
call set "props!apx!=%%props!apx!%%,%%b"
) else (
set "props!apx!=%%b"
)
)
set "props"
You may solve this problem with less code with the aid of arrays. For example:
#echo off
setlocal EnableDelayedExpansion
rem Create the arrays of properties:
set /A i=1, ID=11, IDPref=1011
for %%a in (A B C D E) do (
SET propertyID[!i!]=!ID!
SET propertyIDPref[!i!]=!IDPref:~-3!
SET propertyName[!i!]=%%a
set /A i+=1, ID+=1, IDPref+=1
)
echo Properties menu:
echo/
for /L %%i in (1,1,5) do echo %%i. !propertyName[%%i]!
echo/
set /P "M=Enter properties list: "
call :parse "%M%"
echo Names: !propNames:~0,-1!
echo Ids: !propIds:~0,-1!
echo List: !propList:~0,-1!
pause
goto :eof
:parse
set "propNames="
set "propIds="
set "propList="
for %%i in (%~1) do (
set "propNames=!propNames!!propertyName[%%i]!,"
set "propIds=!propIds!!propertyID[%%i]!,"
set "propList=!propList!!propertyIDPref[%%i]!,"
)
exit /B