如何使用批处理将文件名加入到该文件的第1行中

如123.txt中aaaaa bbbbb 处理后变为 文件名为123.txt aaaaa bbbbb
2025-12-25 03:48:30
推荐回答(1个)
回答1:

如果只有这一个文件,批处理如下
echo 文件名为123.txt > temp.txt
type 123.txt >> temp.txt
del 123.txt
ren temp.txt 123.txt

如果你需要将文件名换成参数,也可以这样,比如建立一个叫做sample.bat,内容如下
@echo off
echo 文件名为%1 > temp.txt
type %1 >> temp.txt
del %1
ren temp.txt %1

用的时候用sample.bat 123.txt就行了。