标题: [文本处理] [已解决]如何要bat来删除文件中的特定字符串,求解 [打印本页]
作者: BingjianREMIX 时间: 2023-5-20 13:16 标题: [已解决]如何要bat来删除文件中的特定字符串,求解
本帖最后由 BingjianREMIX 于 2023-5-23 20:38 编辑
在一个日志文件中,找到“text”并删除
要求:其它(类似于text1)保留,就是:
123.log中:text,text1,text2.
处理后123.log中:text1,text2.
作者: jyswjjgdwtdtj 时间: 2023-5-20 14:21
set fso=createobject("scripting.filesystemobject")
log=fso.opentextfile("xxx.log",1).readall()
set re=new regexp
re.pattern="(\b)text(\b)"
re.global=true
fso.opentextfile("xxx.log",2).write re.replace(log,"$1$2")
作者: BingjianREMIX 时间: 2023-5-20 15:55
set fso=createobject("scripting.filesystemobject")
log=fso.opentextfile("xxx.log",1).readall()
set ...
jyswjjgdwtdtj 发表于 2023-5-20 14:21
但是咋样用呢
作者: LTSC1809 时间: 2023-5-20 16:10
回复 3# BingjianREMIX
这个应该是vbs,要保存为.vbs文件
作者: BingjianREMIX 时间: 2023-5-20 17:14
回复 4# LTSC1809 "非法赋值‘log’"
作者: 77七 时间: 2023-5-20 17:24
- @echo off
- (for /f "delims=" %%a in ('type "1.txt"^|findstr /n .*') do (
- set "str=%%a"
- setlocal enabledelayedexpansion
- set "str=!str:“text”=!"
- echo=!str:*:=!
- endlocal
- ))>2.txt
- pause
复制代码
将文本和批处理都保存为ansi编码
作者: buyiyang 时间: 2023-5-20 18:16
回复 5# BingjianREMIX
将第2、6行的log改为logg看看,或者用- powershell -c "gc 1.log | %%{$_ -replace '\btext\b'} | Set-Content 1.log"
复制代码
- sed -i "/\btext\b/s///g" 1.log
复制代码
作者: jyswjjgdwtdtj 时间: 2023-5-20 22:38
回复 5# BingjianREMIX
你把xxx.log改成你的目标文件 如果文件为空会报错
作者: BingjianREMIX 时间: 2023-5-20 22:39 标题: 如何要bat来删除文件中的特定字符串,求解
写了一些代码来实现注册登录重置和删除,但是删除一直有问题,求解- :login
- cd /d %~dp0
- title MICRODOS LOGIN ^& REGISTER
- setlocal enabledelayedexpansion
- if not exist urini.ini echo. >urini.ini
- cls
-
- :users2
- color 07
- cls
- echo.
- echo -------------------------
- echo MICRODOS LOGIN ^& REGISTER
- echo -------------------------
- echo.
- echo 1.Login
- echo.
- echo 2.register
- echo.
- echo 3.Reset users
- echo.
- echo 4.delete users
- echo.
- set /p a=Enter you choose:
- if /i "%a%"=="4" goto :delete4
- if /i "%a%" equ "3" (
- del urni.ini
- echo. >urini.ini
- cls
- echo RESET COMPLETE!
- pause>nul
- goto :users2
- ) else if /i "%a%" equ "2" (
- call :register
- goto :users2
- ) else if "%a%" equ "1" (
- set /p str1=Enter the USER NAME:
- set /p str2=Enter the password:
- findstr /x /c:"USER_NAME:!str1!_PASSWORD:!str2!" "urini.ini" 1>nul && (
- cls
- color 27
- echo TRUE!
- echo Please push any key to continue!
- pause>nul
- goto :dos
- ) || (
- cls
- color 47
- echo ERROR!
- echo Please push any key to continue!
- pause>nul
- goto :users2
- )
- ) else goto :users2
-
- :register
- cls
- :str1
- cls
- set "str1="
- set /p "str1=Enter the new user name:"
- if "x%str1%" equ "x" (
- echo Invalid user name.
- goto :str1
- )
-
- :str2
- cls
- set "str2="
- set /p "str2=Enter the new password:"
- if "x%str2%" equ "x" (
- echo Invalid password.
- goto :str2
- )
- >>"urini.ini" echo USER_NAME:%str1%_PASSWORD:%str2%
- goto :users2
-
- :delete4
- echo on
- echo What user do you want to delete:
- set /p ax=
- echo What the user %ax%'s password:
- set /p bx=
- findstr /x /c:"USER_NAME:%ax%_PASSWORD:%bx%" "urini.ini" 1>nul
- for /f %%i in (urini.ini) do ( set "url=%%i"
- set url="USER_NAME:%ax%_PASSWORD:%bx%"
-
- )<nul >>"urini(2).ini"
- pause
- del urini.ini
- ren "urini(2).ini" "urini.ini"
- cls
- echo DELETE COMPLETE!
- pause>nul
- goto :users2
复制代码
有无大佬可以完美解决,在以上的源码的前提下实现的,非常感谢
作者: jyswjjgdwtdtj 时间: 2023-5-20 22:40
当然也有可能log是个保留词(我不知道?)那就把log改成随便啥
作者: BingjianREMIX 时间: 2023-5-20 22:42
回复 9# jyswjjgdwtdtj 你可以去看看另一个我的同名帖子那里情况更详细
作者: 77七 时间: 2023-5-21 02:55
- :delete4
- @echo off
- echo What user do you want to delete:
- set /p ax=
- echo What the user %ax%'s password:
- set /p bx=
- findstr /x /c:"USER_NAME:%ax%_PASSWORD:%bx%" "urini.ini" 1>nul
- if %errorlevel% equ 0 (
- findstr /v /x /c:"USER_NAME:%ax%_PASSWORD:%bx%" "urini.ini" >"urini.ini.temp"
- ) else (
- echo not exist "USER_NAME:%ax%_PASSWORD:%bx%"
- goto :delete4
- )
- move /y "urini.ini.temp" "urini.ini"
- cls
- echo DELETE COMPLETE!
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |