Batch file to Write Filename to first line

痞子三分冷 提交于 2019-12-11 18:17:48

问题


Hello and first thank you for taking the time to read this.

I am trying to write a simple batch file that will do one thing to every text file in a folder. The text file has the following format:

Format of txt files:
##### [delimeter "tab"] #####
##### [delimeter "tab"] #####
##### [delimeter "tab"] #####
....
EOF

I need to add one line as a header to each file in the folder. The header should consist of part of the filename. The filename will always have 2-3 letters and 3-5 numbers. I need the numbers with x and y appended to it.

For instance for 
File CJF0185.PRN 

0185X [delimeter "tab"] 0185Y
##### [delimeter "tab"] #####
##### [delimeter "tab"] #####
##### [delimeter "tab"] #####
....
EOF

So far I have only been able to add text to the first line of a single file that I know the name of.


回答1:


@echo off
setlocal enableDelayedExpansion
for %%F in (*.txt) do (
  for /f "delims=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" %%n in ("%%~nF") do (
    >%%F.new echo %%nX  %%nY
    >>%%F.new type %%F
    move /y %%F.new %%F
  )
)

There is a tab character hard coded in the first ECHO statement in the batch script, but I'm not sure that this site preserves the tab character. There is no known method to programmatically produce a tab character in batch.



来源:https://stackoverflow.com/questions/12335360/batch-file-to-write-filename-to-first-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!