0

I have a Batch Script where I am trying to encode a string. I have successfully encoded the string with the help of Xor a string in a bat file

But now I want to store the value %show% !val! in a variable so that it can be used any where in the script. Below is my code. Any suggestion would be greatly appreciated.

    @echo off
setlocal enableDelayedExpansion
call :init

:: Curly braces are used to denote text that should be encrypted.
:: Encryption can span multiple lines
:: {
:::password
:: }

:: Here I use a FOR loop to show all encrypted lines within this script
:: that begin with :::

for /f "delims=: tokens=*" %%A in ('findstr /b ":::" "%~f0"') do (

    set val=%%A
    set pwd = %show% !val!

    )
echo %pwd%

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
Kaushik
  • 111
  • 10
  • Possible duplicate of [Windows batch files: How to set a variable with the result of a command?](https://stackoverflow.com/questions/889518/windows-batch-files-how-to-set-a-variable-with-the-result-of-a-command) – Dominique Jun 29 '18 at 12:44
  • You are using `set pwd = %show% !val!` incorrecly you should use `set "pwd=%show% %%A"` and avoid the intermediate variable val. Aside from that I'm missing a question with a [mcve] –  Jun 29 '18 at 21:03
  • This didn't solve the purpose. It's saying The filename, directory name, or volume label syntax is incorrect. ECHO is off. – Kaushik Jul 02 '18 at 09:27

1 Answers1

0

One method for accomplishing something like return values (CMD doesn't actually having them) is to pass a variable name to the function and have it assign a value to the name (outside a setlocal).

CALL :MYFUNC RETVAL 5 7 11
@ECHO Return Value: %RETVAL%

EXIT /B


:MYFUNC
SETLOCAL
SET DESTVAR=%~1
REM  Do some other stuff.
SET /A TEMPVAR=%2 * %3 * %4
REM  Now for the magic...
EXIT /B && SET "%DESTVAR%=%TEMPVAR%"

The crucial line is the combination of EXIT /B (or you could use ENDLOCAL, then EXIT /B) and an assignment where the substitution happens before it's executed (not delayed expansion).

mojo
  • 4,050
  • 17
  • 24
  • I am bit naive to this Batch Scripting. @mojo Would be great if you could you show me the final implementation? Thanks in advance.!! – Kaushik Jul 02 '18 at 09:30
  • `:show` is not simplistic. My suggestion is to accept another parameter (at the beginning of the parameter list), `%1`, and use it like I illustrated. You'll have to change the existing `set "str=%~1"` to `set "str=%~2"` to allow for the new parameter. Your script doesn't exactly work well for me, so I can't offer to fix it. – mojo Jul 02 '18 at 17:13