- <# :
- cls
- @echo off
- cd /d "%~dp0"
- powershell -NoProfile -ExecutionPolicy bypass "Invoke-Command -ScriptBlock ([ScriptBlock]::Create([IO.File]::ReadAllText('%~f0',[Text.Encoding]::Default))) -Args '%~dp0'"
- pause
- exit
- #>
- $word="qwert";
- $path=$args[0].trimend('\');
- $newpath=$path+'\#result';
- $files=@(dir -liter $path -recurse|?{('.txt' -eq $_.Extension) -and ($_ -is [System.IO.FileInfo])});
- $enc=[Text.Encoding]::Default;$regstr=[regex]::Escape($word)+'[\s\S]+?(?='+[regex]::Escape($word)+'|$)';
- for($i=0;$i -lt $files.length;$i++){
- write-host $files[$i].FullName;
- $newfolder=$newpath+'\'+$files[$i].Directory.Name;
- if(-not (test-path -liter $newfolder)){[void](md $newfolder -force);};
- $text=[IO.File]::ReadAllText($files[$i].FullName,$enc);
- $m=[regex]::matches($text,$regstr);$n=1;
- foreach($it in $m){
- $newfile=$newfolder+'\'+$files[$i].BaseName+'_'+$n.toString()+$files[$i].Extension;
- [IO.File]::WriteAllText($newfile, $it.groups[0].value, $enc);
- $n++;
- };
- }
复制代码
|