怎么用批处理把文件夹下的所有任意txt文件的每行行首加上任意字符 比如?

怎么用批处理把文件夹下的所有任意txt文件的每行行首加上任意字符 比如?,第1张

不清楚你的实际文件/情况,仅以问题中的样例/说明为据;以下代码复制粘贴到记事本,另存为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)

}

不清楚你的实际文件/情况,仅以问题中的样例/说明为据;以下代码复制粘贴到记事本,另存为xx.bat,编码选ANSI,跟要处理的文件放一起双击运行

<# :

cls&echo off&mode con lines=5000

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="首行固定的文字`r`n"

$newfolder=get-item -liter $env:newfolder

$self=get-item -liter $args[0]

$path=$self.Directory.FullName

$enc=[Text.Encoding]::GetEncoding('UTF-8')

$files=@(dir -liter $path|?{('.txt' -eq $_.Extension) -and ($_ -is [System.IO.FileInfo])})

if($files.length -ge 1){

write-host $files[0].Name

$text=$insertword+[IO.File]::ReadAllText($files[0].FullName,$enc)

$newfile=$newfolder.FullName+'\'+$files[0].Name

[IO.File]::WriteAllText($newfile,$text,$enc)

}

@echo off

::设置要添加的内容

set str=这是首行内容

for /f "delims=" %%a in ('dir /a-d/s/b *.txt') do (

    (echo %str%

    type "%%~a")>tmp_file$

    move /y tmp_file$ "%%~a">nul

)

pause

这个就是对当前目录下(包括子目录),所有txt文档添加指定内容。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11852051.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-19
下一篇2023-05-19

发表评论

登录后才能评论

评论列表(0条)

    保存