批量CMR - 删除前两行,在底行添加文本并更改扩展名

时间:2015-07-07 04:50:09

标签: batch-file rename edit

想知道如果你对编程有一点了解,你们是否可以帮助解决这些问题。

我要做的就是删除文本文件的前两行,在末尾添加unit= mm,然后将.txt到.tfm的扩展名更改为一批文件。

Ex:我会有一堆看起来像这样的.txt文件:

VERSION =   1
MATRIX  =
-8.53018635e-001   5.21877542e-001  -1.74356374e-003   1.16403895e+004
-4.70640528e-001  -7.70705865e-001  -4.29546229e-001   2.25759988e+004
-2.25514305e-001  -3.65590346e-001   9.03043187e-001  -3.03304760e+004
 0.00000000e+000   0.00000000e+000   0.00000000e+000   1.00000000e+000

我的输出应该是:

-9.23773447e-001   3.82937087e-001  -1.34365271e-003   1.57083071e+004
-3.45348709e-001  -8.34603411e-001  -4.29151971e-001   1.99811620e+004
-1.65459623e-001  -3.95975167e-001   9.03231299e-001  -3.39194884e+004
 0.00000000e+000   0.00000000e+000   0.00000000e+000   1.00000000e+000
unit= mm

然后将扩展名从.txt更改为.tfm。

所以基本上它始终是一致的6行文件,必须删除前两行,然后在末尾添加第5行,其中“units = mm”。 该文件也可以被覆盖。

希望有人可以帮我这个。

3 个答案:

答案 0 :(得分:1)

如果所有文本文件都在同一目录中(未测试):

@echo off
::change the dir location
set "dir_with_files=c:\somedir"

pushd "%dir_with_files%"
for %%# in (*.txt) do (
  break>"%%~n#.tfm"
  for "usebackq skip=2 tokens=* delims=" %%$ in ("%%~f#") do (
     (echo(%%$)>>"%%~n#.tfm" 
  )
  (echo(unit= mm)>>"%%~n#.tfm" 
)
:: check if the new files are ok and delete the echo to activate deletion
del /q /f *.txt

答案 1 :(得分:0)

for %%i in (*.txt) do (
    more +2 "%%i">"%%~ni.tfm"
    echo units=mm>>"%%~ni.tfm"
    REM del "%%i"
)

参考:more /?for /?

答案 2 :(得分:0)

非常感谢你的帮助。 最后我使用了:

for %%i in (*.txt) do (
    more +2 "%%i">>"%%~ni.tfm"
    echo units=mm>>"%%~ni.tfm"
    REM del %%i
)
del *.txt

它成功了。

一切顺利,再次感谢。