我正在尝试创建一个批处理脚本,我可以根据指定的前缀或后缀获取所有环境变量,然后对所述值进行更新。
我知道实际的更新是SETX
命令,但我无法获取所需的变量列表。我尝试了SET
命令,但它列出了所有内容。有没有办法迭代SET
结果?或者以其他方式进行部分匹配并遍历所有发现?
答案 0 :(得分: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
对正则表达式的支持是有限且非标准的。 示例输出:
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