Batch translator

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

Batch translator

Post by MigrationUser »

25 Jan 2016 21:19
Batcher

I am working on a translator made in batch and I keep receiving a missing operator error does anyone have any idea why? Just for background information due to the number of translations verb.bat calls a file based on the first letter of user input here is the code for verb.bat.

Code: Select all

@echo off
cls
cd data
echo type hlp for help
:top
set /p verb=
if %verb% == cls cls
if %verb% == hlp goto hlp
If /I "%verb:~0,1%"=="a" call abc
If /I "%verb:~0,1%"=="b" call abc
If /I "%verb:~0,1%"=="c" call abc
If /I "%verb:~0,1%"=="d" call defg
If /I "%verb:~0,1%"=="e" call defg
If /I "%verb:~0,1%"=="f" call defg
If /I "%verb:~0,1%"=="g" call defg
If /I "%verb:~0,1%"=="h" call defg
If /I "%verb:~0,1%"=="i" call hijklm
If /I "%verb:~0,1%"=="j" call hijklm
If /I "%verb:~0,1%"=="k" call hijklm
If /I "%verb:~0,1%"=="l" call hijklm
If /I "%verb:~0,1%"=="m" call hijklm
If /I "%verb:~0,1%"=="n" call nopq
If /I "%verb:~0,1%"=="o" call nopq
If /I "%verb:~0,1%"=="p" call nopq
If /I "%verb:~0,1%"=="q" call nopq
If /I "%verb:~0,1%"=="r" call rs
If /I "%verb:~0,1%"=="s" call rs
If /I "%verb:~0,1%"=="t" call tuvwxyz
If /I "%verb:~0,1%"=="u" call tuvwxyz
If /I "%verb:~0,1%"=="v" call tuvwxyz
If /I "%verb:~0,1%"=="w" call tuvwxyz
If /I "%verb:~0,1%"=="x" call tuvwxyz
If /I "%verb:~0,1%"=="y" call tuvwxyz
If /I "%verb:~0,1%"=="z" call tuvwxyz
set /p num=<verb1.txt
set /a "num=%num%+1" && echo %num%>verb1.txt
goto top
:hlp
echo you have searched for %num% verbs
goto top
and if anyone knows how to simplify the lines below please tell me thanks -Batcher

Code: Select all

If /I "%verb:~0,1%"=="a" call abc
If /I "%verb:~0,1%"=="b" call abc
If /I "%verb:~0,1%"=="c" call abc
If /I "%verb:~0,1%"=="d" call defg
If /I "%verb:~0,1%"=="e" call defg
If /I "%verb:~0,1%"=="f" call defg
If /I "%verb:~0,1%"=="g" call defg
If /I "%verb:~0,1%"=="h" call defg
If /I "%verb:~0,1%"=="i" call hijklm
If /I "%verb:~0,1%"=="j" call hijklm
If /I "%verb:~0,1%"=="k" call hijklm
If /I "%verb:~0,1%"=="l" call hijklm
If /I "%verb:~0,1%"=="m" call hijklm
If /I "%verb:~0,1%"=="n" call nopq
If /I "%verb:~0,1%"=="o" call nopq
If /I "%verb:~0,1%"=="p" call nopq
If /I "%verb:~0,1%"=="q" call nopq
If /I "%verb:~0,1%"=="r" call rs
If /I "%verb:~0,1%"=="s" call rs
If /I "%verb:~0,1%"=="t" call tuvwxyz
If /I "%verb:~0,1%"=="u" call tuvwxyz
If /I "%verb:~0,1%"=="v" call tuvwxyz
If /I "%verb:~0,1%"=="w" call tuvwxyz
If /I "%verb:~0,1%"=="x" call tuvwxyz
If /I "%verb:~0,1%"=="y" call tuvwxyz
If /I "%verb:~0,1%"=="z" call tuvwxyz
----------------------------

#2 25 Jan 2016 21:50
Aacini

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem Define the number of letters per subroutine
set "numbers=3 5 5 4 2 7"
set "letters=0abcdefghijklmnopqrstuvwxyz"

rem Create the array of equivalences, i.e. sub[a]=abc, sub[b]=abc, sub[c]=abc, sub[d]=defg, ...

set /A "pos=1, start=0"
for %%i in (%numbers%) do (
   set /A start+=1, end=start+%%i-1
   for /L %%j in (!start!,1,!end!) do (
      for %%p in (!pos!) do set "sub[!letters:~%%j,1!]=!letters:~%%p,%%i!"
   )
   set /A "pos+=%%i, start=end"
)

SET SUB

Output:

sub[a]=abc
sub[b]=abc
sub[c]=abc
sub[d]=defgh
sub[e]=defgh
sub[f]=defgh
sub[g]=defgh
sub[h]=defgh
sub[i]=ijklm
sub[j]=ijklm
sub[k]=ijklm
sub[l]=ijklm
sub[m]=ijklm
sub[n]=nopq
sub[o]=nopq
sub[p]=nopq
sub[q]=nopq
sub[r]=rs
sub[s]=rs
sub[t]=tuvwxyz
sub[u]=tuvwxyz
sub[v]=tuvwxyz
sub[w]=tuvwxyz
sub[x]=tuvwxyz
sub[y]=tuvwxyz
sub[z]=tuvwxyz
After that, just do:

Code: Select all

call !sub[%verb:~0,1%]!
instead of your 26 If's...

----------------------------

#3 25 Jan 2016 22:06
Shadow Thief

Also, change

Code: Select all

echo %num%>verb1.txt
to

Code: Select all

echo %num% >verb1.txt
(note the space to the left of the > symbol)

Last edited by Shadow Thief (25 Jan 2016 22:06)

----------------------------

#4 26 Jan 2016 21:24
Batcher

Thank you so much Aacini!
Shadow Thief thank you but every time I close verb.bat the # resets to zero do you know how to fix this

Code: Select all

@echo off
cls
cd data
setlocal EnableDelayedExpansion

set "numbers=3 4 6 4 2 7"
set "letters=0abcdefghijklmnopqrstuvwxyz"

rem Create the array of equivalences, i.e. sub[a]=abc, sub[b]=abc, sub[c]=abc, sub[d]=defg, ...

set /A "pos=1, start=0"
for %%i in (%numbers%) do (
   set /A start+=1, end=start+%%i-1
   for /L %%j in (!start!,1,!end!) do (
      for %%p in (!pos!) do set "sub[!letters:~%%j,1!]=!letters:~%%p,%%i!"
   )
   set /A "pos+=%%i, start=end"
)

SET SUB
cls
echo Leave out the word "to" for example for "to sleep" just type sleep
echo type hlp for help
:top
set /p verb=
set ar=regular AR verb O AS A AMOS AIS AN ANDO
set er=regular ER verb O ES E EMOS EIS EN IENDO
set ir=regular IR verb O ES E IMOS ¡S EN IENDO
set oue=stem change O - UE
set l= ----- 
set go=yo - go
set ei=e - i stem change
set iie=i - ie stem change
set "col=color"
if %verb% == cls cls
if %verb% == ir echo to go%l%irregular voy vas va vamos vais van yendo
if %verb% == hlp goto hlp
if %verb% == color goto color
echo %time%>verb.txt
call !sub[%verb:~0,1%]!
set /p num= <verb1.txt
set /a "num=%num%+1" && echo %num% >verb1.txt
goto top

:hlp
echo you have searched for %num% verbs
goto top

:color
echo 0 = Black       8 = Gray
echo 1 = Blue        9 = Light Blue
echo 2 = Green       A = Light Green
echo 3 = Aqua        B = Light Aqua
echo 4 = Red         C = Light Red
echo 5 = Purple      D = Light Purple
echo 6 = Yellow      E = Light Yellow
echo 7 = White       F = Bright White
echo.
echo enter color code (background no space foreground)
echo my favorite is 0a
set /p code=
%col% %code%
goto top
----------------------------

#5 27 Jan 2016 00:48
Shadow Thief

It's not resetting to zero, %num% is never being incremented.

I'm not sure why, but batch is treating that variable like it's inside of a code block. Change the line where you post to verb1.txt to

set /a num=!num!+1 && echo !num! >verb1.txt

and it will work.

----------------------------

#6 29 Jan 2016 21:03
Batcher

Thanks Shadow Thief

Last edited by Batcher (13 Feb 2016 20:53)

----------------------------

#7 13 Feb 2016 20:54
Batcher

I have another question I am working on a way for users to input a new translation ad i'm not sure how this is what I have so far.

Code: Select all

@echo off
cls
set /p a="Enter Spanish "
SET va=%a:~0,1%
SET %end%=%a:~-2%
echo. 
set /p b="Enter English "
SET vb=%b:~0,1%
echo. 
set /p c="Enter stem change and or end note "
@echo off
@echo "if %%verb%% == %a% echo to %b% ----- %end% ----- %c%">> %va%.bat
@echo "if %%verb%% == %b% echo %a% ----- %end% ----- %c%">> %vb%.bat
Last edited by Batcher (17 Feb 2016 23:12)

----------------------------

#8 11 Apr 2016 19:52
Batcher

Does anyone know whats wrong with this

Code: Select all

for /f delims^=^ eol^= %%i in ('!sub[%verb:~0,1%]!.bat') do set lastline=%%i
set "var="
echo %lastline%|findstr /i "\<AR verb\>">nul && (set "var=ar" & goto :found)
echo %lastline%|findstr /i "\<ER verb\>">nul && (set "var=er" & :found)
echo %lastline%|findstr /i "\<IR verb\>">nul && (set "var=ir" & goto :found)
echo %lastline%|findstr /i "\<e - ie\>">nul && (set "var=iei" & :found)
echo %lastline%|findstr /i "\<e - i\>">nul && (set "var=ei" & goto :found)
echo %lastline%|findstr /i "\<O - UE\>">nul && (set "var=oue" & goto :found)
::echo %lastline%|findstr /i "\<yo - go\>">nul && (set "var=yo-go" & goto :found)
:FOUND
echo %var%
%var% is always set to = ar

----------------------------

#9 12 Apr 2016 10:18
bluesxman

At a guess, it's the spaces in your search string. Try using "/r" and "/c", like so:

Code: Select all

findstr /i /r /c:"\<AR verb\>">nul
... for each occurrence.

cmd | *sh | ruby | chef

----------------------------

#10 16 Apr 2016 14:36
Batcher

This only works for finding "AR verb" for all the others nothing is found.

Code: Select all

echo %lastline%|findstr /i /r /c:"\<AR verb\>">nul && (set "var=ar" & goto :found)
Last edited by Batcher (22 Apr 2016 21:35)

----------------------------

#12 15 May 2016 19:59
Batcher

I am working on the following code but the final output is "dueo" when it shouhld be "duermo" does any one know what I'm doing wrong?

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set v=dormir
Set "char=%v:~-2%"
for /F "tokens=1* delims=%char%" %%a in ("%v%") do (
   Set "beforey=%%a"
)
set char=%unchanged%
for /F "tokens=1* delims=%char%" %%a in ("%beforey%") do (
   Set "afterx=%%b"
)
SET var3=%v:~-2%
set variable=!after:~0,-2!
if %var3% ==ar goto ar
if %var3% ==er goto er
if %var3% ==ir goto ir
if %change% ==ue set unchanged=o
:ar
set p1=o
set p2=as
set p3=a
set p4=amos
set p5=ais
set p6=an
goto type
:er
set p1=o
set p2=es
set p3=e
set p4=emos
set p5=eis
set p6=en
goto type
:ir
set p1=o
set p2=es
set p3=e
set p4=imos
set p5=is
set p6=en
:type
Set "char=o"
for /F "tokens=1* delims=%char%" %%a in ("%v%") do (
   Set "before=%%a"
   Set "after=%%b"
)
echo %before%%change%%afterx%%p1%
pause
----------------------------

#13 15 May 2016 23:18
Shadow Thief

That code only outputs "do"

----------------------------

#14 16 May 2016 23:36
Batcher

Do you know why?

----------------------------

#15 17 May 2016 01:06
Shadow Thief

I've commented your code to show exactly how it runs and what variables get set.

Code: Select all

@echo off
setlocal EnableDelayedExpansion

:: Alright, boys and girls, today's secret word is "dormir"
set v=dormir

:: The variable %char% is set to the last two letters, which are "ir"
Set "char=%v:~-2%"

:: Take "dormir" and split it into two pieces, using "i" and "r" as delimiters
:: This results in the first token being "do" and the second token being "m"
:: We don't care about the second token, but the variable %beforey% is set to "do"
for /F "tokens=1* delims=%char%" %%a in ("%v%") do (
   Set "beforey=%%a"
)

:: Set the variable %char% to whatever the value of %unchanged% is
:: However, since %unchanged% is empty, this simply unsets any current value of %char%
set char=%unchanged%

:: Take the value of %beforey% (right now it's "do") and split it on nothing,
:: since %char% is empty; this results in the entire string being stored in %%a
:: Take the second token (which is empty) and store it in %afterx%
for /F "tokens=1* delims=%char%" %%a in ("%beforey%") do (
   Set "afterx=%%b"
)

:: set %var3% to the last two letters of %v%, which are "ir"
SET var3=%v:~-2%

:: Set the value of %variable% to the entire value of !after! except for the last two characters
:: Since !after! does not exist, %variable% is set to nothing
:: Appropriately, %variable% is never mentioned after this line anyway
set variable=!after:~0,-2!

:: If the value of %var3%<space> is equal to ar then go to :ar
if %var3% ==ar goto ar

:: It is not, so check if the value of %var3%<space> is equal to er 
:: If it is, go to :er
if %var3% ==er goto er

:: Again, it is not, so check if the value of %var3%<space> is equal to ir
:: If it is, go to :ir
:: This check succeeds through pure dumb luck since there is technically a space
::  between "==ir" and "goto"
:: A better check would be `if "%var3%"=="ir" goto ir` since that way spaces and
:: other special characters are preserved and you don't have to worry about spacing
if %var3% ==ir goto ir

:: This line never gets run since we went to :ir in the previous line
:: Since this is the first place %change% appears, it remains empty
if %change% ==ue set unchanged=o

:: This is just a bunch of set statements that do exactly what you expect them to
:ar
set p1=o
set p2=as
set p3=a
set p4=amos
set p5=ais
set p6=an
goto type

:: This is also a bunch of set statements that behave normally
:er
set p1=o
set p2=es
set p3=e
set p4=emos
set p5=eis
set p6=en
goto type

:: More set statements
:: Also, good job not ending with "goto type" right before ":type" like I've
::  seen many people do
:ir
set p1=o
set p2=es
set p3=e
set p4=imos
set p5=is
set p6=en

:type
:: Set %char% to "o"
Set "char=o"

:: Split "dormir" into two tokens using "o" as a delimiter
:: Set %before% to the first token ("d")
:: Set %after% to the second token ("rmir")
for /F "tokens=1* delims=%char%" %%a in ("%v%") do (
   Set "before=%%a"
   Set "after=%%b"
)

:: Print the value of %before% ("d")
:: Followed by the value of %change% (nothing)
:: Followed by the value of %afterx% (nothing)
:: Followed by the value of %p1% ("o")
:: This results in the string "do" being displayed
echo %before%%change%%afterx%%p1%
pause
----------------------------

#16 22 May 2016 18:08

foxidrive
Batcher wrote:

Do you know why?
Showing gratitude and replying to posts doesn't cost you anything. &nbsp; You might even start an epidemic of kindness and consideration...

----------------------------

#17 23 May 2016 21:44
Batcher
foxidrive wrote:



Showing gratitude and replying to posts doesn't cost you anything. &nbsp; <span style="color: #bfbfbf">You might even start an epidemic of kindness and consideration...</span>
Fox drive I was going to say tank you when I had a fixed code to post as a final product but I didn't get a chance to fix the code. However I guess I'll say thank you now.

Thank you Shadow Thief I appreciate you writing what all of the lines do.

----------------------------

#18 26 Jul 2016 00:52
Batcher

I SOLVED THIS WITHIN EXCEL I no longer need a solution.

Hi everyone I have a new question about the following code

Code: Select all

@echo off
setLocal EnableDelayedExpansion
set "var=if %%v%% == "
for /f "tokens=* delims= " %%a in (input.txt) do (
set /a N+=1
echo %var% %%a echo %INPUT.txt% >>output.txt
echo %var% %%a echo %INPUT1.txt% >>output.txt
)

for example
INPUT

1
2
3
4
5
6
7

INPUT1

a
b
c
d
e
f
g

Output

%var% 1 echo a
%var% a echo 1
%var% 2 echo b
%var% b echo 2
%var% 3 echo c
%var% c echo 3
%var% 4 echo d
%var% d echo 4
...

so how can I get the word on the same line to export to output.txt in that format?
Thanks ~Batcher

Last edited by Batcher (28 Jul 2016 00:01)
Post Reply