Solved Batch: Help with translating with a map

June 13, 2015 at 07:46:50
Specs: Windows 7, Intel Core i7 4790K @ 4.0 GHz / 16 GB RAM
Hello. I was training a bit with my knowledge of batch, but I can't figure out what is wrong with this, as I don't have the necessary knowledge.
Also about "enabledelayedexpansion", I can't find any info that actually tells exactly what it does, so you are welcome to point to such a site.

The code I have is designed to (as a function)translate an input with a map.
Here is the code:

@echo off
setlocal enabledelayedexpansion
set list=Mon,Man;Tue,Tir;Wed,Ons;Thu,Tor;Fri,Fre;Sat,Lor;Sun,Son
set /p input=Translate:
call :f map %input%

for /F %%a "delims=;" in (%~1) DO (
echo %%a | finstr /r %~2 | set /p f_temp=
if errorlevel 0 set f_temp=!f_temp:%~2,=!&echo %f_temp%
goto :eof


I know there are more simple ways to do this, but again; training with current knowledge.

Thanks in advance.

See More: Batch: Help with translating with a map

Report •

June 13, 2015 at 17:22:54
To answer one of your questions:
Enabledelayedexpansion enables delayed variable expansion, which is expanding them (inserting the contents) right before they're needed, rather than right before the command using the variable. Variables using delayed expansions uses !'s instead of %'s (!delayed! vs. %normal%). Some commands gets more options when delayed variable expansions are enabled (see -command- /? in cmd.exe)

I think I've fixed your FOR loop, however I cannot help you any further without knowing what you want to accomplish (like what %f_temp% should contain after processing a given input, and also what the purpose of list is.) The delimiter ("delims=;") Does nothing unless the %input% string contains any semicolons. I don't know if this is on purpose or an error.

for /F "delims=;" %%A in ("%1") DO (
echo %%A | findstr /r "%2" | set /p f_temp=
if !errorlevel!==0 (
	set f_temp=!f_temp:%2=!
	echo !f_temp!

Here, %1 would equal "map", and %2 is the %input% string.
What you are doing here is looking for %input% in %%A (which would be "map" as "map" doesn't have any semicolons).

set f_temp=!f_temp:%2=!
removes %input% from f_temp, as of now you won't accomplish anything by doing that, as it will always be empty.

99 little bugs in the code,
99 little bugs.
Take one down, patch it around,
129 little bugs in the code.

message edited by RainBawZ

Report •

June 13, 2015 at 20:53:34
✔ Best Answer
Here's the best I could approximate your approach without jumping through hoops:

@echo off & setlocal
set trans=
set map="one,1","two,2","three,3"
set /p x=input:
for %%a in (%map%) do (
call :f %%a
if defined trans goto :display
goto :eof

for /f "tokens=1,2 delims=," %%b in (%1) do (
if /i "%%b" neq "%x%" goto :eof
set trans=%%c
echo %%c
goto :eof

echo ***translated: "%trans%"
::===== end batch
As you mentioned, there are a number of solutions. This is a simple association table: if input is A, output B, if input is X, output Y, etc.

ps: set /? covers some of "enabledelayedexpansion"

Report •

June 22, 2015 at 12:35:07
Well. I see your point. map and list were supposed to be the same variable name...

The function is supposed to take an input(arg2), and translate with the given map(arg1).
I see the issues, but I don't have time to try anything right now...

Report •

Related Solutions

Ask Question