Board logo

标题: [文本处理] 批处理怎样查找txt文本并输出为变量? [打印本页]

作者: Yeyongmao    时间: 2015-11-3 23:23     标题: 批处理怎样查找txt文本并输出为变量?

现有a.txt文本(文本内是一个电脑物理地址),b.txt文本内是(物理地址=ip地址,这里面有几百个这样的组合)
要求是以a中的物理地址为基础查找b中的ip地址并输出变量c。求大神们指点。
作者: /zhqsystem/zhq    时间: 2015-11-4 01:22

本帖最后由 /zhqsystem/zhq 于 2015-11-4 01:29 编辑
  1. @echo off&color 0e
  2. for /f "delims=" %%i in ('type "%~dp0\a.txt"') do (
  3. for /f "tokens=1,* delims==" %%j in ('type "%~dp0\b.txt"') do (
  4.   if /i "%%~nxi"=="%%~nxj" (
  5.    set "c=%%j^=%%k"
  6.    echo=%%j=%%k
  7.   )
  8. )
  9. )
  10. echo=%c%
  11. pause
复制代码

作者: DAIC    时间: 2015-11-4 08:55

  1. @echo off
  2. (for /f "tokens=2 delims==" %%i in ('findstr /g:a.txt b.txt') do (
  3.     echo %%i
  4. ))>c.txt
复制代码
  1. @echo off
  2. for /f "tokens=2 delims==" %%i in ('findstr /g:a.txt b.txt') do (
  3.     set "c=%%i"
  4. )
  5. echo %c%
  6. pause
复制代码

作者: Yeyongmao    时间: 2015-11-6 10:42

回复 3# DAIC


    为什么第二段代码在应用时经常报(ecoh处于关闭状态)的错误呢?我找了好久都没有找到哪里错了,麻烦您帮忙看看好吗 ?
这是我整段代码!

@echo off
echo 正在设置网络参数,请不要操作。
echo 获取MAC地址
for /f "tokens=12 delims= " %%i in ('ipconfig/all^|find /i "phys"')do if not defined m set "m=%%i"
if not exist C:\自动填写ip md C:\自动填写ip
echo %m%>C:\自动填写ip\a.txt
echo 计算IP地址
(for /f "tokens=2 delims==" %%i in ('findstr /g:a.txt b.txt') do (
    echo %%i
))>c.txt
echo 正在设置IP及子网掩码

cmd /c netsh interface ip set address name="本地连接" source=static addr=%C% mask=255.255.255.0 gateway=192.168.70.1 gwmetric=1

echo 正在设置DNS服务器

cmd /c netsh interface ip set dns name="本地连接" source=static addr=202.99.96.68 register=PRIMARY
taskkill /f /im excel.exe /t
del C:\自动填写ip\a.txt
exit
作者: DAIC    时间: 2015-11-6 20:13

回复 4# Yeyongmao


    echo %%i 改成 echo,%%i




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