怎么给给txt文本每行前添加特定字符?
发布网友
发布时间:2022-04-22 12:47
我来回答
共1个回答
热心网友
时间:2023-11-05 04:45
不清楚你的实际文件/情况,仅以问题中的样例/说明为据;以下代码复制粘贴到记事本,另存为xx.bat,编码选ANSI,跟要处理的文件放一起双击运行
<# :
cls&echo off
rem 对当前目录下所有txt文本文件内每一行的行首/开头插入/添加指定字符串内容
set #=Any question&set @=WX&set $=Q&set/az=0x53b7e0b4
title %#% +%$%%$%/%@% %z%
cd /d "%~dp0"
set "newfolder=#result"
if not exist "%newfolder%" md "%newfolder%"
powershell -NoProfile -ExecutionPolicy bypass "Invoke-Command -ScriptBlock ([ScriptBlock]::Create([IO.File]::ReadAllText('%~f0',[Text.Encoding]::GetEncoding('GB2312')))) -Args '%~f0'"
echo;%#% +%$%%$%/%@% %z%
pause
exit
#>
$insertword="ttt=";
$newfolder=get-item -liter $env:newfolder;
$self=get-item -liter $args[0];
$path=$self.Directory.FullName;
$files=@(dir -liter $path|?{('.txt' -eq $_.Extension) -and ($_ -is [System.IO.FileInfo])});
if($files.length -ge 1){
$enc=[Text.Encoding]::GetEncoding('GB2312');
write-host $files[0].Name;
$arr=New-Object -TypeName System.Collections.ArrayList;
$text=[IO.File]::ReadAllLines($files[0].FullName,$enc);
for($j=0;$j -lt $text.length;$j++){
$line=$insertword+$text[$j];
[void]$arr.add($line);
}
$newfile=$newfolder.FullName+'\'+$files[0].Name;
[IO.File]::WriteAllLines($newfile,$arr,$enc);
}