Determine Yes/No/All string for current locale

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Determine Yes/No/All string for current locale

#16 Post by dbenham » 29 Oct 2020 17:07

I like it. You might want to include space in the delimiter list if you want the entire word without trailing space.

siberia-man
Posts: 208
Joined: 26 Dec 2013 09:28
Contact:

Re: Determine Yes/No/All string for current locale

#17 Post by siberia-man » 30 Oct 2020 08:49

dbenham wrote:
29 Oct 2020 17:07
You might want to include space in the delimiter list if you want the entire word without trailing space.
Trailing spaces are already trimmed in the separate section of the script. I didn't include the space in the delimiter list because it would be too hard to parse the string. Even more, I am not sure that the existing [(/)] delimiters are 100% reliable. What if they are applicable in the user name (except /)?

isidroco
Posts: 6
Joined: 21 Oct 2020 08:54

Re: Determine Yes/No/All string for current locale

#18 Post by isidroco » 20 Dec 2020 14:45

Thanks Siberia-Man, your code doesn't work if TEMP folder includes (). Here's a slighly simplified version, based also on jfl, skips an "if", doesn't need delayed expansion and allows easy testing. Won't work if TEMP folder has # on it (one could choose another unused valid character on dummy "yesnoall#.tmp" file). I think this could be final solution for winXP or later, error on earliear windows/DOS:

Code: Select all

@echo off
  REM Get local language Yes/No/All letters {to answer prompts}
  REM set errorlevel 1, to check next command
<nul find ""
  REM enable extensions to use FOR /f
setlocal enableextensions
IF ERRORLEVEL 1 echo OLD OS: Can't enableExtensions
if ERRORLEVEL 1 goto :FIN

set PromptLine=
  rem create dummy file [in w9x]: echo x >"%TEMP%\yesnoall#.tmp" >nul
copy /y nul "%TEMP%\yesnoall#.tmp" >nul
  rem capture overwrite prompt after "#" ie: ".tmp? [Sí/No/Todos]:"
for /f "tokens=2* delims=#" %%A in ( '^<nul copy /-y nul "%TEMP%\yesnoall#.tmp"'
  ) do set PromptLine=%%A
  rem remove dummy file
del "%TEMP%\yesnoall#.tmp"
  rem For testing complicated case [russian], uncomment next line
rem set PromptLine=.tmp [Yes (a?)/No (b??)/All (c??)]:
  rem remove spaces for russian
set PromptLine=%PromptLine: =%
  rem parse simple case get: A,B,C else complicated case with brackets and parenthesis: A,C,E
for /f "tokens=2-7 delims=[(/)]" %%A in ( "%PromptLine%"
  ) do if "%%~E" == "" ( set lang_yes=%%A& set lang_no=%%B& set lang_all=%%C
  ) else ( set lang_yes=%%A& set lang_no=%%C& set lang_all=%%E
)
  rem Extract first char
set lang_y=%lang_yes:~0,1%& set lang_n=%lang_no:~0,1%& set lang_a=%lang_all:~0,1%
  rem Display results
echo %lang_yes%, %lang_no%, %lang_all%
echo %lang_y%, %lang_n%, %lang_a%
:FIN
pause

  rem Prompts tested, in XP or later:
rem english: set PromptLine=Overwrite x.tmp? (Yes/No/All):
rem spanish: set PromptLine=¿Sobrescribir x.tmp? (Sí/No/Todos):
rem  german: set PromptLine=x.tmp überschreiben? (Ja/Nein/Alle):
rem  french: set PromptLine=Remplacer x.tmp (Oui/Non/Tous) :
rem russian: set PromptLine=???????? x.tmp [Yes (a?)/No (b??)/All (c??)]:

Post Reply