Board logo

标题: [文本处理] 批处理中变量延迟及管道符怎么使用? [打印本页]

作者: hzs1997    时间: 2016-9-21 13:53     标题: 批处理中变量延迟及管道符怎么使用?

见图或下面拷屏,涉及变量延缓及管道的问题,实在想不明白,请高手指点,谢谢。
===============================
C:\>more b.bat
@Echo Off & Setlocal Enabledelayedexpansion & setlocal EnableExtensions
set a=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\11
set b=    ServiceName REG_SZ  {4A966A11-AE30-4187-B6D1-7C029202087D}
set c=    Description REG_SZ  Realtek RTL8139/810x Family Fast Ethernet NIC
@echo !a! ^| findstr /C:"Realtek RTL8139/810x Family Fast Ethernet NIC" >nul  && echo YES || echo NO
@echo !b! ^| findstr /C:"Realtek RTL8139/810x Family Fast Ethernet NIC" >nul  && echo YES || echo NO
@echo !c! ^| findstr /C:"Realtek RTL8139/810x Family Fast Ethernet NIC" >nul  && echo YES || echo NO

C:\>b.bat
YES
YES
YES

C:\>@echo HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\11 | findstr /C:"Realtek RTL8139/810x Family Fast Ethernet NIC" >nul && echo YES || echo NO
NO

C:\>@echo     ServiceName REG_SZ  {4A966A11-AE30-4187-B6D1-7C029202087D} | findstr /C:"Realtek RTL8139/810x Family Fast Ethernet NIC" >nul && echo YES || echo NO
NO

C:\>@echo     Description REG_SZ  Realtek RTL8139/810x Family Fast Ethernet NIC | findstr /C:"Realtek RTL8139/810x Family Fast Ethernet NIC" >nul && echo YES || echo NO
YES
作者: pcl_test    时间: 2016-9-21 13:59

建议LZ先把基础学好
  1. @echo off
  2. set "a=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\11"
  3. set "b=    ServiceName REG_SZ  {4A966A11-AE30-4187-B6D1-7C029202087D}"
  4. set "c=    Description REG_SZ  Realtek RTL8139/810x Family Fast Ethernet NIC"
  5. echo,%a%|find "Realtek RTL8139/810x Family Fast Ethernet NIC">nul&&echo YES||echo NO
  6. echo,%b%|find "Realtek RTL8139/810x Family Fast Ethernet NIC">nul&&echo YES||echo NO
  7. echo,%c%|find "Realtek RTL8139/810x Family Fast Ethernet NIC">nul&&echo YES||echo NO
  8. pause
复制代码

作者: hzs1997    时间: 2016-9-21 14:26

回复 2# pcl_test
该脚本仅仅实际脚本抽出来测试错误的一部分,实际脚本用有使用for及需启用环境变量延缓,而不仅仅是预处理的变量替换。在启用环境变量延缓的情况下应该如何处理?谢谢。
作者: hzs1997    时间: 2016-9-21 15:56

已解决,见下
=============================
C:\>more b.bat
@Echo Off & Setlocal Enabledelayedexpansion & setlocal EnableExtensions
set a=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\11
set b=    ServiceName REG_SZ  {4A966A11-AE30-4187-B6D1-7C029202087D}
set c=    Description REG_SZ  Realtek RTL8139/810x Family Fast Ethernet NIC
echo !a! | findstr /C:"Realtek RTL8139/810x Family Fast Ethernet NIC" >nul  && echo YES || echo NO
echo !b! | findstr /C:"Realtek RTL8139/810x Family Fast Ethernet NIC" >nul  && echo YES || echo NO
echo !c! | findstr /C:"Realtek RTL8139/810x Family Fast Ethernet NIC" >nul  && echo YES || echo NO
echo.
echo !a! ^| findstr /C:"Realtek RTL8139/810x Family Fast Ethernet NIC" >nul  && echo YES || echo NO
echo !b! ^| findstr /C:"Realtek RTL8139/810x Family Fast Ethernet NIC" >nul  && echo YES || echo NO
echo !c! ^| findstr /C:"Realtek RTL8139/810x Family Fast Ethernet NIC" >nul  && echo YES || echo NO
echo.
endlocal Enabledelayedexpansion & endlocal EnableExtensions
echo %a% | findstr /C:"Realtek RTL8139/810x Family Fast Ethernet NIC" >nul  && echo YES || echo NO
echo %b% | findstr /C:"Realtek RTL8139/810x Family Fast Ethernet NIC" >nul  && echo YES || echo NO
echo %c% | findstr /C:"Realtek RTL8139/810x Family Fast Ethernet NIC" >nul  && echo YES || echo NO

C:\>b.bat
NO
NO
YES

YES
YES
YES

NO
NO
YES

C:\>




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2