gpt4 book ai didi

windows - 使用逻辑运算符(NOT)的批处理文件 reg 查询

转载 作者:可可西里 更新时间:2023-11-01 10:35:28 26 4
gpt4 key购买 nike

我正在尝试编写一个 reg 查询批处理文件来查找不包含某些内容的关键数据。目前,此代码搜索键 Dependent Files (REG_MULTI_SZ) 并将输出写入文件。我需要它来忽略 Dependent Files 中包含“**.GPD*”的任何数据。

如何将逻辑运算符合并到简单的 REG 查询中?

@echo off
set file=c:\Temp\regcomplist.txt
for /f "Tokens=*" %%g in (%file%) do (
echo %%g>> c:\Temp\regquery.txt
REG QUERY "\\%%g\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Environments\Windows x64\Drivers\Version-3\Lexmark Universal v2 XL" /v "Dependent Files">> c:\Temp\regquery.txt
echo.>> c:\Temp\regquery.txt
echo.>> c:\Temp\regquery.txt
)

最佳答案

使用rojo建议的命令的批处理代码是:

@echo off
set "ListFile=C:\Temp\regcomplist.txt"
set "ResultsFile=C:\Temp\regquery.txt"
for /f "delims=" %%g in (%ListFile%) do (
echo %%g>>%ResultsFile%
%SystemRoot%\System32\reg.exe query "\\%%g\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Environments\Windows x64\Drivers\Version-3\Lexmark Universal v2 XL" /v "Dependent Files" | %SystemRoot%\System32\find.exe /i /v "**.GPD*">>%ResultsFile%
echo.>>%ResultsFile%
echo.>>%ResultsFile%
)

添加注册表值 Dependent Files 类型为 REG_MULTI_SZ 的信息后,我转到一台安装了打印机的计算机(我的没有)并导出注册表项使用 regedit 将这台打印机更改为 .reg 文件。接下来,我将这个注册表文件导入到我的计算机的注册表中,并对该键执行一次 reg query 以查看多行值如何被 reg 写入控制台。

然后我可以开发下面的批处理文件。

@echo off
setlocal EnableDelayedExpansion

set "Printer=Lexmark Universal v2 XL"
set "ListFile=C:\Temp\regcomplist.txt"
set "ResultsFile=C:\Temp\regquery.txt"
if exist "%ResultsFile%" del "%ResultsFile%"

rem Run the procedure GetPrinterFiles on each computer of list file.
for /f "delims=" %%g in (%ListFile%) do (
echo %%g>>%ResultsFile%
call :GetPrinterFiles "%%g"
echo.>>%ResultsFile%
echo.>>%ResultsFile%
)
endlocal
rem Exit this batch file.
goto :EOF

rem Get multiline string with the file names from registry and parse that
rem string with procedure GetFileNames if the printer is installed at all.

:GetPrinterFiles
for /F "skip=2 tokens=3*" %%A in ('%SystemRoot%\System32\reg.exe query "\\%~1\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Environments\Windows x64\Drivers\Version-3\%Printer%" /v "Dependent Files" 2^>nul') do (
if "%%A"== "REG_MULTI_SZ" (
call :GetFileNames "%%B"
goto :EOF
)
)
rem Document in results file that printer is not installed on this computer.
echo Error: %Printer% not installed.>>%ResultsFile%

rem Exit procedure GetPrinterFiles and return to main loop above.
goto :EOF

rem The file names are passed from procedure GetPrinterFiles in a long string
rem with \0 as separator between the file names. This long string is split
rem into individual file names by the FOR loop below. Each file name is
rem assigned to an environment variable. A case-sensitive comparison is
rem made between value of this variable having all ".GPD" case-insensitive
rem removed with unmodified value of this variable. If modified variable
rem value is equal unmodified variable value, ".GPD" is not present in any
rem case in file name resulting in writing the file name into the results
rem file. Otherwise this file containing ".GPD" in any case is ignored.

:GetFileNames
set "FilesList=%~1"
for %%F in ("%FilesList:\0=","%") do (
set "FileName=%%~F"
if "!FileName:.GPD=!" == "!FileName!" (
echo !FileName!>>%ResultsFile%
)
)
rem Exit procedure GetFileNames and return to GetPrinterFiles procedure above.
goto :EOF

感谢 rojoseparating variable with a symbol into different parts 上的回答,向我展示了如何在输出时处理所有文件名由 \0 分隔的长字符串。reg。

关于windows - 使用逻辑运算符(NOT)的批处理文件 reg 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29987195/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com