批处理文件,将文件名附加到行尾 [英] batch file to append file names to end of lines
问题描述
我有一个文件目录,我需要遍历文件,将每个文件的内容输出到一个新文件,同时将文件名附加到每行的末尾.
I have a directory of files and I need to loop through the files, outputting the content of each file to a new file while appending the filename to the end of each line.
因此,从10个文件的目录中,我想最终得到一个由每个文件的内容组成的文件,但每个文件名都位于每行的末尾,这样我就知道它的原始文件来自哪个文件.
So from a directory of 10 files I want to end up with 1 file made up of the contents of each file but with each filename on the end of each line so i know which file it originaly came from.
我可以在每行末尾添加固定文本,但是我不知道如何使用文件名,也不能更改它以适用于任何文件.我试过使用通配符((%〜dp0 * .csv),但它说找不到指定的文件.
I can append fixed text to the end of each line but I can't work out how to use the file name and also change it to work for any file. I've tried using a wildcard((%~dp0*.csv) but it says it cant find the file specified.
这是我到目前为止所拥有的:
This is what I have so far:
for /F "delims=" %%j in (%~dp0\6691_706.csv) do echo.%%jAddToEndofLine >> %~dp0\New.txt
任何人都可以帮忙吗?谢谢.
Can anyone help? Thanks.
推荐答案
它失败了,因为FOR/F不允许文件使用通配符,因此它是用于逐行读取文件的.
您应该使用普通" FOR
It fails, as FOR /F doesn't allow wildcards for files, it is intended for reading a file line by line.
You should use the "normal" FOR
for %%A in (%~dp0\*.bat) do (
echo Processing file '%%A'
FOR /F "delims=" %%L in (%%A) do (
echo Line %%L from file %%A >> %~dp0\New.txt
)
)
这篇关于批处理文件,将文件名附加到行尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!