我需要在几个文件的开头和结尾添加几行文本。
我想使用批处理脚本来执行此操作。
我要做的第一件事是替换文件扩展名并将它们更改为 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 代码。
如何解决此问题?
我有一个函数可以添加一个标题和尾部记录,这可能会做你想要的:
: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