Tricky Batch File Code?

April 25, 2009 at 16:41:19
Specs: Windows Vista
Hi, I would like to take what the user has set and take it character by character and convert it into somthing else.
The user typed in "Hi World"(so before hand this was set; set /p um=Hi World) , I would like the batch file to take the first letter of %um%, which would be "H" and convert it into a already set variable (lets say H=9375) and then take the second letter and "i" and convert it into 4759 (i=4759), and so on... Also to convert it back, how would i do this? Say I compiled all of them together and the end result of "Hi" was "93754759", how would I get it back to "Hi"? Thanks a-million!

Thoughts: Maybe filename juggling, or file content juggling?

So here's the exmple above (it's missing so much it currently doesn't make much sence):

set H=9375
set i=4759
set %space%=4954
(and set every letter.. so on..)
set /p um= "Hi World" (user input in "")
...unknow code
type outputfile.txt (I'm assuming that the above code that I don't know will direct the character's set value piece by piece here(some output for temp file). But if not, forget it is here)

Thanks abunch in advance!!!

See More: Tricky Batch File Code?

Report •

April 25, 2009 at 18:01:54
here's a vbscript
Set objFS = CreateObject("Scripting.FileSystemObject")
Set dict = CreateObject("Scripting.Dictionary")
dict.Add "H" , "9375"
dict.Add "i" , "4954"
WScript.Echo "Enter word: "
Do While Not WScript.StdIn.AtEndOfLine
   strInput = strInput & WScript.StdIn.Read(1)

Do Until Len(strInput) = 0
	s = Mid(strInput,1,1)
	For i=0 To dict.Count-1 
	 If keys(i) = s Then
	 	WScript.Echo keys(i), items(i)
	 End If 
	strInput = Mid(strInput,j,Len(strInput))

using the same concept, you can get back characters if you do substring on every 4 characters.

Unix Win32 tools | Gawk for Windows

Report •

April 25, 2009 at 19:10:42
Any idea how to do this in a .BAT file?


Since this way is unknown, how about sending the user's text to a seperate file let's call it input.txt

echo %um%  >>input.txt

Does anyone know how to do what I wanted to do in the bat file it's self, in another file (input.txt) and then the .bat file will take character by character and send it to a temp file until the input.txt file is finished and then set the temp file's content as a variable? I know what I need to do, I just lack the coding skills. :\

Report •

April 25, 2009 at 21:05:00
Converting the letters to numbers is easy

for %%a in ("a=654" "b=54687" "c=564" "d=545") do call set var=%%var:%%~a%%

Converting back to letters is more difficult, you must ensure that your numbers don't overlap, for example in the above the number 545 is ambiguous, it can be either part of an "a" and "b" (i.e. ab=65454687) or it could be a "d". After this is figured out the above can be modified to do the opposite.

Report •

Related Solutions

April 25, 2009 at 21:43:49
Ok, so the only part about .BAT that I don't understand completly is the [for] commands. What would the output variable be (or they) for that line, and how would I set the input? Basically how would I apply it to this:

set /p msg=

Also, say I did have the overlap situation figured out. Would I just be flipping the ("a=654" "b=54687" "c=564" "d=545"...) to ("654=a" "54687=b" "564=c" "545=d"). And again. How would I apply it if the input were from a text file (textfile.txt). But this time we'll just echo the result with %rslt%.

Thanks sooo much! Your awsome. :-)

Report •

April 25, 2009 at 21:56:32
The script above was converting a variable named var, which was it's only needed input as all of the definitions were static.

As for reading your text file, it really depends on what the contents are and how they are layed out.

A for loop generally works over a finite set and does the same thing on each of the elements, though they can vary. Start up a command line and type in "for /?" to see the specifics of batch for loops, while your at it check out "set /?"

Report •

April 25, 2009 at 22:42:16
But where's the output? What would the variable be for the output?
So I could do this:

echo %output% >>textfile.txt

As for the contents of the Textfile, it would only be the output of the for loop (%output%).

Report •

April 25, 2009 at 23:10:22
The output is the same as it's input, the variable(what ever the variable name used is), but the variable should be changed so that the letters are numbers(of course you will have to extent the definitions to go up to z).

You most certainly could echo the variable into the text file, so long as it is the same variable that was converted.

Set /p output=Enter words to be converted to numbers
for %%a in ("a=654" "b=54687" "c=564" "d=545") do call set output=%%output:%%~a%%
>>textfile echo %output%

Report •

April 26, 2009 at 00:12:08
Ok, thanks dude. But, when I got to test it out it doesn't work properly. Here's a interactive test BAT, I'm almost positive the manifest contains values which don't overlap (or that's what statistics tell me). Although, when it write the file it mess some of them up, like "C/c", it adds a few number.

@echo off
set /p qwer=
for %%a in ("A=1e" "B=2e3" "C=4e51" "D=5e123" "E=1e2345" "F=1d" "G=2d3" "H=4d51" "I=5d123" "J=1d2345" "K=1d" "L=2d3" "M=4d51" "N=5d123" "O=1d2345" "P=1c" "Q=2c3" "R=4c51" "S=5c123" "T=1c2345" "U=1b" "V=2b3" "Q=4b51" "X=5b123" "Y=1b2345" "Z=1a" " =2a3") do call set qwer=%%qwer:%%~a%%
echo %qwer%  >>%userprofile%\desktop\text.txt
SetLocal EnableDelayedExpansion
set qwert=
for /F "delims=" %%i in (%userprofile%\desktop\text.txt) do set qwert=!qwert! %%i
set qwer=%qwert%
for %%a in ("1e=A" "2e3=B" "4e51=C" "5e123=D" "1e2345=E" "1d=F" "2d3=G" "4d51=H" "5d123=I" "1d2345=J" "1d=K" "2d3=L" "4d51=M" "5d123=N" "1d2345=O" "1c=P" "2c3=Q" "4c51=R" "5c123=S" "1c2345=T" "1b=U" "2b3=V" "4b51=W" "5b123=X" "1b2345=Y" "1a=Z" "2a3= ") do call set qwer=%%qwer:%%~a%%
echo %qwer%

Report •

April 26, 2009 at 01:26:29
You didn't say anything about letters....

The problem is this, the for loop is going through a list 1 by 1.
And using set %var:occurrence=replacement% to change the values,
this procedure is not case sensitive so the overlap is caused by
letters that have been replaced are themselves being replaced.

Since these seem to be hex values you could just give each letter an
alias and replace them afterwards. Alternatively you could change the
whole way the procedure is done.

Your system does overlap, how do you distinguish between 1d and 1d2345?
Bringing the larger strings to the top of the list solves most of
the problems, but I didn't go over it thoroughly enough to rule out
problems. There are also a couple of errors in your script apparently
f and k are both 1d and w doesn't exist in the first conversion.

I had a quick go at fixing it up and had closer sucess, but since it's your baby.....
I used extended ascii characters(dos bar symbols to be exact) as aliases
to avoid conflicts.

@echo off
SetLocal EnableDelayedExpansion
set /p qwer=
for %%a in ("A=1º" "B=2º3" "C=4º51" "D=5º123" "E=1º2345" "F=1¼" "G=2¼3" "H=4¼51" "I=5¼123" "J=1¼2345" "K=1¼" "L=2¼3" "M=4¼51" "N=5¼123" "O=1¼2345" "P=1»" "Q=2»3" "R=4»51" "S=5»123" "T=1»2345" "U=1È" "V=2È3" "W=4È51" "X=5È123" "Y=1È2345" "Z=1É" " =2É3") do set qwer=!qwer:%%~a!
for %%a in ("É=a" "È=b" "»=c" "¼=d" "º=e" "Î=f") do set qwer=!qwer:%%~a!
echo %qwer%  >>"%userprofile%\desktop\text.txt"

set qwert=
for /F "usebackq delims=" %%i in ("%userprofile%\desktop\text.txt") do set qwert=!qwert! %%i
set qwer=%qwert%
for %%a in ("5d123=N" "1d2345=O" "1b2345=Y" "5d123=I" "1d2345=J" "1e2345=E" "1c2345=T" "5e123=D" "5c123=S" "5b123=X" "4b51=W" "4c51=R" "4d51=H" "4e51=C" "4d51=M" "2e3=B"  "2d3=G" "1d=K" "2d3=L" "1c=P" "2c3=Q" "1b=U" "2b3=V" "1a=Z" "2a3= " "1e=A" "1d=F") do call set qwer=%%qwer:%%~a%%
echo %qwer%

Report •

April 26, 2009 at 10:01:59
Ok thanks man, sorry to have to have done that to you, it was late lastnight when I compiled that list. So I can't use letters that are being converted (like: Y=6y443) because the the letters in the value will be switched with the remaining numbers. So I could use symbols like these?:

̆ ̊¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶¸¹º»¼½¾¿▒

And I can use characters from other languages, correct?:

Завдяки Вашим чудовим людиною

Report •

April 26, 2009 at 13:55:47
You will be able to use some extended ansi but most characters for other languages fall into a different category. Ascii/Ansi uses a single byte to represent a character 00 - ff, where something like unicode uses two bytes, 00 00 - ff ff.

Report •

April 26, 2009 at 14:19:24
I just also want to point out that it is probably not very good practice to use extended Ansi in batch file. Some characters will work and if memory serves me correctly some may not. It's trial and error, it works but it can get confusing quickly if it's used too much.

Report •

April 26, 2009 at 20:47:21
Thanks man so much, it really helped. My end result is perfect and is a bullseye on what I wanted to achieve.

Have a good one Judago,

Report •

Ask Question