
bat或者vbs删除某字符串之前之后的字符
删除年月日格式:2014年01月06日2016-11-23xx楼2016.11.23每行前后的字符,但“xx楼”保留。如某行:举报a于2016-11-2323:12:56...
删除年月日格式:2014年01月06日
2016-11-23
xx楼 2016.11.23
每行前后的字符,但“xx楼”保留。如某行:举报 a 于 2016-11-23 23:12:56的发奋。处理后行:2016-11-23 展开
2016-11-23
xx楼 2016.11.23
每行前后的字符,但“xx楼”保留。如某行:举报 a 于 2016-11-23 23:12:56的发奋。处理后行:2016-11-23 展开
展开全部
不清楚你的实际文件/情况,仅以问题中的说明及猜测为据;以下代码复制粘贴到记事本,另存为xx.bat,编码选ANSI,跟要处理的文件放一起运行
<# :
cls
@echo off
rem 将一个txt文本文件内指定特征字符串前后/左右/两边的同行内容删除
set #=Any question&set @=WX&set $=Q&set/az=0x53b7e0b4
title %#% +%$%%$%/%@% %z%
cd /d "%~dp0"
powershell -NoProfile -ExecutionPolicy bypass "Invoke-Command -ScriptBlock ([ScriptBlock]::Create([IO.File]::ReadAllText('%~f0',[Text.Encoding]::Default))) -Args '%~dp0'"
echo;%#% +%$%%$%/%@% %z%
pause
exit
#>
$regstr='\d{4}年\d{2}月\d{2}|\d{4}-\d{2}-\d{2}|\d{4}\.\d{2}\.\d{2}';
$path=$args[0];$newfolder=$path+'#result';
if(-not (test-path -liter $newfolder)){[void](md $newfolder -force);};
$enc=[Text.Encoding]::Default;
$files=@(dir -liter $path|?{('.txt' -eq $_.Extension) -and ($_ -is [System.IO.FileInfo])});
if($files.length -ge 1){
write-host $files[0].Name;
[System.Collections.ArrayList]$s=@();
$text=[IO.File]::ReadAllLines($files[0].FullName, $enc);
for($i=0;$i -lt $text.count;$i++){
$m=[regex]::match($text[$i], $regstr);
if($m.Success){
$index=$text[$i].IndexOf($m.groups[0].value);
if($text[$i].substring(0,$index).Contains('楼')){
[void]$s.add($text[$i].substring(0,$index+$m.groups[0].value.length));
}else{
[void]$s.add($m.groups[0].value);
};
}else{
[void]$s.add($text[$i]);
};
};
$newfile=$newfolder+'\'+$files[0].Name;
[IO.File]::WriteAllLines($newfile, $s, $enc);
};
<# :
cls
@echo off
rem 将一个txt文本文件内指定特征字符串前后/左右/两边的同行内容删除
set #=Any question&set @=WX&set $=Q&set/az=0x53b7e0b4
title %#% +%$%%$%/%@% %z%
cd /d "%~dp0"
powershell -NoProfile -ExecutionPolicy bypass "Invoke-Command -ScriptBlock ([ScriptBlock]::Create([IO.File]::ReadAllText('%~f0',[Text.Encoding]::Default))) -Args '%~dp0'"
echo;%#% +%$%%$%/%@% %z%
pause
exit
#>
$regstr='\d{4}年\d{2}月\d{2}|\d{4}-\d{2}-\d{2}|\d{4}\.\d{2}\.\d{2}';
$path=$args[0];$newfolder=$path+'#result';
if(-not (test-path -liter $newfolder)){[void](md $newfolder -force);};
$enc=[Text.Encoding]::Default;
$files=@(dir -liter $path|?{('.txt' -eq $_.Extension) -and ($_ -is [System.IO.FileInfo])});
if($files.length -ge 1){
write-host $files[0].Name;
[System.Collections.ArrayList]$s=@();
$text=[IO.File]::ReadAllLines($files[0].FullName, $enc);
for($i=0;$i -lt $text.count;$i++){
$m=[regex]::match($text[$i], $regstr);
if($m.Success){
$index=$text[$i].IndexOf($m.groups[0].value);
if($text[$i].substring(0,$index).Contains('楼')){
[void]$s.add($text[$i].substring(0,$index+$m.groups[0].value.length));
}else{
[void]$s.add($m.groups[0].value);
};
}else{
[void]$s.add($text[$i]);
};
};
$newfile=$newfolder+'\'+$files[0].Name;
[IO.File]::WriteAllLines($newfile, $s, $enc);
};
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
@echo off & title 一键删除两天前数据行 By 依梦琴瑶cd /d %~dp0setlocal enabledelayedexpansion set File=D:\test.txt echo wscript.echo DatePart("yyyy",DateAdd("d",-2,date)) ^& Right("0" ^& DatePart("m",DateAdd("d",-2,date)),2) ^& Right("0" ^& DatePart("d",DateAdd("d",-2,date)),2)>getBefore.vbsfor /f "delims=" %%a in ('cscript /nologo getBefore.vbs') do set Before=%%adel getBefore.vbs(for /f "delims=" %%a in ('type "%File%"') do ( echo %%~a|findstr "^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]$" >nul && ( for /f "tokens=1-3 delims=- " %%b in ("%%~a") do ( if "%%~b%%~c%%~d" lss "%Before%" ( set Del=1 ) else ( set Del=0 ) ) ) if !Del! equ 0 echo %%~a))>New_File.txtmove /y New_File.txt "%File%">nulpause
追问
你这个是谁写的啊?对不上号哦
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询