Help with batch error

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ryuh
Posts: 4
Joined: 19 Oct 2014 13:09

Help with batch error

#1 Post by ryuh » 19 Oct 2014 13:15

Hello,
I am trying to create a batch file that encrypts whatever text is typed in it in a way that it replaces every "a" with a "z" and every "b" with a "y" and so on.
But when i execute it, and type any letter from "a" to "m", it does not get encrypted, however from "n" to "z" it DOES turn into its complementary letter.
So here is my script:
------------------------------------------------------------------------------------------------------------------------------------

Code: Select all

@echo off
setlocal enableDelayedExpansion
:a
set /p text=Normal:
set chars=0abcdefghijklmnopqrstuvwxyz


set "text=!text:a=z!"
set "text=!text:b=y!"
set "text=!text:c=x!"
set "text=!text:d=w!"
set "text=!text:e=v!"
set "text=!text:f=u!"
set "text=!text:g=t!"
set "text=!text:h=s!"
set "text=!text:i=r!"
set "text=!text:j=q!"
set "text=!text:k=p!"
set "text=!text:l=o!"
set "text=!text:m=n!"
set "text=!text:n=m!"
set "text=!text:o=l!"
set "text=!text:p=k!"
set "text=!text:q=j!"
set "text=!text:r=i!"
set "text=!text:s=h!"
set "text=!text:t=g!"
set "text=!text:u=f!"
set "text=!text:v=e!"
set "text=!text:w=d!"
set "text=!text:x=c!
set "text=!text:y=b!"
set "text=!text:z=a!"


)
)
echo %text%
pause
goto a

------------------------------------------------------------------------------------------------------------------------------------
I hope someone has a fix.. Thanks

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help with batch error

#2 Post by foxidrive » 19 Oct 2014 17:20

When you execute your batch file this first line changes a to z. Then when the last line runs it changes z back to a.

set "text=!text:a=z!"
set "text=!text:z=a!"

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Help with batch error

#3 Post by Squashman » 19 Oct 2014 18:01

Going to assume you only showed us part of your code as there is no need for using Delayed Expansion with the code you showed us.

Also, if you search the forums you will see this type of encryption has already been done on the forums. It is called ROT13.

There is several encryption topics that have been started on this forum. You got a couple of hours of reading to catch up on. Just search the forums or use this google search.
http://lmgtfy.com/?q=encryption+site%3Adostips.com

ryuh
Posts: 4
Joined: 19 Oct 2014 13:09

Re: Help with batch error

#4 Post by ryuh » 20 Oct 2014 03:18

foxidrive wrote:When you execute your batch file this first line changes a to z. Then when the last line runs it changes z back to a.

set "text=!text:a=z!"
set "text=!text:z=a!"

That makes sense.. but how would it be fixed? Should I add another variable to separate the 2 groups of letters? a-m, n-z?

ryuh
Posts: 4
Joined: 19 Oct 2014 13:09

Re: Help with batch error

#5 Post by ryuh » 20 Oct 2014 03:21

Squashman wrote:Going to assume you only showed us part of your code as there is no need for using Delayed Expansion with the code you showed us.

Also, if you search the forums you will see this type of encryption has already been done on the forums. It is called ROT13.

There is several encryption topics that have been started on this forum. You got a couple of hours of reading to catch up on. Just search the forums or use this google search.
http://lmgtfy.com/?q=encryption+site%3Adostips.com

I actually showed you the whole thing but I was editing another script and forgot to remove the Delayed Expansion. But it wouldn't hurt to keep it anyway. And thanks, I'll start reading now :p

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help with batch error

#6 Post by foxidrive » 20 Oct 2014 04:55

ryuh wrote:but how would it be fixed?


It depends on what your needs are.

The free GnuSED for Windows has a transliteration switch

Code: Select all

@echo off
sed "y/anbocpdqerfsgthuivjwkxlymz/naobpcqdresftguhviwjxkylzm/" file.txt
pause

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Help with batch error

#7 Post by Squashman » 20 Oct 2014 07:41

ryuh wrote:But it wouldn't hurt to keep it anyway.

That depends on what you are doing.
Not sure if there is a speed difference either. I would think there is but I haven't tested it.

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Help with batch error

#8 Post by Aacini » 20 Oct 2014 12:31

ryuh wrote:
foxidrive wrote:When you execute your batch file this first line changes a to z. Then when the last line runs it changes z back to a.

set "text=!text:a=z!"
set "text=!text:z=a!"

That makes sense.. but how would it be fixed? Should I add another variable to separate the 2 groups of letters? a-m, n-z?

You must differentiate the "z" letter used to replace the "a" from any other "z" letter that may appear in the text, that is, you must use a different character for the first replacement and, after replaced the original "z", replace again the different character to "z". The example below use the digits 0..9 and #$/ characters to replace the letters a..m, and replace such characters back to letters n..z at end:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set text=abcdefghijklmnopqrstuvwxyz

set "text=!text:a=0!"
set "text=!text:b=1!"
set "text=!text:c=2!"
set "text=!text:d=3!"
set "text=!text:e=4!"
set "text=!text:f=5!"
set "text=!text:g=6!"
set "text=!text:h=7!"
set "text=!text:i=8!"
set "text=!text:j=9!"
set "text=!text:k=#!"
set "text=!text:l=$!"
set "text=!text:m=/!"

set "text=!text:n=m!"
set "text=!text:o=l!"
set "text=!text:p=k!"
set "text=!text:q=j!"
set "text=!text:r=i!"
set "text=!text:s=h!"
set "text=!text:t=g!"
set "text=!text:u=f!"
set "text=!text:v=e!"
set "text=!text:w=d!"
set "text=!text:x=c!
set "text=!text:y=b!"
set "text=!text:z=a!"

set "text=!text:0=z!"
set "text=!text:1=y!"
set "text=!text:2=x!"
set "text=!text:3=w!"
set "text=!text:4=v!"
set "text=!text:5=u!"
set "text=!text:6=t!"
set "text=!text:7=s!"
set "text=!text:8=r!"
set "text=!text:9=q!"
set "text=!text:#=p!"
set "text=!text:$=o!"
set "text=!text:/=n!"

echo %text%


Of course, this method prevents that the original text may include anyone of the characters used in the replacement...

If you want that the text include any character, then the conversion must be performed character by character so converted characters will not be processed again.

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem Create the replacement array
set letter=abcdefghijklmnopqrstuvwxyz
for /L %%i in (0,1,25) do (
   set /A j=25-%%i
   for %%j in (!j!) do set "replace["!letter:~%%i,1!"]=!letter:~%%j,1!"
)

set "text=0123456789 %letter% #$&/()[]"
echo Original: !text!

rem Separate the text in individual characters, and convert each one
set "result="
for /F "delims=" %%a in ('cmd /U /C set /P "=!text!" ^< NUL ^| find /V ""') do (
   if defined replace["%%a"] (
      set "result=!result!!replace["%%a"]!"
   ) else (
      set "result=!result!%%a!
   )
)

echo Replaced: !result!


Previous Batch file use a trick (documented elsewhere) that execute "cmd /U" to create Unicode characters, that is, each character will be separated from the next one with a zero byte, so find command can take each character individually. The conversion is done via an array in a direct way.

Antonio

ryuh
Posts: 4
Joined: 19 Oct 2014 13:09

Re: Help with batch error

#9 Post by ryuh » 21 Oct 2014 05:29

Aacini wrote:You must differentiate the "z" letter used to replace the "a" from any other "z" letter that may appear in the text, that is, you must use a different character for the first replacement and, after replaced the original "z", replace again the different character to "z". The example below use the digits 0..9 and #$/ characters to replace the letters a..m, and replace such characters back to letters n..z at end:


Antonio

[Solved.]
Thanks! I didn't think of that, I tried using this line in the middle of the 2 parts :
if %text% == a goto end
so if the input was an "a" it would skip the part which says "z=a" and go to the end where the text will be echoed, but that didnt work either.
I also tried making the other half of the letters go to another bat and then use the command "call" in the original bat, but all that did was give me 2 words one of them is one half and the other is another half which meant i had to figure out which part is coded and which isn't and then put them together in one coded word.
You code worked just fine and it's a pretty smart solution so thanks a lot man (:
Also could you recommend somewhere for me to learn more bat programming? I learnt the basics and am trying to learn more but I need it to be consistent so I don't learn one code from one website and another from a different website then my ideas wouldn't link properly..
Thanks again.

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Help with batch error

#10 Post by ShadowThief » 21 Oct 2014 06:15

http://www.robvanderwoude.com/batchfiles.php is pretty good for a batch intro, although it can be a bit dated in places since it's geared mainly towards pre-NT systems.

http://ss64.com/NT has the most comprehensive list of batch commands with useful examples that I've found anywhere online.

Post Reply