Batch file to Write Filename to first line

2019-08-29 09:32发布

问题:

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.