如何从环境变量字符串中删除多个重复的单词(语言名称)?

时间:2017-11-24 10:15:39

标签: batch-file cmd

我现在在运行echo %subsnew%

时输出了这个输出
English English Arabic Danish Dutch Finnish French German Hindi Norwegian Swedish Turkish English Dutch French German English French German

如何删除变量subsnew中的重复项?

我需要输出:

English Arabic Danish Dutch Finnish French German Hindi Norwegian Swedish Turkish

3 个答案:

答案 0 :(得分:1)

以下是此任务的注释批处理代码:

@echo off
rem Define the languages for testing removing duplicates if not already defined.

if not defined set "subsnew=English English Arabic Danish Dutch Finnish French German Hindi Norwegian Swedish Turkish English Dutch French German English French German"

rem Create a local environment for removing the duplicates with enabled
rem command extensions needed for command FOR /F and for command SET with
rem using syntax "variable=value" and enabled delayed environment variable
rem expansion required for rebuilding the language list without duplicates.

setlocal EnableExtensions EnableDelayedExpansion

rem Create for each language an environment variable starting with LANG_
rem and name of the language with value 1. The value is not important.

for %%I in (%subsnew%) do set "LANG_%%I=1"

rem Delete the environment variable subsnew.

set "subsnew="

rem Run command SET with argument LANG_ to get listed alphabetically sorted
rem all environment variables starting with LANG_ line by line in format
rem variable=value, e.g. LANG_English=1, LANG_Finnish=1, ...

rem Command FOR executes the command in a separate command process started
rem with cmd.exe /C in background and captures all lines output to handle
rem STDOUT for processing them next line by line.

rem Each line is split up into substrings using underscore and equal sign
rem as string delimiters. The first string is always LANG which is of no
rem interest. The third string is always 1 which is the value of each
rem LANG_* environment variable after the equal sign which is also of no
rem interest. Therefore only the second string being the language string
rem is processed further by assigning it to loop variable I.

rem The environment variable subsnew is rebuild with the language strings.
rem Delayed environment variable expansion is needed as the help output on
rem running in a command prompt window the command SET /? explains on a very
rem similar example to the command line below.

for /F "tokens=2 delims=_=" %%I in ('set LANG_') do set "subsnew=!subsnew! %%I"

rem Restore previous environment which means discarding all environment
rem variables defined and modified after command SETLOCAL above.

rem But the new value of environment variable subsnew is needed in previous
rem environment. So subsnew must be defined again in previous environment
rem with the current value of subsnew in current environment without the
rem space at begin which is done with the command line below.

endlocal & set "subsnew=%subsnew:~1%"

rem Output value of environment variable subsnew with the languages
rem sorted alphabetically and duplicate language strings removed.

echo %subsnew%
pause

请注意,此方法仅适用于语言字符串本身不包含空格字符的语言字符串。但看起来这不是问题。

要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。

  • echo /?
  • endlocal /?
  • for /?
  • pause /?
  • rem /?
  • set /?
  • setlocal /?

答案 1 :(得分:1)

这与Mofi提供的想法类似,不过它应该更快一些,并且应保持唯一变量值的顺序。

@Echo Off
Set "subsnew=English English Arabic Danish Dutch Finnish French German Hindi Norwegian Swedish Turkish English Dutch French German English French German"
SetLocal EnableDelayedExpansion
Set "tmpExt=.$%RANDOM%"
Set "tmpVar="
For %%A In (%subsnew%) Do If Not Exist "%%A%tmpExt%" (Break>"%%A%tmpExt%"
    Set "tmpVar=!tmpVar! %%A")
Del/Q "*%tmpExt%"
EndLocal&Set "subsnew=%tmpVar:~1%
Echo %subsnew%
Pause

前两行和后两行仅用于说明目的,只有那些行之间的行需要粘贴到现有脚本中。

答案 2 :(得分:1)

这可以在只有2个语句的单个循环中完成。从集合中删除当前元素(这是一个棘手的部分,使用参数扩展从字符串中删除变量子字符串);如果集合没有改变,那么该元素尚未包含并随后被添加。

@echo off
REM input is a string with names
REM return a string with unique names
REM 5:00:06 PM Friday 24/11/2017

if not defined subsnew set "subsnew=English English Arabic Danish Dutch Finnish French German Hindi Norwegian Swedish Turkish English Dutch French German English French German"

setlocal EnableDelayedExpansion

REM collection may not be empty
set "collection= "
for %%a in (%subsnew%) do (
    REM remove the current name from the collection
    set "test=!collection:%%a=!"
    REM if the collection has not changed, name was missing so it is added now
    if !test!==!collection! set "collection=!collection! %%a"
)
REM remove the leading 2 spaces
set collection=!collection:~2!

echo final string is ^>%collection%^<