letters scramble improperly with batch script - batch-file

i have this code that basically takes the input that the user types and changes all of the letters and then puts it into a .gjt file
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set /p textfileContents=
#echo %textfileContents% > "temp\temp.txt"
timeout 3
(
FOR /f "usebackqdelims=" %%a IN ("temp\temp.txt") DO (
SET "line=%%a"
SET "line=!line:a=p!"
SET "line=!line:b=f!"
SET "line=!line:c=i!"
SET "line=!line:d=e!"
SET "line=!line:e=o!"
SET "line=!line:f=g!"
SET "line=!line:g=u!"
SET "line=!line:h=m!"
SET "line=!line:i=s!"
SET "line=!line:j=q!"
SET "line=!line:k=w!"
SET "line=!line:l=a!"
SET "line=!line:m=b!"
SET "line=!line:n=z!"
SET "line=!line:o=x!"
SET "line=!line:p=j!"
SET "line=!line:q=c!"
SET "line=!line:r=t!"
SET "line=!line:s=h!"
SET "line=!line:t=r!"
SET "line=!line:u=k!"
SET "line=!line:v=l!"
SET "line=!line:w=n!"
SET "line=!line:x=y!"
SET "line=!line:y=d!"
SET "line=!line:z=v!"
ECHO !line!
)
)>"encrypt output\message.gjt"
del "temp\temp.txt"
pause
and then another batch file that will change all of the letters back and puts it into a .txt file
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
(
FOR /f "usebackqdelims=" %%a IN ("decrypt input\message.gjt") DO (
SET "line=%%a"
SET "line=!line:p=a!"
SET "line=!line:f=b!"
SET "line=!line:i=c!"
SET "line=!line:e=d!"
SET "line=!line:o=e!"
SET "line=!line:g=f!"
SET "line=!line:u=g!"
SET "line=!line:m=h!"
SET "line=!line:s=i!"
SET "line=!line:q=j!"
SET "line=!line:w=k!"
SET "line=!line:a=l!"
SET "line=!line:b=m!"
SET "line=!line:z=n!"
SET "line=!line:x=o!"
SET "line=!line:j=p!"
SET "line=!line:c=q!"
SET "line=!line:t=r!"
SET "line=!line:h=s!"
SET "line=!line:r=t!"
SET "line=!line:k=u!"
SET "line=!line:l=v!"
SET "line=!line:n=w!"
SET "line=!line:y=x!"
SET "line=!line:d=y!"
SET "line=!line:v=z!"
ECHO !line!
)
)>"decrypt output\message.txt"
pause
but for whatever reason it outputs completely incorrectly for example if i type "abcdefghijklmnopqrstuvwxyz" it outputs "pusyyuumsqwzmzypqtstuzwyyz"
tl;dr
I tried to make a user inputted text randomize and then un randomize I expected just this but it outputs completely incorrectly

Related

Why doesn't this Batch Program give an Output?

I recently had an issue with one of my batch programs, it is basically an incompleted platformer: My code is work in progress and i got stuck with my attemp of collision detection. I have a matrix M, that i can access within an EnableDelayedExpansion block like this !M[%%a][%%d]!
The first time it works, but the second usage does not work anymore:
echo A!M[%xx%,%yy%]!B yields AB.
I would appreciate any hint.
Thanks in advance!
#echo off
::creating a new line variable for multi line strings
set NLM=^
:: Two empty lines are required here
::set up initial grid
set height=9
set width=14
for /l %%a in (0,1,9) do (
for /l %%d in (0,1,14) do (
set M[%%a][%%d]=.
)
)
::create some vars at an initial value
set falling=0
set row=5
set turns=0
set x=8
set face=empty
:turn
set M[%row%][%x%]=^P
::set wall position
set x2=4
set y2=6
set M[%y2%][%x2%]=^#
::display current grid
setlocal EnableDelayedExpansion
set "grid="
for /l %%a in (0,1,9) do (
set line=!M[%%a][0]!
for /l %%d in (1,1,14) do (
set line=!line!!M[%%a][%%d]!
)
set grid=!grid! !NLM! !line!
)
cls
echo !grid!
endlocal
::prompt and make move
:: 4 = d
choice /c:wasd /n /m "" /t 1 /d s
set move=0
set /a xx=x
set /a yy=row
:move clearing Player
set M[%row%][%x%]=.
:: ^ this clears the characters trail so that there is no trail left behind after
:: moves, even when blocked. But at the start of grid Player is drawn again.
if %errorlevel%==4 ( if %x% LSS %width% ( set face=right
set /a xx=x+1) else ( set face=empty))
::if %errorlevel%==3 ( if %row% LSS %height% ( set face=down
::set /a move=0) else ( set face=empty))
if %errorlevel%==2 ( if %x% GTR 0 ( set face=left
set /a xx=x-1) else ( set face=empty))
if %errorlevel%==1 ( if %row% GTR 0 ( set face=up
set /a move=1) else ( set face=empty))
::falling!
set /a yy=yy-move
if %move%==0 (
set /a falling+=1
set face=down
) else (
set falling=0
)
set /a yy=yy+falling
::collision tests
setlocal enableDelayedExpansion
::echo !M[%xx%,%yy%]! > tmp.var
echo A!M[%xx%,%yy%]!B
endlocal
::set /P var1="" < tmp.var
::if %var1%==# (
:: set face=empty
::)
::now the actual movements
::if %face%==right set /a x+=1
::if %face%==left set /a x-=1
::if %face%==up set /a row=yy
::if %face%==down set /a row=yy
set /a x=xx
set /a row=yy
::increment turns, return to top
set /a turns+=1
goto :turn

split.bat - how to save list1,list2 and not list1-part1,list-part2?

This is split.bat script for Windows. My question is how do I save list1, list2 and not
list1-part1
list-part2
list-part3
This is the script I found and tried. Can anybody modify it?
#echo off
setLocal EnableDelayedExpansion
set file=%1
set limit=%2
set lineCounter=1
set filenameCounter=1
set name=
set extension=
for %%a in (%file%) do (
set "name=%%~na"
set "extension=%%~xa"
)
for /f "tokens=*" %%a in (%file%) do (
set splitFile=!name!-part!filenameCounter!!extension!
if !lineCounter! gtr !limit! (
set /a filenameCounter=!filenameCounter! + 1
set lineCounter=1
echo Created !splitFile!.
)
echo %%a>> !splitFile!
set /a lineCounter=!lineCounter! + 1
)
Just change this line set splitFile=!name!-part!filenameCounter!!extension!
to this set splitFile=!name!!filenameCounter!!extension!

display Hex in cmd

I have some trouble with batch file
#echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set LOOKUP=0123456789abcdef &set HEXSTR=
set MAC_B1=03
set MAC_B2=5D
set MAC_B3=00
set MAC_B4=00
set MAC_B5=00
set MAC_B6=00
set my_file=runningno.txt
set runningno=128
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set LOOKUP=0123456789abcdef &set /a HEXSTR=
set /a A=%runningno%
:loop
set /a B=!A! %% 16 & set /a A=!A! / 16
set HEXSTR=!LOOKUP:~%B%,1!%HEXSTR%
if %A% GTR 0 goto :loop
if %runningno% LEQ 255 (
set /a MAC_B6 = %HEXSTR%
)
ECHO MAC address:%MAC_B1%-%MAC_B2%-%MAC_B3%-%MAC_B4%-%MAC_B5%-%MAC_B6%
the output is fine if i get runningno in hex without "abcdef"
03-5D-00-00-00-80
in other i.e set runningno=255 the output should be 03-5D-00-00-00-FF but is
03-5D-00-00-00-0
PLEASE HELP
#echo off
setlocal enableextensions enabledelayedexpansion
set "mac_b1=03"
set "mac_b2=5d"
set "mac_b3=00"
set "mac_b4=00"
set "mac_b5=00"
set "mac_b6=00"
set "runningno=128"
set "lookup=0123456789abcdef"
set /a "high=(runningno & 255) >> 4", "low=runningno & 15"
set "mac_b6=!lookup:~%high%,1!!lookup:~%low%,1!"
echo %mac_b1%-%mac_b2%-%mac_b3%-%mac_b4%-%mac_b5%-%mac_b6%
edited As it seems the value in the runningno will increase,
#echo off
setlocal enableextensions enabledelayedexpansion
set "mac_b1=03"
set "mac_b2=5d"
set "mac_b3=00"
set "mac_b4=00"
set "mac_b5=00"
set "mac_b6=00"
set "runningno=1458796"
set "lookup=0123456789abcdef"
set "value=%runningno%"
for /l %%a in (6, -1, 3) do (
set /a "high=(value & 255) >> 4", "low=value & 15", "value>>=8"
for %%H in (!high!) do for %%L in (!low!) do (
set "mac_b%%a=!lookup:~%%H,1!!lookup:~%%L,1!"
)
)
echo %mac_b1%-%mac_b2%-%mac_b3%-%mac_b4%-%mac_b5%-%mac_b6%
BUT this has a problem. Arithmetics in batch files are limited to 32bit signed integers. That means that the higher value that can be handled with this method is 0x7fffff

How do I concatenate the value of a variable into the name of another in a batch file?

So I've been writing a batch file which backs up a certain file while cleaning old entries every so often. I've run into an issue where it would be simpler and more readable to store month lengths (for calculation purpose) (days1,days2,days3,etc.), and reference these by concatenating the word days with a variable which stores the month (1, 2, 3, etc.). Unfortunately, this never seems to reference the right variable correctly. Here's the relevant code, from a section calculating a date 28 days previously:
set days1=31
set days2=28
set days3=31
set days4=30
set days5=31
(etc.)
...
set pastmonthday=%curday%-28
set pastmonthmonth=%curmonth%
set pastmonthyear=%curyear%
if %pastmonthday% lss 0 (
set /a pastmonthmonth=%pastmonthmonth%-1
set /a pastmonthprevmon=1
)
if %pastmonthmonth%==0 (
set /a pastmonthyear-=1
set /a pastmonthmonth=12
)
set monthlengthvar=0
setlocal EnableDelayedExpansion
set tempmonthlengthvar=0
if %pastmonthday% lss 0 (set tempmonthlengthvar = !days%pastmonthmonth%!)
echo.%tempmonthlengthvar%
pause
for /F "delims=" %%A in (!tempmonthlengthvar!) DO (
endlocal
set "monthlengthvar=%%A"
)
set pastmonthday+=%monthlengthvar%
echo.%pastmonthday%
pause
...
The two echoes output 0 and -7, respectively. I can't figure out why this is, no matter how I've reworked it.
some errors (forgotten /a in set, quotes etc.)
#ECHO OFF &SETLOCAL disableDelayedExpansion
SET /a days1=31
set /a days2=28
set /a days3=31
set /a days4=30
set /a days5=31
set /a pastmonthday=22-28
set /a pastmonthmonth=2
set /a pastmonthyear=2014
if %pastmonthday% lss 0 (
set /a pastmonthmonth-=1
set /a pastmonthprevmon=1
)
if %pastmonthmonth% equ 0 (
set /a pastmonthyear-=1
set /a pastmonthmonth=12
)
set /a monthlengthvar=0
setlocal EnableDelayedExpansion
set /a tempmonthlengthvar=0
if %pastmonthday% lss 0 SET /a tempmonthlengthvar=!days%pastmonthmonth%!
ECHO(%tempmonthlengthvar%
for /F "delims=" %%A in ("%tempmonthlengthvar%") DO (
IF "!"=="" endlocal
set /a monthlengthvar=%%A
)
set /a pastmonthday+=monthlengthvar
ECHO(%pastmonthday%

Batch variable inside of variable

I've got a set of variables called p1 to p9
I've got 2 vars to set the range I'm interested in.
I want to list all the vars from minimum range +1 to the max.
It looks like that (and it won't work)
set currentvar=5
set maxvar=9
set p1=aaa
set p2=bbb
set p3=ccc
set p4=ddd
set p5=eee
set p6=fff
set p7=ggg
set p8=hhh
set p9=iii
set /a result = %maxvar% - %currentvar%
echo Found %result% vars in the range.
:LOOP
if %currentvar% LSS %maxvar% (
set /a currentvar=%currentvar% + 1
echo %p %currentvar% % //IT WON'T WORK AND I DON'T KNOW HOW TO MAKE IT WORK...
goto LOOP
) else (
goto END
)
:END
The result I'd like to see:
fff
ggg
hhh
iii
this might work for you:
#ECHO OFF &SETLOCAL
set /a currentvar=5
set /a maxvar=9
set /a RangeStart=currentvar+1
set p1=aaa
set p2=bbb
set p3=ccc
set p4=ddd
set p5=eee
set p6=fff
set p7=ggg
set p8=hhh
set p9=iii
set /a result=maxvar-currentvar
echo Found %result% vars in the range.
for /l %%a in (%RangeStart% 1 %maxvar%) do call echo(%%p%%a%%
You need to Enabledelayedexpansion:
#echo off
Setlocal Enabledelayedexpansion
set currentvar=5
set maxvar=9
set p1=aaa
set p2=bbb
set p3=ccc
set p4=ddd
set p5=eee
set p6=fff
set p7=ggg
set p8=hhh
set p9=iii
set /a result = %maxvar% - %currentvar%
echo Found %result% vars in the range.
:LOOP
if %currentvar% LSS %maxvar% (
set /a currentvar=%currentvar% + 1
echo !p%currentvar%! &REM This is how you make a comment in batch
goto LOOP
) else (
goto END
)
:END
Endlocal
And that should do what you want to do. Also to make a comment use a new line and type :: or REM.
Mona

Resources