【问题标题】:loop through all environmental variables and take actions depending on prefix循环遍历所有环境变量并根据前缀采取行动
【发布时间】:2016-08-27 16:23:23
【问题描述】:

我正在尝试制作一个批处理脚本,我可以在其中根据指定的前缀或后缀获取所有环境变量,然后对所述值进行更新。

我知道实际更新将是一个SETX 命令,但我无法获取我需要的变量列表。我尝试了SET 命令,但它列出了所有内容。有没有办法遍历SET 结果?或者以其他方式进行部分匹配并循环遍历所有发现?

【问题讨论】:

标签: windows batch-file scripting


【解决方案1】:

有没有办法遍历SET 结果?

您可以使用for /f 循环查看结果。

DisplayEnv.cmd:

@echo off 
setlocal
for /f "delims== tokens=1,2" %%a in ('set') do (
  echo Variable: %%a, Value: %%b
  )
endlocal

示例输出:

F:\test>DisplayEnv.cmd
Variable: ALLUSERSPROFILE, Value: C:\ProgramData
Variable: APPDATA, Value: C:\Users\DavidPostill\AppData\Roaming
Variable: asl.log, Value: Destination
Variable: CommonProgramFiles, Value: C:\Program Files\Common Files
Variable: CommonProgramFiles(x86), Value: C:\Program Files (x86)\Common Files
Variable: CommonProgramW6432, Value: C:\Program Files\Common Files
Variable: COMPUTERNAME, Value: HAL
Variable: ComSpec, Value: C:\Windows\system32\cmd.exe
Variable: configsetroot, Value: C:\Windows\ConfigSetRoot
Variable: DOCS, Value: C:\Users\DavidPostill\Documents
Variable: FP_NO_HOST_CHECK, Value: NO
Variable: HOMEDRIVE, Value: C:
Variable: HOMEPATH, Value: \Users\DavidPostill
Variable: LOCALAPPDATA, Value: C:\Users\DavidPostill\AppData\Local
Variable: LOGONSERVER, Value: \\HAL
Variable: NUMBER_OF_PROCESSORS, Value: 4
Variable: OS, Value: Windows_NT
Variable: Path, Value: C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\apps\WSCC\Sysinternals Suite;C:\apps\WSCC\NirSoft Utilities
Variable: PATHEXT, Value: .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
Variable: PROCESSOR_ARCHITECTURE, Value: AMD64
Variable: PROCESSOR_IDENTIFIER, Value: Intel64 Family 6 Model 42 Stepping 7, GenuineIntel
Variable: PROCESSOR_LEVEL, Value: 6
Variable: PROCESSOR_REVISION, Value: 2a07
Variable: ProgramData, Value: C:\ProgramData
Variable: ProgramFiles, Value: C:\Program Files
Variable: ProgramFiles(x86), Value: C:\Program Files (x86)
Variable: ProgramW6432, Value: C:\Program Files
Variable: PROMPT, Value: $P$G
Variable: PSModulePath, Value: C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules
Variable: PUBLIC, Value: C:\Users\Public
Variable: RANDFILE, Value: C:\apps\NTP\etc\.rnd
Variable: SESSIONNAME, Value: Console
Variable: SystemDrive, Value: C:
Variable: SystemRoot, Value: C:\Windows
Variable: TEMP, Value: c:\temp
Variable: TMP, Value: c:\temp
Variable: USERDOMAIN, Value: Hal
Variable: USERNAME, Value: DavidPostill
Variable: USERPROFILE, Value: C:\Users\DavidPostill
Variable: windir, Value: C:\Windows
Variable: windows_tracing_flags, Value: 3
Variable: windows_tracing_logfile, Value: C:\BVTBin\Tests\installpackage\csilogfile.log

我可以对结果进行部分匹配吗?

上面的批处理文件可以使用findstr来过滤for循环。

FindInEnv.cmd:

@echo off 
setlocal
for /f "delims== tokens=1,2" %%a in ('set ^| findstr /i / "%1"') do (
  echo Variable: %%a, Value: %%b
  )
endlocal

用法:

  • 将要匹配的字符串作为参数传递给FindInEnv
  • 删除/i 执行区分大小写的搜索。
  • findstr 对正则表达式的支持是有限且非标准的。
  • 请参阅findstr 了解支持的详细信息。

示例输出:

F:\test>FindInEnv.cmd Program*
Variable: ALLUSERSPROFILE, Value: C:\ProgramData
Variable: CommonProgramFiles, Value: C:\Program Files\Common Files
Variable: CommonProgramFiles(x86), Value: C:\Program Files (x86)\Common Files
Variable: CommonProgramW6432, Value: C:\Program Files\Common Files
Variable: ProgramData, Value: C:\ProgramData
Variable: ProgramFiles, Value: C:\Program Files
Variable: ProgramFiles(x86), Value: C:\Program Files (x86)
Variable: ProgramW6432, Value: C:\Program Files
Variable: PSModulePath, Value: C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules

进一步阅读

【讨论】:

    猜你喜欢
    • 2021-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-09
    • 1970-01-01
    • 2018-04-17
    • 2019-01-07
    • 1970-01-01
    相关资源
    最近更新 更多