<# :
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)
}
不清楚你的实际文件/情况,仅以问题中的样例/说明为据;以下代码复制粘贴到记事本,另存为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]::GetEncoding('GB2312')))) -Args '%~f0'"
echo%#% +%$%%$%/%@% %z%
pause
exit
#>
$ext=@(".txt")
$self=get-item -liter $args[0]
$path=$self.Directory.FullName
$newfolder=$path+'\#result'
if(-not (test-path -liter $newfolder)){[void](md $newfolder)}
$enc=[Text.Encoding]::GetEncoding('GB2312')
$files=@(dir -liter $path|?{($ext -contains $_.Extension) -and ($_ -is [System.IO.FileInfo])})
[System.Collections.ArrayList]$s=@()write-host $files[0].Name
$text=[IO.File]::ReadAllLines($files[0].FullName, $enc)
for($j=0$j -lt $text.count$j++){
$line=$files[0].BaseName+($j+1).toString()+' '+$text[$j]
[void]$s.add($line)
}
$newfile=$newfolder+'\'+$files[0].Name
[IO.File]::WriteAllLines($newfile, $s, $enc)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)