文件夹内所有txt文档中内容,增加一列,列里面的内容就是文件名

2024-11-07 21:14:32
推荐回答(2个)
回答1:

wsfile 这个txt批量处理器可以实现,合并TXT就可以将很多txt放在一个txt里面 或者下面这个宏可以将文件名作为第一列,其他数据依次导入;你可以试试
Sub aa()
Dim s As String
s = Dir(ThisWorkbook.Path & "/*.txt")
Do While s <> ""
Open ThisWorkbook.Path & "/" & s For Input As #1
Do While Not EOF(1)
i = i + 1
Input #1, x
Cells(i, 2) = x
Cells(i, 1) = s
Loop
Close #1
s = Dir()
Loop
End Sub

回答2:

@echo off&setlocal Enabledelayedexpansion
rem 设置为txt文件的路径
set folderpath=C:\temp\
for /f "tokens=*" %%i in ('dir /b %folderpath%*.txt') do call :rn %%~ni
goto eof

:rn %1
set txt=,%1
type nul>%1_new.txt
for /f "tokens=*" %%j in (%1.txt) do echo %%j!txt!>>%1_new.txt
del /q %folderpath%%1.txt
ren %folderpath%%1_new.txt %1.txt

:eof