我有个问题。我有许多文本文件(.txt)。我希望在它们中写入相应文本文件的名称,并最终合并它们。我有很多。我不能手动做,太费时了。
假设我有一个名为"windows.txt“的文本文件,其内容如下
Windows XP
Windows 7
Windows 8
我希望他们的内容是
windows
Windows XP
Windows 7
Windows 8
在合并了类似的文件后,它们应该如下所示
windows
Windows XP
Windows 7
Windows 8
continents
Europe
Asia
Africa
languages
English
Chinese
救命啊!!
发布于 2014-04-18 11:30:02
@echo off
echo. >result
for /f %%i in ('dir /b /a-d *.txt') do (
echo.
echo %%~ni
type %%i
)>>result
ren result result.txt
Pseude更好理解的代码:
turn echo off
delete result file
for all txt files in current directory do:
print empty line
print name of textfile without extension
print textfile
and put it into new file "result"
if done, rename the resulting file
发布于 2014-04-18 10:25:50
也许太容易了,但我认为这是正确的答案:
type *.txt >result 2>&1
ren result result.txt
诀窍是,当type
与文件列表或通配符一起使用时,它会错误地打印文件名,stream.And不会将其重定向为具有txt
扩展名的文件,以避免打印,但在最后被重命名。
关于写入它们的文件的名称(最好先测试一下)
@echo off
for %%a in (*.txt) do (
type %%~na.txt* >%%~na 2>&1
del /q /f %%~na.txt
ren %%~na %%~na.txt
)
编辑
如果启用8.3表示法,可能会有效:
@echo off
ren ????????.txt ???????? >nul 2>&1
for /f "tokens=* delims=" %%# in ('for %%a in (????????^) do @echo %%a') do (
echo %%#>%%~#_
rem echo %%~n#_
type %%~n# >>"%%~#_" 2>nul
del /q /f "%%~n#"
ren "%%~n#_" "%%~n#.txt"
)
https://stackoverflow.com/questions/23151851
复制相似问题