有没有办法遍历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
进一步阅读