批处理脚本在文本前添加和追加文件

问题描述 投票:0回答:1

我需要在几个文件的开头和结尾添加几行文本。

我想要使用批处理脚本来执行此操作。

我要做的第一件事是替换文件扩展名并将其更改为XMP。

最初,文件名遵循此结构DJI_0035.mp4.json

所以我开始:

set mypath=%~dp0 ren *.mp4.json *.xmp

然后,我需要添加:

<?xpacket begin='' id='W5M0MpCehiHzreSzNTczkc9d'?>
<x:xmpmeta xmlns:x='adobe:ns:meta/' x:xmptk='Image::ExifTool 11.65'>
<rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>


 <rdf:Description rdf:about=''
  xmlns:exif='http://ns.adobe.com/exif/1.0/'>
  <exif:UserComment>
   <rdf:Alt>
    <rdf:li xml:lang='x-default'>

然后附加:

</rdf:li>
   </rdf:Alt>
  </exif:UserComment>
 </rdf:Description>
</rdf:RDF>
</x:xmpmeta>
<?xpacket end='w'?>

附加我尝试做的事情:

FOR %%G IN (*) DO echo "</rdf:li>
   </rdf:Alt>
  </exif:UserComment>
 </rdf:Description>
</rdf:RDF>
</x:xmpmeta>
<?xpacket end='w'?>" >> %%G "%~dp0"

这适用于简单的字符串,但不适用于XML代码。

如何解决这个问题?

batch-file append rename prepend
1个回答
0
投票

我有一个添加标题和尾部记录的函数,它们可能会执行您想要的操作:

:addHeaderAndTrailer
:: step 1 store headers and trailers into temp files
echo:%~1>_hdr.tmp
echo:%~2>_tlr.tmp

:: step 2 Concatenate the three
copy /a _hdr.tmp+%~3+_tlr.tmp _new.tmp>nul
move /y _new.tmp %~3>nul& rem 'move' saves a couple of copy/del steps

:: step 3 cleanup and exit
del _hdr.tmp & del _tlr.tmp

exit /b

:: /addHeaderAndTrailer

::addHeaderAndTrailer [1] [2] [3]
::===================
::FUNCTION appends header and trailer records into logs or other files
::PARAMETERS:
:: 1= Header Record
:: 1=  [/h] : this help screen
:: 2= Trailer Record
:: 3= Filename
::    if file doesn't exist, creates with just the headers and trailers
© www.soinside.com 2019 - 2024. All rights reserved.