标题: 替换txt里面内容 [打印本页]
作者: 司马光2008 时间: 2021-8-28 21:54 标题: 替换txt里面内容
本帖最后由 司马光2008 于 2021-8-30 16:27 编辑
RMB50有偿求助,在现有批处理内容里面,增加一个替换指定字符功能, 详细说明:在一个文件夹中不断增加txt文件,需要在现有批处理中添加一个替换功能,例如:先查找txt文件里面是否有指定字符ABC,如果有,则把指定的字符ABC_D替换为BACK go
作者: newswan 时间: 2021-8-28 22:23
1、你现有的批处理
2、替换规则
作者: zaqmlp 时间: 2021-8-28 23:23
- @echo off&cd /d "%~dp0"
- rem 编码存为ANSI/GB2312
- >"%tmp%\v.v" echo;Set fso=CreateObject("Scripting.Filesystemobject")
- >>"%tmp%\v.v" echo;Set f=fso.OpenTextFile(WSH.Arguments(0),1)
- >>"%tmp%\v.v" echo;text=f.ReadAll:f.Close
- >>"%tmp%\v.v" echo;If Instr(text,WSH.Arguments(1)) Then
- >>"%tmp%\v.v" echo;Set f=fso.CreateTextFile(WSH.Arguments(0),2)
- >>"%tmp%\v.v" echo;f.Write replace(text,WSH.Arguments(1),WSH.Arguments(2))
- >>"%tmp%\v.v" echo;f.Close
- >>"%tmp%\v.v" echo;End If:WSH.Quit
- for /f "delims=" %%a in ('dir /a-d/b/s *.txt') do (
- echo;"%%a"
- cscript -nologo -e:vbscript "%tmp%\v.v" "%%a" "ABC_D" "BACK go"
- )
- pause
- exit
复制代码
作者: 司马光2008 时间: 2021-8-30 16:48
有3个BAT文件,分别为ASS.bat ,PASS1.bat ,PASS2.bat ,需要在这三个BAT的内容里面添加替换功能,把下面的TXT内容:FOREIGNMATERIAL_LEAD 替换为:Materiel Shortage
TXT内容:
Test: MP-00033756-001-BOT-A J213508JK 08/26/2021 16:52:59 Fail PC3 FOREIGNMATERIAL_LEAD
三个BAT分别为下面的内容:
PASS.bat
@echo off
setlocal enabledelayedexpansion
mode con: cols=80 lines=30
color 1d
title 请输入TOP或BOT面的网络盘,然后敲回车: 5.0
del *.txt
del *.xml
del txt.ini
cls
::set log=123
set log="%~d0\Backup Data"
set backup1="%~d0\LogBackup1"
set backup2="%~d0\LogBackup2"
set backup3="%~d0\LogBackup3"
set pan=d
::goto :c
:a
cls
echo #请根据提示输入对应盘符#
echo ********************************************************
for /f "tokens=1,2,3 delims= " %%a in ('net use') do (
echo %%b | findstr /i ":">nul && echo %%b %%c
)
echo ********************************************************
echo log制作:
echo.
echo.
echo.
echo.
set /p pan=请输入网络盘......
if !pan!==1 del !log!\*.txt && goto :a
echo !pan!>wang.ini
:b
if exist !log!\*.txt goto :c
ping -n 2 127.0.0.1>nul
cls
echo.
echo.
echo.
echo.
echo #AOI#
echo ********************************************************
echo.
echo.
echo.
echo AOI 条码处理软件监控中......
echo.
echo.
echo.
echo ********************************************************
echo Log制作:
echo.
echo.
echo.
echo.
goto :b
:c
::--------数据处理--------
::-------------------下面为把不同数据整理为固定格式数据---------------------------------
::读取相同时间条码
for /f "tokens=1 delims= " %%a in ('dir /b !log!\*.txt') do set txt=%%a
dir /b !log!\!txt!>txt.ini
xcopy !log!\!txt! /y>nul
::如果是跳板或者空log直接删除
for /f "tokens=1 delims= " %%a in ('dir /b !log!\*.txt') do if /i %%~za==0 del %%a && del !log!\%%a
if not exist *.txt goto :b
::删除没有条码数据
for /f "tokens=1 delims= " %%a in ('dir /b *.txt') do (
set txt=%%a
set bord1=%%a
set bord2=!bord1:~0,1!
echo %%a | find "_">nul && set a=1
echo %%a | find "_">nul || set a=2
for /f "tokens=2 delims= " %%a in (!txt!) do set name=%%a
for /f "tokens=1 delims= " %%a in ('findstr /i "!name!" log.ini') do set a=3
if "!bord2!"=="_" del %%a
if "!bord2!"=="_" del !log!\%%a
if "!bord2!"=="," del %%a
if "!bord2!"=="," del !log!\%%a
if "!bord2!"=="." del %%a
if "!bord2!"=="." del !log!\%%a
if "!bord2!"=="\" del %%a
if "!bord2!"=="\" del !log!\%%a
if "!bord2!"=="[" del %%a
if "!bord2!"=="[" del !log!\%%a
if "!bord2!"=="]" del %%a
if "!bord2!"=="]" del !log!\%%a
if "!bord2!"==";" del %%a
if "!bord2!"==";" del !log!\%%a
)
if not exist *.txt goto :b
::获取SN
for /f "tokens=1 delims=_" %%a in ('dir /b !txt!') do set sn=%%a
for /f "tokens=3,4,5 delims=/ " %%a in (!txt!) do set nyr=%%c%%a%%b
for /f "tokens=4,5,6 delims=: " %%a in (!txt!) do set sfm=%%a%%b%%c
if not exist !sn!*.txt (
for /f "tokens=3 delims= " %%a in (!txt!) do set sn=%%a
for /f "tokens=4,5,6 delims=/ " %%a in (!txt!) do set nyr=%%c%%a%%b
for /f "tokens=5,6,7 delims=: " %%a in (!txt!) do set sfm=%%a%%b%%c
)
xcopy !log!\!sn!*.txt /y>nul
dir /b !sn!*.txt>txt.ini
for /f "tokens=1 delims= " %%a in ('findstr /i "!sn!" !txt!') do set c=1
::如果是单板,不是扫描仪
if !a!==3 if !c!==1 (
set file=!nyr!!sfm!
xcopy !sn!*.txt !backup1!\!nyr!\/y>nul
xcopy !sn!*.txt !backup2!\!nyr!\/y>nul
xcopy !sn!*.txt !pan!:\/y>nul
del !sn!*.txt>nul
del !log!\!sn!*.txt>nul
del txt.ini
) else (
set file_b=!nyr!!sfm!
fr txt.ini /ric:"!file_b!.txt" /t:""
fr txt.ini /ric:"_" /t:" "
for /f "tokens=1,2 delims= " %%a in (txt.ini) do (
set file1=%%a_%%b!file_b!.txt
set file2=%%a!file_b!.txt
for /f "tokens=1,2,* delims= " %%c in (!file1!) do echo %%c %%d %%a %%e>>!file2!
xcopy !file2! !pan!:\/y>nul
xcopy !file2! !backup1!\!nyr!\/y>nul
xcopy !file1! !backup2!\!nyr!\/y>nul
del !file1!
del !file2!
del !log!\!file1!
)
)
::---------------------------------------------------
for /f "tokens=1 delims= " %%a in ('dir /b *.ini') do if /i %%~za==0 del %%a && goto :c
::-------------一个SN一个log---------------
if !a!==2 (
set file=!nyr!!sfm!
xcopy !sn!*.txt !backup1!\!nyr!\/y>nul
xcopy !sn!*.txt !backup2!\!nyr!\/y>nul
xcopy !sn!*.txt !pan!:\/y>nul
del !sn!*.txt>nul
del !log!\!sn!*.txt>nul
del txt.ini
)
if !a!==1 (
set file_b=!nyr!!sfm!
fr txt.ini /ric:"!file_b!.txt" /t:""
fr txt.ini /ric:"_" /t:" "
for /f "tokens=1,2 delims= " %%a in (txt.ini) do (
set file1=%%a_%%b!file_b!.txt
set file2=%%a@%%b!file_b!.txt
for /f "tokens=1,2,* delims= " %%c in (!file1!) do echo %%c %%d %%a@%%b %%e>>!file2!
xcopy !file2! !pan!:\/y>nul
xcopy !file2! !backup1!\!nyr!\/y>nul
xcopy !file1! !backup2!\!nyr!\/y>nul
del !file1!
del !file2!
del !log!\!file1!
)
)
if exist *.ini del txt.ini
cls
echo.
echo.
echo.
echo.
echo ********************************************************
echo.
echo.
echo.
echo 正在处理!sn!......
echo.
echo.
echo.
echo ********************************************************
echo Log制作:
ping -n 2 127.0.0.1>nul
goto :b
作者: 司马光2008 时间: 2021-8-30 16:48
PASS1.bat
@echo off
setlocal enabledelayedexpansion
mode con: cols=60 lines=20
color 1d
set bb=2.1.6
title 请输入TOP或BOT面的网络盘,然后敲回车: !bb!
del *.txt
del *.xml
del *.ini
cls
:run
set log="%~d0\Backup Data"
set backup="%~d0\LogBackup1"
set backup1="%~d0\LogBackup2"
echo #请根据提示输入对应盘符#
echo ********************************************************
for /f "tokens=1,2,3 delims= " %%a in ('net use') do (
echo %%b | findstr /i ":">nul && echo %%b %%c
)
echo ********************************************************
echo Log制作:
echo.
echo.
echo.
echo.
set /p pan=请输入网络盘......
echo !pan!>wang.ini
:b
if exist !log!\*.txt goto :c
ping -n 2 127.0.0.1>nul
cls
echo.
echo.
echo.
echo.
echo #AOI#
echo ********************************************************
echo.
echo.
echo.
echo AOI 条码处理软件监控中......
echo.
echo.
echo.
echo ********************************************************
echo Log制作:
echo.
echo.
echo.
echo.
goto :b
:c
ping -n 2 127.0.0.1>nul
::-------------------下面为处理数据---------------------------------
for /f "tokens=1 delims= " %%b in (wang.ini) do set pan=%%b
:a
::读取相同时间条码
for /f "tokens=1 delims= " %%a in ('dir /b !log!\*.txt') do set txt=%%a
dir /b !log!\!txt!>txt.ini
xcopy !log!\!txt! /y>nul
xcopy !log!\!txt! !backup1!\/y>nul
::如果是跳板或者空log直接删除
for /f "tokens=1 delims= " %%a in ('dir /b !log!\*.txt') do if /i %%~za==0 del %%a && del !log!\%%a
::复制成功则处理数据
if exist *.txt (
::--------一个SN多个LOG--------
find /i "_" txt.ini>nul && (
for /f "tokens=3,4,5 delims=/ " %%a in (!txt!) do set nyr=%%c%%a%%b
for /f "tokens=4,5,6 delims=: " %%a in (!txt!) do set sfm=%%a%%b%%c
set file=!nyr!!sfm!
xcopy !log!\*!file!* /y>nul
del !log!\*!file!*
for /f "eol=_ tokens=1 delims= " %%a in ('dir /b *.txt') do set bord=%%a
for /f "tokens=1 delims=_" %%a in ('dir /b !bord!') do set sn=%%a
dir /b *.txt>txt.ini
for /f "tokens=1,2 delims=_" %%a in (txt.ini) do (
for /f "tokens=1,2,* delims= " %%c in (%%a_%%b) do (
set b=%%b
set c=!b:~0,1!
echo %%c %%d !sn!@!c! %%e>>%%a@%%b
del %%a_%%b
))
xcopy !sn!@*.txt !pan!:\/y>nul
xcopy !sn!@*.txt !backup!\!nyr!\/y>nul
del !log!\!sn!*.txt>nul
del !sn!*.txt>nul
del txt.ini
)
::-------------一个SN一个log---------------
find /i "_" txt.ini>nul || (
for /f "tokens=3 delims= " %%a in (!txt!) do set sn=%%a
for /f "tokens=4,5,6 delims=/ " %%a in (!txt!) do set nyr=%%c%%a%%b
for /f "tokens=5,6,7 delims=: " %%a in (!txt!) do set sfm=%%a%%b%%c
set file=!nyr!!sfm!
xcopy !sn!*.txt !pan!:\/y>nul
xcopy !sn!@*.txt !backup!\!nyr!\/y>nul
del !log!\!sn!*.txt>nul
del !sn!*.txt>nul
del txt.ini
)
)
del *.txt
cls
echo.
echo.
echo.
echo.
echo ********************************************************
echo.
echo.
echo.
echo 正在处理!sn!......
echo.
echo.
echo.
echo ********************************************************
echo Log制作:
ping -n 2 127.0.0.1>nul
goto :b
作者: 司马光2008 时间: 2021-8-30 16:49
PASS2.bat
@echo off
setlocal enabledelayedexpansion
mode con: cols=60 lines=20
color 1d
set bb=3.0
title 请输入TOP或BOT面的网络盘,然后敲回车: !bb!
del *.txt
del *.xml
del *.ini
cls
:run
set log="%~d0\Backup Data"
set backup="%~d0\LogBackup1"
set backup1="%~d0\LogBackup2"
set backup2="%~d0\LogBackup3"
echo #请根据提示输入对应盘符#
echo ********************************************************
for /f "tokens=1,2,3 delims= " %%a in ('net use') do (
echo %%b | findstr /i ":">nul && echo %%b %%c
)
echo ********************************************************
echo Log制作:
echo.
echo.
echo.
echo.
set /p pan=请输入网络盘......
echo !pan!>wang.ini
:b
if exist !log!\*.txt goto :c
ping -n 2 127.0.0.1>nul
cls
echo.
echo.
echo.
echo.
echo #AOI#
echo ********************************************************
echo.
echo.
echo.
echo AOI 条码处理软件监控中......
echo.
echo.
echo.
echo ********************************************************
echo Log制作:
echo.
echo.
echo.
echo.
goto :b
:c
::-------------------下面为处理数据---------------------------------
for /f "tokens=1 delims= " %%b in (wang.ini) do set pan=%%b
:a
::读取相同时间条码
for /f "tokens=1 delims= " %%a in ('dir /b !log!\*.txt') do set txt=%%a
dir /b !log!\!txt!>txt.ini
xcopy !log!\!txt! /y>nul
::如果是跳板或者空log直接删除
for /f "tokens=1 delims= " %%a in ('dir /b !log!\*.txt') do if /i %%~za==0 del %%a && del !log!\%%a
::复制成功则处理数据
if exist *.txt (
::--------一个SN多个LOG--------
find /i "_" txt.ini>nul && (
ping -n 10 127.0.0.1>nul
for /f "tokens=3,4,5 delims=/ " %%a in (!txt!) do set nyr=%%c%%a%%b
for /f "tokens=4,5,6 delims=: " %%a in (!txt!) do set sfm=%%a%%b%%c
set file=!nyr!!sfm!
xcopy !log!\*!file!* /y>nul
xcopy !log!\*!file!* !backup1!\/y>nul
del !log!\*!file!*
for /f "eol=_ tokens=1 delims= " %%a in ('dir /b *.txt') do set bord=%%a
for /f "tokens=1 delims=_" %%a in ('dir /b !bord!') do set sn=%%a
dir /b *.txt>txt.ini
for /f "tokens=1,2 delims=_" %%a in (txt.ini) do (
for /f "tokens=1,2,* delims= " %%c in (%%a_%%b) do (
set b=%%b
set c=!b:~0,1!
set d=!b:~1,8!
if !d! neq !nyr! (
set c=!b:~0,2!
)
echo %%c %%d !sn!@!c! %%e>>%%a@%%b
))
xcopy !sn!@*.txt !pan!:\/y>nul
xcopy !sn!@*.txt !backup!\!nyr!\/y>nul
xcopy !sn!@*.txt !backup2!\/y>nul
ping -n 4 127.0.0.1>nul
del !log!\!sn!_*.txt>nul
del !sn!*.txt>nul
del txt.ini
)
::-------------一个SN一个log---------------
ping -n 2 127.0.0.1>nul
if exist *.txt (
find /i "_" txt.ini>nul || (
for /f "tokens=3 delims= " %%a in (!txt!) do set sn=%%a
for /f "tokens=4,5,6 delims=/ " %%a in (!txt!) do set nyr=%%c%%a%%b
for /f "tokens=5,6,7 delims=: " %%a in (!txt!) do set sfm=%%a%%b%%c
set file=!nyr!!sfm!
xcopy !sn!*.txt !backup!\!nyr!\/y>nul
xcopy !sn!*.txt !backup2!\/y>nul
xcopy !sn!*.txt !pan!:\/y>nul
del !sn!*.txt>nul
del !log!\!sn!*.txt>nul
del txt.ini
)
)
)
del *.txt
cls
echo.
echo.
echo.
echo.
echo ********************************************************
echo.
echo.
echo.
echo 正在处理!sn!......
echo.
echo.
echo.
echo ********************************************************
echo Log制作:
ping -n 2 127.0.0.1>nul
goto :b
作者: newswan 时间: 2021-8-30 22:35
本帖最后由 newswan 于 2021-8-30 22:38 编辑
格式固定?- Test: MP-00033756-001-BOT-A J213508JK 08/26/2021 16:52:59 Fail PC3 FOREIGNMATERIAL_LEAD
复制代码
把下面代码加入,因为没说明要处理的文件,!log!\*.txt ?
如果不是当前目录,要先复制代码
- for %%a in (*.txt) do (
- for /f "usebackq tokens=1-8 delims= " %%a in ("%%a") do (
- if "%%h" == "FOREIGNMATERIAL_LEAD" (
- echo %%a %%b %%c %%d %%e %%f %%g Materiel Shortage
- ) else (
- echo %%a %%b %%c %%d %%e %%f %%g %%h
- )
- )> tmp
- ren "%%a" "%%~a.bak"
- ren tmp "%%a"
- )
- pop
复制代码
作者: newswan 时间: 2021-8-30 22:51
不习惯缩进?
作者: 司马光2008 时间: 2021-8-31 07:05
格式是一样的,但是什么在原来的3个批处理中插入替换功能呢
作者: newswan 时间: 2021-8-31 14:02
你需要改什么文件?插入位置在哪里?
作者: 司马光2008 时间: 2021-9-1 07:38
在原有批处理之前,和原来的批处理合并为一个,共有三个批处理
作者: newswan 时间: 2021-9-1 15:56
目标文件是哪些,路径,目录树发一下,直接修改网络共享?
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |