Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am working on a encryption method that changes all letters to a string of numbers an Example Follows:

set a=a
set b=b
set c=c
set aa=543241254
set bb=785214185
set cc=842501445

My issue is How can I scan a file entered by the user and change all letters to the numbers. (The File being changed text is like "hihowareyou" No spaces or capitals) What I tried was to search for an 'a' in said file and replace it with the numbers but my section did not work.

(for /f "delims=" %%i in ('findstr /n "^" "%textfile%"') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:%b%=%bb%!"
    echo(!line!
    endlocal
))>"%newfile%"
type "%newfile%" >>%newfile%

I am not sure what to do to scan for the letter in the text file and replace it with %a%,%b%,%c% etc.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
414 views
Welcome To Ask or Share your Answers For Others

1 Answer

You should realize at first that these commands:

set a=a
set b=b
set c=c

... are useless "by definition" (that is, with no further explanation), although it should be evident that in this case writting %a% will always be the same than writting just a (the same happen for the rest of letters).

If you want to change one letter by a string of numbers, then the right way should be this:

set a=543241254
set b=785214185
set c=842501445

Then, in order to change the 'a' letter by the contents of a variable, you may do something like this inside your for command:

set "line=!line:a=%a%!"

Again: this change the a letter by the contents of a variable.


Now, you should realize that you need to perform the previous replacement not on the single a letter/variable, but on a series of variables represented each one by one letter. The standard way to process a series of variables in the same way (that really means "using the same code") is via a for command that vary the value of a subscript in combination with an array. For example, if we choose "letter" for the name of the array and define the desired replacement values in different elements of the array, surely we do something like this:

set letter[a]=543241254
set letter[b]=785214185
set letter[c]=842501445

After this, we just need to know how these variables/values may be manipulated. Full details on this point are given at Arrays, linked lists and other data structures in cmd.exe (batch) script, so I will not repeat they here again. However, the array management required to solve this problem is simpler than the full explanation given at that link.

If you execute this command: set letter, all the variables that start with "letter" will be shown, including their values; for example:

letter[a]=543241254
letter[b]=785214185
letter[c]=842501445

If we use a for /F "tokens=2,3 delims=[]=" %%a in ('set letter') do ... command, then the %%a replaceable parameter will contain each one of the subscripts of the array, that is, the letters a, b, c, etc., and the %%b parameter will contain the corresponding numeric values.

This way, in order to complete your program and replace all defined letters, you just need to place your set "line=!line:%b%=%bb%!" line inside previous for command and correctly change the %b% part, that represent the letter, by %%a; and replace the %bb% part, that represent the numeric value, by %%b:

for /F "tokens=2,3 delims=[]=" %%a in ('set letter') do (
   set "line=!line:%%a=%%b!"
)

Of course, you must also define the letter array at beginning of the program...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...